Skip to content

Return the stored span from a bridged context - #20931

Open
Ayush0612005 wants to merge 1 commit into
open-telemetry:mainfrom
Ayush0612005:bridge-keep-application-span
Open

Ayush0612005 wants to merge 1 commit into
open-telemetry:mainfrom
Ayush0612005:bridge-keep-application-span

Conversation

@Ayush0612005

Copy link
Copy Markdown
Contributor

Fixes #20729

Follow-up to #20442. That PR caches the converted SpanContext on ApplicationSpan, but Span.fromContext(context) reads the span through ContextKeyBridge.get(), which builds a new ApplicationSpan on every call. The new wrapper starts with an empty cache, so Span.fromContext(context).getSpanContext() still converts each time.

Change

  • ContextKeyBridge gets an opt-in mode, enabled only for the span bridge. When the application stores a span, with() also keeps the application span next to the agent span (under a private agent context key), and get() returns that same instance as long as the agent span in the context is unchanged.
  • If agent code has replaced the span since, or the span was stored by agent code in the first place, get() converts it as before.
  • Besides avoiding the conversion, this matches the API without the agent: Span.fromContext(context.with(span)) returns span itself.

The trade-off is one extra entry in the agent context each time the application stores a span. Baggage and the other bridges are unchanged.

Testing

  • New SpanContextKeyBridgeTest in the unitTests suite. It checks that the stored span comes back as the same instance, also after other values are added to the context, and that a span replaced or stored by agent code is still converted.
  • With the mode turned off, the two identity tests fail.

@dpol1, could you run the reproducer from #20729 against this when you have time? context.with(span) would be worth checking too, since it now adds one more context entry.

Span.fromContext() on a context the application stored a span in went
through ContextKeyBridge.get(), which built a new ApplicationSpan on
every read. The new wrapper had an empty SpanContext cache, so each
call converted the span context again (open-telemetry#20729).

The span bridge now keeps the application span next to the agent span
when it is stored, and get() returns that same instance as long as the
agent span is unchanged. A span stored or replaced by agent code is
still converted as before.
@Ayush0612005
Ayush0612005 requested a review from a team as a code owner October 24, 2026 05:16
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-10-24 06:40 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrowly scoped change preserves conversion fallback and has focused regression coverage, with no blocking issues identified in static review.

No open issues 🎉

What changed in this PR

This PR extends the API bridge’s span-context caching work from #20442 by returning the original application span from a bridged context, avoiding repeated conversion on that path.

Changes:

  • Adds opt-in retention of the application value alongside its agent counterpart.
  • Enables retention only for spans, preserving conversion when agent code replaces or supplies the span.
  • Adds regression tests for span identity, unrelated context updates, and conversion fallback.
File Description
instrumentation/​opentelemetry-api/​opentelemetry-api-1.0/​javaagent/​src/​unitTests/​java/​io/​opentelemetry/​javaagent/​instrumentation/​opentelemetryapi/​v1_0/​context/​SpanContextKeyBridgeTest.java Tests retained span identity and agent-side fallback behavior.
instrumentation/​opentelemetry-api/​opentelemetry-api-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​opentelemetryapi/​v1_0/​context/​ContextKeyBridge.java Stores application values optionally and reuses them while agent identity matches.
instrumentation/​opentelemetry-api/​opentelemetry-api-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​opentelemetryapi/​v1_0/​context/​AgentContextWrapper.java Enables application-value retention for the span bridge only.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Javaagent bridge: Span.fromContext(context).getSpanContext() converts the span context on every call

2 participants