Skip to content

mcp: Reparent active span based on body's traceparent - #48038

Open
dmcardle wants to merge 5 commits into
envoyproxy:mainfrom
dmcardle:reparent-active-span
Open

dmcardle wants to merge 5 commits into
envoyproxy:mainfrom
dmcardle:reparent-active-span

Conversation

@dmcardle

@dmcardle dmcardle commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: mcp: Reparent active span based on body's traceparent

This PR adds new behavior to McpFilter controlled by the reparent_active_span option. When it's enabled, the filter will extract the body's traceparent, set attributes on the active span to place it under the incoming traceparent, and finally rewrite the request body and headers so the upstream will place its own spans under the active span.

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

I added integration tests for McpFilter and the opentelemetry tracer.

Additional Description: 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:]

@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: #48038 was opened by dmcardle.

see: more, trace.

@dmcardle
dmcardle force-pushed the reparent-active-span branch from b46a7aa to 9b55035 Compare October 5, 2026 22:07
@dmcardle
dmcardle marked this pull request as draft October 5, 2026 22:42
@dmcardle
dmcardle force-pushed the reparent-active-span branch 5 times, most recently from 7977540 to f560a81 Compare October 6, 2026 21:16
@dmcardle dmcardle changed the title [WIP] mcp: Reparent active span based on body's traceparent mcp: Reparent active span based on body's traceparent Oct 6, 2026
@dmcardle
dmcardle marked this pull request as ready for review October 6, 2026 21:28

@dmcardle dmcardle left a comment

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.

I'm not quite sure about all the non-opentelemetry overrides of the new setter methods, but I figure this is enough to get some early feedback.

PTAL @paul-r-gall and @yanavlasov

Comment thread source/extensions/tracers/xray/tracer.h Resolved
Comment thread envoy/tracing/trace_driver.h Resolved
Comment thread envoy/tracing/trace_driver.h Resolved
Comment thread envoy/tracing/trace_driver.h Resolved
if (active_span != nullptr && rewrite_result != nullptr) {
active_span->setTraceId(rewrite_result->parsed_traceparent.traceId());
active_span->setParentId(rewrite_result->original_parent_id);
active_span->setSampled(rewrite_result->parsed_traceparent.sampled());

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.

this is also a problem -- this allows a client to trigger sampled spans on every MCP request which can DoS/trigger expensive span exports for consumers of this filter.

Speaking purely from an otel perspective, this should (somehow) trigger a call into source/extensions/tracers/opentelemetry/tracer.cc callSampler method in order to respect the parent-based sampling policy on the configured tracer. I am not sure how this should work logistically.

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.

I'm considering adding another bool config knob that decides whether we copy the incoming sampling bit to the active span. Maybe this is worth doing in another PR. WDYT @paul-r-gall?

ASSERT(buffer != nullptr);

absl::StatusOr<RewriteTraceContextResult> rewrite_result =
rewriteBodyWithNewTraceparent(*buffer, new_parent_id);

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.

in cases where end_stream has not yet arrived (e.g. when shouldStopOnConfiguredAttributes is set), this buffer will not be the complete JSON body of the MCP request, and so the loadFromJsonNoThrow will fail. Additionally, the unconditional setContentLength below will cause a failure in such cases.

the "ideal" solution here is to track the offset of the tracing fields inside of McpJsonParser and do a rewrite directly into the buffer without re-parsing.

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.

in cases where end_stream has not yet arrived ...

I tried to deal with this by making McpFilter::decodeData return StopIterationAndBuffer when the new config knob is enabled and end_stream is false.

the "ideal" solution here is to track the offset of the tracing fields inside of McpJsonParser and do a rewrite directly into the buffer without re-parsing.

I couldn't figure out a way to get field offsets in McpFieldExtractor, via the ProtobufUtil::converter::ObjectWriter interface. I wound up re-parsing with Wuffs which is less than ideal. WDYT? Is it worth rewriting the McpFieldExtractor and friends to use Wuffs?

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.

@yanavlasov This thread is a little stale, but the question remains — should we try to avoid having two JSON parsers? The new code uses WuffsJsonCursor because it lets us extract token locations and thereby enables the quick-and-dirty rewrite algorithm in McpJsonParser::rewriteTraceContext(). (The naive alternative of fully parsing/serializing the request body opens a can of worms, such as numeric edge cases that don't round-trip.)

*/
bool isRemote() const { return is_remote_; }

void setTraceId(std::string trace_id) { trace_id_ = std::move(trace_id); }

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.

here and below: check for non-empty.

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.

@paul-r-gall Can you clarify? Do you think we should avoid overwriting trace_id_ with the empty string?

@dmcardle
dmcardle force-pushed the reparent-active-span branch 4 times, most recently from 5c042af to bc14dc6 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 reparent-active-span branch from bc14dc6 to c7403cb Compare October 29, 2026 18:59
@dmcardle
dmcardle force-pushed the reparent-active-span branch from c7403cb to 2a9caa7 Compare October 30, 2026 21:35
@dmcardle
dmcardle force-pushed the reparent-active-span branch 4 times, most recently from 127bc78 to c07fabe 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 reparent-active-span branch from c07fabe to 047f78d Compare November 3, 2026 16:14
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]>
@dmcardle
dmcardle force-pushed the reparent-active-span branch 2 times, most recently from b403f15 to 1f970e9 Compare November 23, 2026 20:21
@wbpcode wbpcode removed their assignment Nov 24, 2026
@dmcardle
dmcardle force-pushed the reparent-active-span branch 2 times, most recently from 6e5673c to 7109a42 Compare November 24, 2026 17:43

@yanavlasov yanavlasov 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.

/wait

// Stable-sorts the given `edits` by start position, then applies each edit to
// `body`. Rewrites without explicitly linearizing the buffer. Moves each
// `BufferEdit` in `edits`.
absl::Status rewriteBuffer(Buffer::Instance& body, absl::Span<BufferEdit> edits) {

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.

Will be we doing rewriting in some other places? If so we should encapsulate this in a class.

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.

I don't anticipate that we'll be reusing this method, but I can think of one place I would have used this abstraction in the past. Maybe if we come up with one more use case, it'd be worth factoring into a common library?

parsing_started_ = false;
}

absl::Status McpJsonParser::rewriteTraceContext(Buffer::Instance& body,

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.

Do we always rewrite trace context in MCP if there are trace headers?

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.

We don't always rewrite trace context — the behavior is controlled by the config's TraceContextPropagationMode. For instance, if the mode is COPY, McpFilter will update the trace context headers based on the body, but it will not call rewriteTraceContext().

enum TraceContextPropagationMode {
// Copy the request body's trace context to headers without modification.
COPY = 0;
// Copy the request body's trace context to headers without modification.
// Also reparents the active span based on the body's traceparent. In this
// mode, upstream spans will not descend from the active span; the subtree
// of spans will be a sibling of the active span.
COPY_AND_REPARENT_SPAN = 1;
// Rewrite the request body's trace context after reparenting the active
// span based on the body's trace context. In this mode, upstream spans will
// be descend from the active span.
REWRITE_AND_REPARENT_SPAN = 2;
}

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants