-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Describe the bug
The Log4J context data instrumentation seems to have been built with the assumption that Log4J initialization would take place after the OTel agent is initialized. If, for example, there is another agent whose premain is called before the OTel agent's and that premain uses Log4J, it will cause Log4J to initialize, load all available ContextDataProviders. As a result, the classloading of the OTel ContextDataProvider that the OTel agent might do is wasted.
Steps to reproduce
I cannot provide the exact steps due to the environment I'm dealing with being internal to my company. The general steps to reproduce would be to have a different agent run before the OTel agent and have that agent invoke Log4J. If you dump the ThreadContext during processing of requests, you will not see any trace context in it. I've tried this with an always_on sampler locally. In this case, if you include the Log4J instrumentation as a direct dependency of the application, everything will work again - likely because the Log4J service loader picks up the OTel CDP regardless of when the OTel agent is initialized.
Expected behavior
The ThreadContext should have trace context within it regardless of the environment in which the OTel agent was used.
Actual behavior
The ThreadContext contains no trace context.
Javaagent or library instrumentation version
Log4j-context-data-2.17
Environment
JDK: 8+
OS: Amazon Linux 2
Additional context
No response