Skip to content

fix(engine): propagate W3C traceparent from incoming HTTP requests#1769

Open
mikkel-arturo wants to merge 1 commit into
iii-hq:mainfrom
mikkel-arturo:main
Open

fix(engine): propagate W3C traceparent from incoming HTTP requests#1769
mikkel-arturo wants to merge 1 commit into
iii-hq:mainfrom
mikkel-arturo:main

Conversation

@mikkel-arturo

@mikkel-arturo mikkel-arturo commented Jun 4, 2026

Copy link
Copy Markdown

Replace the OtelContext::attach() + info_span!() pattern with set_parent() (via with_parent_headers) so incoming traceparent headers are honoured even for non-contextual spans at the top of the HTTP handler.

Affected sites:

  • views.rs: HTTP handler span (primary)
  • engine/mod.rs: handle_invocation, enqueue_action spans

What

Inject traceparent http header into span

Why

I want end to end observability but there was no way to get it when a service not connected to iii was the initiator of a trace.

Notes

I know nothing about either OTel or Rust, so have no clue what the effects of this may be, other than it is working for me.

  • I license my contributions to this repository under Apache 2.0, and I have all necessary rights over the code I am contributing.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved error handling for distributed tracing context propagation with enhanced logging and event tracking.
  • Refactor

    • Enhanced telemetry span construction for more reliable trace context attachment.
    • HTTP requests now automatically extract and propagate incoming W3C tracing context headers to downstream traces.

Replace the OtelContext::attach() + info_span!() pattern with
set_parent() (via with_parent_headers) so incoming traceparent
headers are honoured even for non-contextual spans at the top
of the HTTP handler.

Affected sites:
- views.rs: HTTP handler span (primary)
- engine/mod.rs: handle_invocation, enqueue_action spans
@vercel

vercel Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@mikkel-arturo is attempting to deploy a commit to the motia Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR refactors distributed tracing context propagation across the engine by introducing a unified SpanExt::with_parent_headers extension method. The method encapsulates parent trace context attachment with explicit result handling and diagnostic event emission. The approach is applied in the HTTP handler to extract and forward W3C tracing context from incoming requests, and in the engine to consistently parent spans for function invocation and action queueing.

Changes

Distributed Tracing Context Propagation

Layer / File(s) Summary
SpanExt::with_parent_headers extension implementation
engine/src/telemetry.rs
Adds TraceContextExt and KeyValue imports, implements with_parent_headers to match on set_parent result and emit success (traceparent.propagated) or failure (traceparent.set_parent_failed) span events with diagnostic attributes including parent trace ID and error details.
HTTP handler tracing context extraction and propagation
engine/src/workers/rest_api/views.rs
Imports telemetry::SpanExt, extracts traceparent and baggage headers from incoming request, and applies .with_parent_headers() to the HTTP span to link downstream traces to the caller's distributed trace context.
Engine span parenting refactoring
engine/src/engine/mod.rs
Updates spawn_invoke_function and enqueue_action to use .with_parent_headers(traceparent, baggage) directly on spans, replacing previous explicit context extraction and Context::attach() pattern; reorganizes telemetry import list formatting.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • iii-hq/iii#1643: Previous PR that inverted these changes in engine/src/engine/mod.rs by switching from .with_parent_headers() to explicit context extraction; this PR reverts that approach.
  • iii-hq/iii#178: Related REST API tracing instrumentation changes to preserve trace context across async boundaries in views.rs.

Suggested reviewers

  • ytallo
  • sergiofilhowz

Poem

🐰 Through spans and traces, context flows,
With parent headers now the engine knows,
HTTP echoes meet the queue,
Distributed paths shining through,
Telemetry blooms in twilight's glow. 🌱

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the primary change: propagating W3C traceparent headers from incoming HTTP requests to enable distributed tracing.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description addresses the template structure with What, Why, and Notes sections, but is somewhat terse and could be more comprehensive.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@engine/src/workers/rest_api/views.rs`:
- Around line 312-319: The code extracts a "baggage" header into bg and later
logs headers with sanitize_headers_for_logging but "baggage" isn't treated as
sensitive, so raw values can be emitted; update the extraction or sanitization
so baggage is redacted: either set bg to a redacted placeholder (e.g.,
"[REDACTED]") when reading the header in the same block that sets tp and bg, or
update sanitize_headers_for_logging to treat the "baggage" header as sensitive
so any later header logging will mask it; refer to the tp and bg variables and
the sanitize_headers_for_logging function when making the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 378efcbb-ad6e-45cb-b088-9e2cf85fa7d7

📥 Commits

Reviewing files that changed from the base of the PR and between cbc21ca and 1a051fe.

📒 Files selected for processing (3)
  • engine/src/engine/mod.rs
  • engine/src/telemetry.rs
  • engine/src/workers/rest_api/views.rs

Comment on lines +312 to +319
let tp = headers
.get("traceparent")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let bg = headers
.get("baggage")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Redact baggage before header logging.

Line 316 introduces explicit baggage ingestion, but sanitize_headers_for_logging does not classify it as sensitive, so Line 350 can emit raw baggage values (often user/session metadata) into logs.

🔧 Suggested patch
 const SENSITIVE_HEADERS: &[&str] = &[
     "authorization",
+    "baggage",
     "cookie",
     "set-cookie",
     "x-api-key",
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@engine/src/workers/rest_api/views.rs` around lines 312 - 319, The code
extracts a "baggage" header into bg and later logs headers with
sanitize_headers_for_logging but "baggage" isn't treated as sensitive, so raw
values can be emitted; update the extraction or sanitization so baggage is
redacted: either set bg to a redacted placeholder (e.g., "[REDACTED]") when
reading the header in the same block that sets tp and bg, or update
sanitize_headers_for_logging to treat the "baggage" header as sensitive so any
later header logging will mask it; refer to the tp and bg variables and the
sanitize_headers_for_logging function when making the change.

@guibeira

Copy link
Copy Markdown
Contributor

Hey @akashMishraX , thanks for your contribution. I reviewed it, and it looks good, but checking the W3C docs, i noticed it also recommends including trace-context:
https://www.w3.org/TR/trace-context/
Do you mind including it as well?
Also, can you include tests?

@guibeira

Copy link
Copy Markdown
Contributor

Also can you accept the Agreement Check?

@anthonyiscoding

Copy link
Copy Markdown
Contributor

@mikkel-arturo We'd be happy to add this (possibly with modifications) but first we need you to include the following acknowledgement verbatim by editing your original PR message

- [x] I license my contributions to this repository under Apache 2.0, and I have all necessary rights over the code I am contributing.

@iii-hq-ci

iii-hq-ci Bot commented Jun 22, 2026

Copy link
Copy Markdown

License agreement recorded

@mikkel-arturo, your agreement has been recorded and you have been added to contributors.md. All future PRs from your account will pass this check automatically.

@mikkel-arturo

Copy link
Copy Markdown
Author

Hey @anthonyiscoding @guibeira no problem. I added the agreement

As for adding tests, I'm unsure what to do, as

a) It's not clear to me what tests to add
b) I can't get the tests to run on my Windows box. There is some sort of issue with my Rust setup that I haven't had time to resolve.

Overall, as I said, as someone that doesn't know anything about either Rust or Otel and just spent time with Claude until it had the right behavior, I'm not confident in the code and would treat this PR more as a PoC that someone else will need to polish to ensure it's conformant.

That said, I have been using it in my base for a few weeks now and it's been working very well. I have been able to diagnose a large range of issues by having e2e observability.

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.

3 participants