Skip to content

opentelemetry: Override Span::updateParent() - #48448

Merged
yanavlasov merged 3 commits into
envoyproxy:mainfrom
dmcardle:otel-span-updateparent-method
Dec 1, 2026
Merged

yanavlasov merged 3 commits into
envoyproxy:mainfrom
dmcardle:otel-span-updateparent-method

Conversation

@dmcardle

@dmcardle dmcardle commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: opentelemetry: Override Span::updateParent()

This new override will be used by McpFilter in PR #48038.

Additional Description: This PR was created with help from generative AI (Gemini). I fully understand the code.

For reference, here is the full PR chain:

  1. api: Add mode field to TraceContextPropagationConfig in mcp.proto #48249
  2. envoy/tracing: Add method Envoy::Tracing::Span::updateParent() #48250
  3. opentelemetry: Add traceparent parse/serialize methods to SpanContextExtractor #48449
  4. opentelemetry: Override Span::updateParent() #48448
  5. opentelemetry: Move traceparent size constants into common lib #48516
  6. mcp: Reparent active span based on body's traceparent #48038
  7. opentelemetry: Add bool field resample_on_update_parent to OpenTelemetryConfig #48450
  8. opentelemetry: Resample in Span::updateParent() #48451

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]

@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #48448 was opened by dmcardle.

see: more, trace.

@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @wbpcode
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #48448 was ready_for_review by dmcardle.

see: more, trace.

@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch 2 times, most recently from 7be0ef7 to b9b1fd7 Compare October 21, 2026 17:53
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch 7 times, most recently from c721478 to 85d00fa Compare October 28, 2026 14:06
botengyao pushed a commit that referenced this pull request Oct 29, 2026
…48249)

<!--
!!!ATTENTION!!!

If you are fixing **any** crash or **any** potential security issue,
**do not
open a pull request**.

Instead, please
[open a GitHub Security
Advisory](https://github.com/envoyproxy/envoy/security/advisories/new)
(preferred). Alternatively, you may email
[email protected].

Thank you in advance for helping to keep Envoy secure.

!!!ATTENTION!!!

For an explanation of how to fill out the fields, please see the
relevant section
in
[PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/main/PULL_REQUESTS.md)

!!!ATTENTION!!!

Please check the [use of generative AI
policy](https://github.com/envoyproxy/envoy/blob/main/CONTRIBUTING.md?plain=1#L41).

You may use generative AI only if you fully understand the code. You
need to disclose
this usage in the PR description to ensure transparency.
-->

Commit Message:
Additional Description:

This is the first PR in a chain, with the high-level goal of getting
McpFilter to reparent the active span based on the request body's trace
context.

For reference, here is the full PR chain:

1. #48249
1. #48250
1. #48449
1. #48448
1. #48516
1. #48038
1. #48450
1. #48451

This PR was created with help from generative AI (Gemini). I fully
understand the code.

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: dmcardle <[email protected]>
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch 4 times, most recently from eea6baf to 71f2b97 Compare November 2, 2026 17:59
botengyao pushed a commit that referenced this pull request Nov 3, 2026
<!--
!!!ATTENTION!!!

If you are fixing **any** crash or **any** potential security issue,
**do not
open a pull request**.

Instead, please
[open a GitHub Security
Advisory](https://github.com/envoyproxy/envoy/security/advisories/new)
(preferred). Alternatively, you may email
[email protected].

Thank you in advance for helping to keep Envoy secure.

!!!ATTENTION!!!

For an explanation of how to fill out the fields, please see the
relevant section
in
[PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/main/PULL_REQUESTS.md)

!!!ATTENTION!!!

Please check the [use of generative AI
policy](https://github.com/envoyproxy/envoy/blob/main/CONTRIBUTING.md?plain=1#L41).

You may use generative AI only if you fully understand the code. You
need to disclose
this usage in the PR description to ensure transparency.
-->

Commit Message:
Additional Description: This change supports
#48038

This PR was created with help from generative AI (Gemini). I fully
understand the code.

For reference, here is the full PR chain:

1. #48249
1. #48250
1. #48449
1. #48448
1. #48516
1. #48038
1. #48450
1. #48451

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: dmcardle <[email protected]>
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from 71f2b97 to 2aa90df Compare November 3, 2026 16:14
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from 2aa90df to 4e8dc08 Compare November 19, 2026 19:29
botengyao pushed a commit that referenced this pull request Nov 19, 2026
…Extractor (#48449)

<!--
!!!ATTENTION!!!

If you are fixing **any** crash or **any** potential security issue,
**do not
open a pull request**.

Instead, please
[open a GitHub Security
Advisory](https://github.com/envoyproxy/envoy/security/advisories/new)
(preferred). Alternatively, you may email
[email protected].

Thank you in advance for helping to keep Envoy secure.

!!!ATTENTION!!!

For an explanation of how to fill out the fields, please see the
relevant section
in
[PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/main/PULL_REQUESTS.md)

!!!ATTENTION!!!

Please check the [use of generative AI
policy](https://github.com/envoyproxy/envoy/blob/main/CONTRIBUTING.md?plain=1#L41).

You may use generative AI only if you fully understand the code. You
need to disclose
this usage in the PR description to ensure transparency.
-->

Commit Message: opentelemetry: Add traceparent parse/serialize methods
to SpanContextExtractor

This new override will be used by McpFilter in PR #48038.

Additional Description: This PR was created with help from generative AI
(Gemini). I fully understand the code.

For reference, here is the full PR chain:

1. #48249
1. #48250
1. #48449
1. #48448
1. #48516
1. #48038
1. #48450
1. #48451

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: dmcardle <[email protected]>
@botengyao botengyao self-assigned this Nov 19, 2026
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from ce0f9e0 to 488c0c0 Compare November 19, 2026 19:55
@dmcardle

Copy link
Copy Markdown
Contributor Author

/retest

@dmcardle

Copy link
Copy Markdown
Contributor Author

@botengyao, PTAL!

botengyao
botengyao previously approved these changes Nov 20, 2026

@botengyao botengyao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks Dan for breaking this out! lgtm with 2 comments.

/wait

}

void Span::updateParent(const Tracing::TraceContext& trace_context, bool preserve_headers) {
preserve_headers_ = preserve_headers;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A couple of issues here:

  1. preserve_headers_ is set here, but never checked in Span::injectContext() in this PR (looks like that hunk stayed in mcp: Reparent active span based on body's traceparent #48038). Also we should only update preserve_headers_ after context.ok(), and propagate it to router child spans (spawn_upstream_span) while still allowing async client sidecalls (upstream.async_client_span_) to inject headers.
  2. For tracestate, if the span initially got tracestate from a different HTTP traceparent, merging leaks state across traces. Naive StrJoin can also produce duplicate keys (e.g. foo=2,foo=1), which violates W3C tracestate rules (Tracing::isValidTraceState()). Should we just replace it with context->tracestate() (matching Tracer::startSpan() with a parent context)?

Proposed fix:

void Span::updateParent(const Tracing::TraceContext& trace_context, bool preserve_headers) {
  SpanContextExtractor extractor(trace_context);
  absl::StatusOr<SpanContext> context = extractor.extractSpanContext();
  if (!context.ok()) {
    ENVOY_LOG(debug, "Unable to extract span context in updateParent: {}", context.status());
    return;
  }
  preserve_headers_ = preserve_headers;
  setParentContextIsRemote(context->isRemote());
  setTraceId(context->traceId());
  setParentId(context->spanId());
  setTracestate(context->tracestate());
}

And in Span::injectContext() (plus propagating preserve_headers_ in spawnChild()):

void Span::injectContext(Tracing::TraceContext& trace_context,
                         const Tracing::UpstreamContext& upstream) {
  if (preserve_headers_ && !upstream.async_client_span_) {
    return;
  }
  ...

wdyt?

@dmcardle dmcardle Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

preserve_headers

  1. preserve_headers_ is set here, but never checked in Span::injectContext() in this PR (looks like that hunk stayed in mcp: Reparent active span based on body's traceparent #48038).

Done. Good catch.

Also we should only update preserve_headers_ after context.ok(),

OK. The effect is that we'll leave the headers alone if we don't understand the trace context.

and propagate it to router child spans (spawn_upstream_span)

I think propagating to child spans makes sense. I'm just trying to figure out whether it's possible for spawnChild() to run before updateParent(). If it runs before, then the child span's preserve_headers_ value would potentially become stale. WDYT @botengyao, is this a valid concern?

while still allowing async client sidecalls (upstream.async_client_span_) to inject headers.

Done. Added a preserve_headers param to OpenTelemetry's Tracer::startSpan().

tracestate

  1. For tracestate, if the span initially got tracestate from a different HTTP traceparent, merging leaks state across traces. Naive StrJoin can also produce duplicate keys (e.g. foo=2,foo=1), which violates W3C tracestate rules (Tracing::isValidTraceState()). Should we just replace it with context->tracestate() (matching Tracer::startSpan() with a parent context)?>

You're right that the keys have to be unique. I'm new to tracing, and it turns out I misread the trace-context spec. Now I think I see where the uniqueness requirement comes in (section 3.3.1.4: "Only one entry per key is allowed because the entry represents that last position in the trace.").

I know @paul-r-gall had a concern about potentially clobbering the sampler-selected tracestate. It looks like we might already do that? First, we will use the tracestate from the sampler in callSampler() (I think the span is new and the tracestate should be empty). Then, in startSpan() we may overwrite the current tracestate with the given trace context. The sampler-selected value would be clobbered here, right? [Edit: I see that startSpan() only sets the tracestate when there's no sampler.]

I'll go ahead and replace the span's tracestate with the one from the MCP request body. I think this matches the current implementation — we definitely want traces based on the MCP request body to match traces based on the headers. WDYT @botengyao and @paul-r-gall?

@dmcardle

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @botengyao!

@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from e59aca6 to 9e952ae Compare November 23, 2026 16:27
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from 6edc76d to a0b46cd Compare November 24, 2026 15:56
@dmcardle
dmcardle force-pushed the otel-span-updateparent-method branch from a0b46cd to b5d48fd Compare November 24, 2026 17:43
@wbpcode wbpcode removed their assignment Nov 27, 2026
@yanavlasov
yanavlasov merged commit 4d4b902 into envoyproxy:main Dec 1, 2026
19 checks passed
yanavlasov pushed a commit that referenced this pull request Dec 2, 2026
This commit deduplicates constants related to W3C traceparent into
source/common/tracing/tracing_validation.h.

Additional Description:

This PR was created with help from generative AI (Gemini). I fully
understand the code.

For reference, here is the full PR chain:

1. #48249
1. #48250
1. #48449
1. #48448
1. #48516
1. #48038
1. #48450
1. #48451

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: dmcardle <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants