Class TraceContext

java.lang.Object
org.apache.juneau.microservice.logging.TraceContext

public final class TraceContext extends Object
Reflective accessor for the currently-active OpenTelemetry trace / span id, used by LogEntryFormatter to populate the optional {traceId} / {spanId} log fields.
Optional / reflective coupling

OpenTelemetry is not a compile-time dependency of juneau-microservice. This class probes for io.opentelemetry.api.trace.Span via Class.forName(String) at class-load time — mirroring how MdcAsyncListener reflectively handles SLF4J. When OTel is absent from the runtime classpath the class gracefully degrades: isAvailable() returns false and currentTraceId() / currentSpanId() return the empty string, so the {traceId} / {spanId} placeholders render empty.

Where the id comes from

The id is read from Span.current().getSpanContext() — the OTel context active on the calling thread. Inside a @RestOp handler (or a custom observation) wrapped by the OTel TracerHook, that is the request / observation span. An invalid (no active trace) context yields the empty string.

Since:
10.0.0
  • Method Details

    • isAvailable

      public static boolean isAvailable()
      Returns whether the OpenTelemetry trace API is available on the runtime classpath.

      When false, currentTraceId() and currentSpanId() always return the empty string.

      Returns:
      true if io.opentelemetry.api.trace.Span was found at class-load time.
    • currentTraceId

      public static String currentTraceId()
      Returns the trace id of the OTel span active on the current thread.
      Returns:
      The 32-char hex trace id, or the empty string when OTel is absent or no valid span is active.
    • currentSpanId

      public static String currentSpanId()
      Returns the span id of the OTel span active on the current thread.
      Returns:
      The 16-char hex span id, or the empty string when OTel is absent or no valid span is active.