elastic / elastic/apm-agent-java
Provide general context key/value store with OTel bridge
- Dominant language
- Java
- Stars
- 594
- Forks
- 338
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 25
Description
Currently, when using the OTel bridge, the `Context` storage only works as expected to access the "current active span" when there is such.
The general contract of `Context` is to provide a general key-value store, and our implementation does not support that.
For example, any application that relies on storing an entry in the active context, for example using `Context.current().with(key, "value").makeCurrent()` will either directly fail or won´t provide the expected value when calling `Context.current().get(key)`.
However, the most common usage to retrieve the "active span" with `Span.current()` will work as expected as the bridged context will retrieve the active span from the `Tracer` state.
There are a few extra complications to making this rather simple storage challenging in practice:
- most entries are stored with keys that are `Class`, which will definitely be a challenge thanks to isolated classloading as identified in #2735,
- The implementation provided by OTel API is [`ArrayBasedContext`](https://github.com/open-telemetry/opentelemetry-java/blob/50408d499f85d5761d0a5ed9bf9d77d5ff01fff5/context/src/main/java/io/opentelemetry/context/ArrayBasedContext.java) which is package-private thus iterating over entries or copying the context might require extra efforts.
- re-creating (or wrapping) the `Context` object when accessed from another CL could be possible if we can map the entries purpose in the original context: for example, an entry with `Span` class as key would always be the "active span" which we can re-create from the `Tracer` state.
Contributor guide
Assessment
This issue has not been assessed yet.