-
Notifications
You must be signed in to change notification settings - Fork 70
feat: Make ambient Activity tracing opt-in and enhance mapping config
#237
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: dev
Are you sure you want to change the base?
Conversation
nblumhardt
left a comment
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 taking a look!
I think this could work, but only if an enricher (ILogEventEnricher) is used to attach the OperationName property from Activity.Current - nothing else will currently set OperationName.
An enricher could be avoided by setting the operation name in the sink's Emit(LogEvent) method using ConditionalWeakTable, but this would require internally wrapping Serilog's batched sink implementation via LoggerSinkConfiguration.Wrap() so that the Emit() call is the one that happens on the application thread at the point the event is created, not the asynchronous/background thread EmitBatch..() methods.
....ApplicationInsights/Sinks/ApplicationInsights/TelemetryConverters/TelemetryConverterBase.cs
Show resolved
Hide resolved
9994dc5 to
c00d3f9
Compare
You're right. I'll take a look at it. I guess there are several camps when using this Sink. Personally, I don't really need the |
src/Serilog.Sinks.ApplicationInsights/LoggerConfigurationApplicationInsightsExtensions.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Sinks.ApplicationInsights/Sinks/ApplicationInsights/ApplicationInsightsSink.cs
Outdated
Show resolved
Hide resolved
|
Looking at #239 and the complexity involved here, I wonder if we're better off putting the new behavior added in 4.1 behind an opt-in configuration option? Currently it seems like fixing this reliably will take a while, so having the sink ignore |
Maybe. I want to spend some more time today to try to figure this out. So far, I've found out that the baggage items and potentially the operation name being present in traces in v4.0.0 of this sink may just have been a lucky coincidence: If I don't set/change the Line 151 in 2b3e6cd
Line 139 in 2b3e6cd
Before this line, the baggage of my manually created Activity is not present in the TraceTelemetry.Properties, and after the call to Track, the property magically appears. Same for the operation name in the nested OperationContext instance.
If I modify the call to using Activity disruptor = new("DisruptorActivity");
{
disruptor.AddBaggage("TakenFromAmbient", "true");
disruptor.Start();
_telemetryClient?.Track(telemetry);
}If I don't set |
@nblumhardt I suggest to
What's your opinion on this approach? |
|
That sounds good, @hangy, thanks for looking into it! @magnusn @ErikPilsits-RJW we'd need to validate that this fixes the issues you raised before shipping it; are you in a position to try out a |
|
Yes I am able to test a dev build. I'm a little unclear on if I need to make any other changes based on:
But just let me know what config changes I might need to make, if any. I guess that depends if you're asking me to test the reverted v4.1.1 or the new v5 also. But either way, let me know what you need. |
I prepared two branches based on tag v4.1.0 instead of
Sorry for causing these issues. It was totally unintended, and I hadn't checked baggage/operation name, as it wasn't one of my uses cases (and no tests existed for this implicit behaviour).
Thank you! A patched v4.1.1 or v4.2.0 should work the same as v4.0.0, except it won't manually set the var log = new LoggerConfiguration()
.WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
.Enrich.WithActivityBaggage() // To copy `Activity.Baggage` to log event properties
.Enrich.WithActivityOperatioName() // To copy the `OperationName` tag from `Activity.Tags` to log event properties
.CreateLogger(); |
…rent operation name
…ID, operation name, and version properties in telemetry converters
… features for Activity enrichment
Activity tracing opt-in and enhance mapping config
|
That sounds good, thank you. So if I'm reading this correctly, you're implying there's a risk here of getting Baggage that doesn't explicitly belong to the LogEvent, because it comes from the ambient Activity at the time the LogEvent is written, and that happens asynchronously? If I've understood that correctly, is this the case currently with v4? Our use case for the Baggage is to attach telemetry information to log events that occur downstream in our other API layers. This allows us to correlate business data more easily directly from those app insights log entries. It also attaches that data to our Exception telemetry. I'm not sure the mechanism here exactly. Is this not coming through Serilog? |
I don't know enough about the inner workings of Serilog to answer this with absolute certainty. In my local tests with v4.1.0, the ambient
Not directly. Prior to this PR, Serilog did not contain any code that explicitly copied the |
|
I tested the https://github.com/hangy/serilog-sinks-applicationinsights/commits/4.x-with-package-upgrades/ branch now and using it the operation name is back and it works as it did previously. Thanks for your work! |

This pull request introduces significant improvements to the documentation and enrichers for Serilog's Application Insights sink, clarifying configuration, enhancing support for distributed tracing, and adding new enrichment capabilities. The most important changes are grouped below:
Documentation and Configuration Improvements:
README.mdhas been extensively updated to clarify recommended configuration patterns, add installation instructions, and explain the precedence of operation ID sources and integration with distributed tracing libraries such as SerilogTracing. It also documents the new enrichers, upgrading guidance for 5.0, and details about operation-related properties mapping.Distributed Tracing and Enrichment:
ActivityOperationNameEnricherandActivityBaggageEnricher, which can be enabled via new extension methods (WithOperationName,WithBaggage) onLoggerEnrichmentConfigurationto automatically copy tracing context from the currentActivityto log events.LoggerEnrichmentConfigurationExtensionsto provide these enrichment methods for seamless integration.API and Code Quality Improvements:
LoggerConfigurationApplicationInsightsExtensionsto use the overload that accepts aTelemetryClientdirectly, simplifying the API and avoiding redundant sink construction.These changes collectively modernize the sink's documentation, improve support for distributed tracing scenarios, and make it easier for users to configure and enrich telemetry data.