Closed Aaronontheweb closed 3 years ago
Reproduced this issue. The problem is that Serilog is accessing ActiveSpan
outside of the scope, and it is null
in spite of the fact that it is set on global tracer.
The solution to this is using the following EnrichLogging
implementation:
public static ILoggingAdapter EnrichLogging(IUntypedActorContext context)
{
var tracer = context.GetInstrumentation().Tracer;
var logger = context.GetLogger<SerilogLoggingAdapter>()
.ForContext("dd.trace_id", tracer.ScopeManager.Active.Span.Context.TraceId)
.ForContext("dd.span_id", tracer.ScopeManager.Active.Span.Context.SpanId);
return logger;
}
Note that you need to get SerilogLoggingAdapter
adapter, not just context.GetLogger()
, to have ForContext
helper working - as stated in the documentation: https://getakka.net/articles/utilities/serilog.html#extensions .
From a customer:
An example of how this got gets used in concert with the
Akka.Logger.Serilog
package inside a running Akka.NET actor (pseudo-code):Expected Results
We'd expect to see the Serilog structured logs contain the
dd.trace_id
anddd.span_id
properties populated with the values of the currentITracer.ActiveSpan.Context.TraceId
andITracer.ActiveSpan.Context.SpanId
properties respectively.Actual Results
Those properties are never added to the Serilog context.
Possible Issues
Some thoughts on what could cause this issue:
GlobalTracer.Instance
in this case is not set, thus all of the properties return null;ActiveSpan
in this context;LogEvent
by the Serilog logger tries to access theActiveSpan
outside of its scope, thus there is non available.Work-arounds
When setting up the
ITracer
you're going to pass into Phobos, explicitly set that tracer as theGlobalTracer.Instance
:If the issue is that the Serilog logger is trying to access contextual properties that aren't available when the Serilog logger runs asynchronously, we can work around that issue by closing over the properties from inside the Akka.NET actor, rather than having the Serilog
ILogEventEnricher
handle it. That way the appropriate span data can be captured during actor execution.