Class OpenTelemetryAppenderContextDataInjector

java.lang.Object
io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppenderContextDataInjector
All Implemented Interfaces:
org.apache.logging.log4j.core.ContextDataInjector

public final class OpenTelemetryAppenderContextDataInjector extends Object implements org.apache.logging.log4j.core.ContextDataInjector
Injects the full OpenTelemetry Context into Log4j event context data for async logger handoff.

This uses Log4j's ContextDataInjector extension point instead of ContextDataProvider because the appender needs to carry the Context object itself, not only string key/value pairs. Although Log4j 2.17 added ContextDataProvider.supplyStringMap(), Log4j does not call it for every thread context map. In web-app mode, which is enabled by default when the Servlet API is on the classpath, Log4j disables thread locals and uses DefaultThreadContextMap. That path calls ContextDataProvider.supplyContextData(), which is limited to Map<String, String>.

By delegating to Log4j's selected injector first and then adding the Context object to the resulting StringMap, this works for DefaultThreadContextMap, copy-on-write, and garbage-free thread context maps.

  • Constructor Details

    • OpenTelemetryAppenderContextDataInjector

      public OpenTelemetryAppenderContextDataInjector()
  • Method Details

    • injectContextData

      public org.apache.logging.log4j.util.StringMap injectContextData(List<org.apache.logging.log4j.core.config.Property> properties, org.apache.logging.log4j.util.StringMap reusable)
      Specified by:
      injectContextData in interface org.apache.logging.log4j.core.ContextDataInjector
    • rawContextData

      public org.apache.logging.log4j.util.ReadOnlyStringMap rawContextData()
      Specified by:
      rawContextData in interface org.apache.logging.log4j.core.ContextDataInjector