opentelemetry-java-instrumentation icon indicating copy to clipboard operation
opentelemetry-java-instrumentation copied to clipboard

Javaagent bridge: `Context.with(span)` and `getSpanContext()` are slow for application spans

Open dpol1 opened this issue 2 months ago • 3 comments

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.

dpol1 avatar Oct 01 '26 13:10 dpol1

Thanks for the detailed benchmark. I checked main and the cause matches what you describe:

  • Bridging.toAgentOrNull(Span) calls applicationSpan.getSpanContext().isValid() before the instanceof ApplicationSpan check.
  • ApplicationSpan.getSpanContext() calls Bridging.toApplication(agentSpan.getSpanContext()). Each call builds a new application SpanContext with SpanContext.create(...), which re-validates the hex ids, and also rebuilds the TraceState through 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:

  1. Reorder toAgentOrNull. Check instanceof ApplicationSpan first and return getAgentSpan() directly. Bridging.toApplication(Span) only wraps valid agent spans and returns Span.getInvalid() otherwise, so an ApplicationSpan always holds a valid agent span and the validity check is redundant on that path. Non-ApplicationSpan inputs keep the existing invalid → Span.getInvalid() / otherwise null behaviour.
  2. Cache the converted SpanContext in ApplicationSpan. The agent span's SpanContext is immutable, so getSpanContext() can lazily compute the application SpanContext once 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.

Ayush0612005 avatar Oct 04 '26 10:10 Ayush0612005

Sounds good to me, go ahead. I can run the JMH reproducer against your PR once it's up.

dpol1 avatar Oct 06 '26 08:10 dpol1

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!

Ayush0612005 avatar Oct 07 '26 07:10 Ayush0612005