-
Notifications
You must be signed in to change notification settings - Fork 782
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
Add logging buffering #5635
base: main
Are you sure you want to change the base?
Add logging buffering #5635
Conversation
src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLogger.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLogger.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLogger.cs
Outdated
Show resolved
Hide resolved
...Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferProvider.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferOptions.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBufferOptions.cs
Outdated
Show resolved
Hide resolved
...Core.Diagnostics.Middleware.Tests/Buffering/HttpRequestBufferLoggerBuilderExtensionsTests.cs
Outdated
Show resolved
Hide resolved
test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.cs
Show resolved
Hide resolved
...s/Microsoft.Extensions.Telemetry.Tests/Buffering/GlobalBufferLoggerBuilderExtensionsTests.cs
Outdated
Show resolved
Hide resolved
test/Libraries/Microsoft.Extensions.Telemetry.Tests/Logging/ExtendedLoggerTests.cs
Outdated
Show resolved
Hide resolved
e40b7b6
to
9d61d12
Compare
9d61d12
to
2f1a335
Compare
...Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/IHttpRequestBufferManager.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/BufferFilterRule.cs
Outdated
Show resolved
Hide resolved
...soft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferLoggerBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBufferOptions.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBufferOptions.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Buffering/HttpRequestBufferManager.cs
Outdated
Show resolved
Hide resolved
Address PR comments Add .NET 8 support Add ExceptionJsonConverter
883334d
to
022f00c
Compare
022f00c
to
a371d9c
Compare
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=896358&view=codecoverage-tab |
src/Libraries/Microsoft.Extensions.Telemetry/Logging/ExtendedLogger.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBuffer.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/ILoggingBuffer.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBuffer.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/GlobalBufferManager.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Telemetry/ILoggerFilterRule.cs
Outdated
Show resolved
Hide resolved
logger.Log( | ||
serializedRecord.LogLevel, | ||
serializedRecord.EventId, | ||
serializedRecord.Attributes, | ||
exception, | ||
(_, _) => serializedRecord.FormattedMessage ?? string.Empty); |
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.
This allocates a closure and a delegate for each ILogger.Log<TState> call. I can think of two ways to minimize the allocations:
- Instead of
serializedRecord.Attributes
, passserializedRecord
itself as the state to ILogger.Log<TState>. ImplementIReadOnlyList<KeyValuePair<string, object?>>
in SerializedLogRecord by calling the corresponding methods ofpublic IReadOnlyList<KeyValuePair<string, string>> Attributes
. - Alternatively, keep using the same delegate across all iterations of both
foreach
loops; declare theserializedRecord
variable and a delegate variable before the outer loop. That way, it would allocate only one closure and one delegate per BufferSink.LogRecords call, no matter how many log records are inIEnumerable<T> serializedRecords
. This is safe because ILogger.Log<TState> is not allowed to save the formatter delegate for later calling.
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.
makes sense for optimizations later, thank you
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.
This code moved from BufferSink to BufferedLoggerProxy in d7661a6. The BufferedLogRecord instances are not pooled, so each BufferedLogRecord call costs at least one allocation per BufferedLogRecord; that may make the delegate and closure allocations less important.
using Microsoft.Extensions.ObjectPool; | ||
|
||
namespace Microsoft.Extensions.Diagnostics.Buffering; | ||
internal sealed class PooledLogRecord : BufferedLogRecord, IResettable |
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 add DebuggerDisplayAttribute so that the list of log records in a buffer can be viewed more easily.
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.
Or add the DebuggerDisplayAttribute to SerializedLogRecord, instead.
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=925013&view=codecoverage-tab |
8319dc8
to
8a91c15
Compare
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=925328&view=codecoverage-tab |
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/StringifyComprarer.cs
Outdated
Show resolved
Hide resolved
d387358
to
4f524eb
Compare
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=925376&view=codecoverage-tab |
src/Libraries/Microsoft.Extensions.Telemetry/Buffering/BufferFilterRule.cs
Outdated
Show resolved
Hide resolved
/// <summary> | ||
/// Defines a rule used to filter log messages for purposes of futher buffering. | ||
/// </summary> | ||
[Experimental(diagnosticId: DiagnosticIds.Experiments.Telemetry, UrlFormat = DiagnosticIds.UrlFormat)] | ||
public class BufferFilterRule |
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.
This should have docs about what happens if a log entry matches a rule, and what happens if the log entry does not match any rule.
- Is the log entry discarded, buffered, or written directly to underlying loggers?
- Does it cause other log entries to be flushed from the buffer to underlying loggers? If so, does that apply to all log categories or only to the same category?
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.
Alternatively, if this type is used with different semantics in different contexts, then document the behaviour in HttpRequestBufferOptions.Rules and GlobalBufferOptions.Rules, and link to those docs from here.
5201765
to
7afd271
Compare
7afd271
to
b2b6e56
Compare
Related to the #5123 proposal, this PR is focused on the logging buffering only. See also the sampling part #5574.
Microsoft Reviewers: Open in CodeFlow