cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Enrich logs with trace_id and span_id manually

tanhuionn
Newcomer

Hi Team,

We have some legacy Java systems that write logs into files with proprietary method. We would like to enrich the logs with trace_id and span_id from Dynatrace. 

With reference to "OpenTelemetry Java" in https://www.dynatrace.com/support/help/observe-and-explore/logs/log-monitoring/log-monitoring-config..., I found that getTraceId() and getSpanId() on Span.current().getSpanContext() always return 0000000000... (spanContext.isValid() return false).

  private static void dtLog(final String msg) {
SpanContext spanContext = Span.current().getSpanContext();
System.out.printf(
"[!dt dt.trace_id=%s,dt.span_id=%s] - %s%n",
spanContext.getTraceId(),
spanContext.getSpanId(),
msg
);
}

I debug on Span.current() in my program and the object contains the valid traceId and spanId (but not passed to the SpanContext). Log enrichment with %X{dt.trace_id} is working fine (I enabled "Java - Trace/span context enrichment for logs" in OneAgent features setting).

Not sure if this is relevant. Although my company has subscription on Dynatrace, I registered a trial account for convenience in testing. The OneAgent (Windows) version 1.259 was download from the trial account to test on my PC.

What could be the cause of this issue (invalid SpanContext) or Span.current.getSpanContext() is not supported?

1 REPLY 1

tanhuionn
Newcomer

After some debugging, I found that the Span.current() returns 

com.dynatrace.agent.introspection.span.opentelemetry.DynatraceCompositeSpan which contains dtSpan (Dynatrace Span) and otelSpan (Opentelemetry Span).  Calling getSpanContext() returns SpanContext from otelSpan.

If getSpanContext() is called directly on dtSpan (returned from calling compositeSpan.getDynatraceSpan()), then traceId and spanId are available in the SpanContext.

Is this behavior by design (which is different with document Connecting log data to traces | Dynatrace Docs) and is there any way to return dynatrace SpanContext from Span.current().getSpanContext()?

Featured Posts