open-telemetry/opentelemetry-java-instrumentation

Share Instrumentation's module contextStore across instrumentations modules

Closed this issue · 3 comments

Is your feature request related to a problem? Please describe.

I would like to store an object to the context store in a module A and access an object from the context store from instrumentation module B.

For instance my apache HC client instrumentation module hooks into HttpEntity.WriteTo() and puts OutputStream into a context as a key. Now I would like to access the context store in OutputStreamInstrumentation and check if the OutputStream is in the context - if it is instrumentation for write methods puts input into buffer (value from the context).

Describe the solution you'd like

Define context store in instrumentation module A and access it in instrumentation module B

Describe alternatives you've considered

Use a global instance of weak map to share objects/state across instrumentations from different modules.

FWIU (which could be wrong :) ), contextStore is basically just a mechanism to add fields to classes, or if that can't happen due to redefinition problems, falls back to a global weak map. I wouldn't see any reason for a context store with the same definition in two modules to not work, unless we explicitly fail to set a second contextStore if there's a duplicate definition, I guess we would be able to remove such a check if it exists.

I have tried to use the same context store in two inst modules it failed for some reason. I didn't dig deeper though.

Closing this seems to work as expected.