-
Notifications
You must be signed in to change notification settings - Fork 163
Add logfile actuator #1499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add logfile actuator #1499
Conversation
* 🧪 Add tests for endpoint options Add tests ensuring defaults are sell and can be overridden. * ✅ Implement endpoint options * ♻️ Refactor endpoint options and tests Fix warnings in tests as well, add reference in property docs, and update public APIs * 🧪 Add failing test for relative path Add a test that ensures config with relative path is relative to entry assembly * ✅ Add GetLogFilePath method Implement the GetLogFilePath to support relative paths * ♻️ Cleanup GetFilePath removed the hardcoding of the path to make use of the configuration options * 🧪 Test Options ✅ Implement test for Options * ♻️ Add tests to fix GetLogFilePath * 🧪 Add test for missing filepath config * ✅ Handle missing filepath config * ♻️ Make getlogfile more readable * 🧪 Add InvokeAsync test * ✅ Implement InvokeAsync InvokeAsync will return the contents of the log file * 🧪 Add test for InvokeAsync with no filepath When no file path is configured, InvokeAsync should return an empty string * ✅ Handle no file path in InvokeAsync * register with All actuators * Add to ActuatorHostBuilderTest * 🧪 Add middleware test
I'll update this PR with a link to a documentation update PR. I also wanted to check if the Management samples would be the correct one to update. |
Hi @tscrypter,
|
Hi @TimHess , Of course, I can make those updates tonight. Good catch on the Regarding handling large log file sizes, maybe introducing a Assuming 2048 byte log file:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Sonar issue has been fixed, please merge/rebase this PR on main. TestContext
was renamed to SteeltoeTestContext
, please adapt tests accordingly to fix build errors. In tests, async methods that take a CancellationToken
(such as app.StartAsync
and httpClient.GetAsync
) should be passed TestContext.Current.CancellationToken
, this is to prepare for xUnit3.
To fix PublicAPI warnings, it's easiest to clear PublicAPI.Unshipped.txt
, close the file, and then use the roslyn fixer (project-wide) to recreate the file.
src/Management/src/Endpoint/Actuators/LogFile/ConfigureLogFileEndpointOptions.cs
Outdated
Show resolved
Hide resolved
src/Management/src/Endpoint/Actuators/LogFile/ConfigureLogFileEndpointOptions.cs
Outdated
Show resolved
Hide resolved
src/Management/src/Endpoint/Actuators/LogFile/EndpointServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/Management/src/Endpoint/Actuators/LogFile/LogFileEndpointHandler.cs
Outdated
Show resolved
Hide resolved
src/Management/src/Endpoint/Actuators/LogFile/LogFileEndpointOptions.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/EndpointMiddlewareTest.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/EndpointServiceCollectionTest.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/EndpointServiceCollectionTest.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/LogFileEndpointTest.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/LogFileEndpointTest.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
* Replace delimited with separated * Review feedback
…ions for WindowsNetworkFileShare (SteeltoeOSS#1503) * Support network shares with disk health contributor * Embed Win32Exceptions for WindowsNetworkFileShare connection errors rather than translating --------- Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing the feedback. I wrongly commented about clearing PublicAPI.Unshipped.txt
. The first line (#nullable enable
) needs to stay, and then the analyzer can refill it.
Do you need some tips regarding ranges and file I/O, or did you just not get to it yet?
src/Management/test/Endpoint.Test/Actuators/LogFile/EndpointMiddlewareTest.cs
Outdated
Show resolved
Hide resolved
src/Management/test/Endpoint.Test/Actuators/LogFile/EndpointMiddlewareTest.cs
Outdated
Show resolved
Hide resolved
using Microsoft.Extensions.Configuration; | ||
using Steeltoe.Management.Endpoint.Configuration; | ||
|
||
namespace Steeltoe.Management.Endpoint.Actuators.Logfile; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The namespaces still have the old casing (also in tests).
src/Management/src/Endpoint/Actuators/LogFile/LogFileEndpointHandler.cs
Outdated
Show resolved
Hide resolved
…andler.cs Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
…ddlewareTest.cs Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
…ddlewareTest.cs Co-authored-by: Bart Koelman <104792814+bart-vmware@users.noreply.github.com>
Thanks @bart-vmware for the guidance. I haven't gotten to it yet, I will likely have some time this evening or tomorrow. I would like your input on the potential implementation before I refactor the code to support it. I plan on introducing For the |
Yes, that sounds about right. Some additional thoughts:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice progress, I've added a few remarks.
FileInfo logFile = new FileInfo(logFilePath); | ||
var logFileResult = new LogFileEndpointResponse(await File.ReadAllTextAsync(logFilePath, cancellationToken), | ||
logFile.Length, | ||
Encoding.Default, // StreamReader.CurrentEncoding is not reliable based on unit tests, so just use default for this .NET implementation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't make assumptions about the file encoding. It may vary per logger provider, over which Steeltoe has no control. For example, in Serilog, it can be overridden.
At the HTTP level, we're using bytes for ranges. The easiest would be to leave out charset
in the response Content-Type header. If we want to include it, it should originate from the endpoint configuration that Steeltoe users must set explicitly, depending on their logger framework.
options.AllowedVerbs.Add("Get"); | ||
options.AllowedVerbs.Add("Head"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EndpointOptions
provides a virtual method GetDefaultAllowedVerbs
to set these. For example usage, see LoggersEndpointOptions
.
public long? RangeStartByte { get; } = rangeStartByte; | ||
public long? RangeLastByte { get; } = rangeLastByte; | ||
public bool ReadContent { get; } = readContent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As described here, both the start and end positions are optional, so using nullable is good here. But let's make LogFileEndpointRequest
in the pipeline signature non-nullable. This is a public API. Otherwise, if we need to pass extra required information in the future, we need to take a breaking change. Adding an internal static get-only None
property is fine if you're concerned with reducing allocations.
{ | ||
public string? Content { get; } = content; | ||
public Encoding? LogFileEncoding { get; } = logFileEncoding; | ||
public long? ContentLength { get; } = contentLength; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on this, we may want to add the total file size, so it can be returned in the Content-Range
header.
HttpResponseMessage response = await client.SendAsync(headRequest, TestContext.Current.CancellationToken); | ||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal("text/plain", response.Content.Headers.ContentType?.MediaType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid using the ?.
operator in assertions, because no assertion is performed if the value is null
at runtime. To ensure it is non-null, assert on that first.
/azp run Steeltoe.All |
Azure Pipelines successfully started running 1 pipeline(s). |
|
@tscrypter Are you still working on this? |
Hi @bart-vmware, yes I am. Sorry for the delay, some critical issues came up at work. I'm hoping to have this wrapped up this weekend. |
Description
This PR adds a Logfile actuator. This endpoint will expose the contents of an logfile when it's configured. To log to a file, the application will need to make use of Third Party Logging providers.
The Third-party logging provider is responsible for log rotation.
This actuator can be configured with settings prefixed with
Management:Endpoints:Logfile
:Fixes #1496
Quality checklist
If your change affects other repositories, such as Documentation, Samples and/or MainSite, add linked PRs here.