Javaagent bridge: `Context.with(span)` and `getSpanContext()` are slow for application spans
Is your feature request related to a problem? Please describe.
I'm adding tracing to Apache Storm (apache/storm#9016). Storm calls only the OpenTelemetry API and documents the javaagent as the setup. With the agent, half to two thirds of the tracing cost in my profiles was in the agent's bridge, not in the SDK.
A JMH benchmark outside Storm, child span of an unsampled parent, ns/op:
| SDK in the application | agent 2.31.1 | |
|---|---|---|
span.getSpanContext() |
1.1 | 131.9 |
context.with(span) |
10.9 | 112.0 |
span.makeCurrent() + close() |
27.6 | 153.6 |
ApplicationSpan.getSpanContext() builds a new SpanContext on every call, which validates the hex ids again and builds a new TraceState. Bridging.toAgentOrNull(Span) calls it before its instanceof ApplicationSpan check, so every Context.with(span) pays for it.
Describe the solution you'd like
Check instanceof ApplicationSpan first in Bridging.toAgentOrNull and test validity on the agent span, or cache the application SpanContext in ApplicationSpan. I haven't measured either.
Describe alternatives you've considered
Registering an SDK in the application avoids the bridge, but loses the agent's configuration and instrumentation.
Additional context
Agent 2.31.1, API and SDK 1.66.0, JMH 1.36, OpenJDK 25.0.4.1, Linux x86-64. Reproducer (one JMH class and a script): https://gist.github.com/dpol1/77560026af8efb32378b308da929d263.
Thanks for the detailed benchmark. I checked main and the cause matches what you describe:
Bridging.toAgentOrNull(Span)callsapplicationSpan.getSpanContext().isValid()before theinstanceof ApplicationSpancheck.ApplicationSpan.getSpanContext()callsBridging.toApplication(agentSpan.getSpanContext()). Each call builds a new applicationSpanContextwithSpanContext.create(...), which re-validates the hex ids, and also rebuilds theTraceStatethrough a builder.
So every Context.with(span) and makeCurrent() on a bridged span pays for a full conversion just to throw it away.
I'd suggest two small changes:
- Reorder
toAgentOrNull. Checkinstanceof ApplicationSpanfirst and returngetAgentSpan()directly.Bridging.toApplication(Span)only wraps valid agent spans and returnsSpan.getInvalid()otherwise, so anApplicationSpanalways holds a valid agent span and the validity check is redundant on that path. Non-ApplicationSpaninputs keep the existing invalid →Span.getInvalid()/ otherwisenullbehaviour. - Cache the converted
SpanContextinApplicationSpan. The agent span'sSpanContextis immutable, sogetSpanContext()can lazily compute the applicationSpanContextonce and reuse it. A racy single-check in a field is enough, since recomputing is harmless.
(1) alone should fix Context.with(span), and (2) fixes direct getSpanContext() calls. I'm happy to open a PR with both plus a unit test, and to run your JMH reproducer before/after, if maintainers are OK with this approach.
Sounds good to me, go ahead. I can run the JMH reproducer against your PR once it's up.
Thanks! I’m definitely serious about taking this up and solving it. I’m a bit tied up for the next couple of days with some hackathon/Hacktoberfest work and a few company-related things, so I’d appreciate 2–3 days to properly work through the changes, tests, and benchmarking rather than rushing it.
I’ll get back to this as soon as I have the bandwidth and aim to put up a solid PR. Thanks for the patience!