feat: add OpenTelemetry trace correlation for LoggerFactoryLogger#706
Conversation
Implements trace context preservation across actor mailbox boundaries by leveraging LogEvent.ActivityContext added in Akka.NET 1.5.59. Key changes: - Add AkkaLogState struct with lazy enumeration for low-allocation trace context propagation in Microsoft.Extensions.Logging state dictionaries - Add AkkaTraceContextProcessor to extract Akka trace context and set LogRecord.TraceId/SpanId/TraceFlags for OpenTelemetry exporters - Add AddAkkaTraceCorrelation() extension method for easy configuration - Update LoggerFactoryLogger to include ActivityContext in log state - Bump Akka.NET to 1.5.59, Microsoft.Extensions to 8.0.0 - Add Aspire demo project for validation Closes akkadotnet#700
|
Validated this approach works for Phobos log/trace correlation. We created a prototype test using the This will allow us to deprecate Phobos's |
This reverts commit 8f9917c.
Aaronontheweb
left a comment
There was a problem hiding this comment.
Reviewed some of the changes here, have some fixes to make
| } | ||
| } | ||
|
|
||
| private static string SafeFormat(LogEvent log) |
There was a problem hiding this comment.
While all of these logging changes are still new, want to be careful about not err-ing out if there are formatting issues / config conflicts
|
|
||
| private static void SetTraceContext(LogRecord record, ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags traceFlags) | ||
| { | ||
| // LogRecord has internal setters, so we need to use reflection |
There was a problem hiding this comment.
this is true unfortunately, per the path the OTEL authors recommended to us open-telemetry/opentelemetry-dotnet#6085
There was a problem hiding this comment.
Going to update this to use UnsafeAccessor instead
There was a problem hiding this comment.
lol can't - due to .NET Framework
Aaronontheweb
left a comment
There was a problem hiding this comment.
Detailed my changes - good to go for a beta release IMHO
| <AkkaVersion>1.5.59</AkkaVersion> | ||
| <MicrosoftExtensionsVersion>[6.0.0,)</MicrosoftExtensionsVersion> | ||
| <SystemTextJsonVersion>[6.0.10,)</SystemTextJsonVersion> | ||
| <MicrosoftExtensionsVersion>[8.0.0,)</MicrosoftExtensionsVersion> |
There was a problem hiding this comment.
The OTEL packages require these
There was a problem hiding this comment.
Added documentation


Summary
Implements GitHub issue #700: Add OpenTelemetry trace correlation support to
LoggerFactoryLogger.Problem:
Activity.Current(used by OpenTelemetry) doesn't flow across actor mailbox boundaries because it usesAsyncLocal<T>. This means logs generated by actors lose their trace context correlation.Solution: Leverage
LogEvent.ActivityContextadded in Akka.NET 1.5.59, which captures trace context at log event creation time before the mailbox crossing.Changes
AkkaLogState- Low-allocation struct using lazy enumeration (yield return) to propagate trace context in MEL state dictionaries. StoresActivityTraceId/ActivitySpanIdas structs directly to avoidToString()allocations.AkkaTraceContextProcessor-BaseProcessor<LogRecord>that extractsAkka.TraceId,Akka.SpanId,Akka.TraceFlagsfrom log attributes and sets them onLogRecordfor OpenTelemetry exporters.AddAkkaTraceCorrelation()- Extension method onOpenTelemetryLoggerOptionsfor easy configuration.LoggerFactoryLogger- Updated to includeActivityContextin log state when available.Dependencies - Bumped Akka.NET to 1.5.59, Microsoft.Extensions to 8.0.0
Aspire Demo - Added demo project (
src/Examples/Akka.Hosting.OpenTelemetry.Demo/) with Seq for visual validationUsage
Test plan
AkkaLogState(8 tests)AkkaTraceContextProcessor(10 tests)Closes #700