Skip to content

Inject data in log4j2 that is also propagated via TextMapPropagator #6588

@TopoDiFogna

Description

@TopoDiFogna

Hello,

I currently have created an extension for the otel agent that uses a TextMapPropagator to propagate a custom header; I need to use this extension, together with the agent, of course, with a spring boot application.

My custom TextMapPropagator extracts the header, which value is a JSON, and creates a custom object that implements ImplicitContextKeyed; this TextMapPropagator is then registered using a ConfigurablePropagatorProvider

This object is saved inside the context using the <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter); method inherited from the interface using as key a public static final ContextKey<LoggingContext> defined in this custom object, then injects the same header using the <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter); method in the downstream http calls using the same key (reference); this works like a charm 😄 .

Now I need to inject some data contained in this custom object (deserialized from JSON) in every log line printed while managing the incoming http call so I defined a new TypeInstrumentation that creates an @Advice.OnMethodExit that basically creates a new ContextDataInjector which first calls injectContextData() on the original ContextDataInjector and then executes its custom logic.

The main problem is that when I try to extract from the context my custom object which was inserted by my TextMapPropagator, and I know that is there from the debugger, cannot be extracted because the public static final ContextKey<LoggingContext> is a different object since it my custom object is loaded also from another classloader.

For the TextMapPropagator my custom object is loaded from the application classloader but for the ContextDataInjector my custom object is loaded from the agent classpath.

Is there something that I'm missing in my implementation?

I followed this readme to implement my custom Instrumentation module for log4j.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions