Skip to content

feat: Add XRayExtractor to obtain Trace ID from environment (#494). - #510

Merged
cijothomas merged 36 commits into
open-telemetry:mainfrom
johannesfloriangeiger:494-opentelemetry-aws-lambda-trace-id
Jun 20, 2026
Merged

feat: Add XRayExtractor to obtain Trace ID from environment (#494).#510
cijothomas merged 36 commits into
open-telemetry:mainfrom
johannesfloriangeiger:494-opentelemetry-aws-lambda-trace-id

Conversation

@johannesfloriangeiger

@johannesfloriangeiger johannesfloriangeiger commented Jan 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #494

Changes

See title: Adds an XRayExtractor to obtain the Trace ID from an environment variable.

With the XRayPropagator looking for the x-amzn-trace-id field in the extractor, setting the context based on a Lambda invocation alone required extracting the Trace ID via the environment variable _X_AMZN_TRACE_ID when wanting to set the correct context. The XRayExtractor combines the extraction of the Trace ID from an HTTP header or, as a fallback, from the aforementioned environment variable.

Example use:

main.rs

async fn handler(_: LambdaEvent<Value>) -> Result<(), Error> {
    let x_amzn_trace_id = env::var("_X_AMZN_TRACE_ID").unwrap();
    println!("x_amzn_trace_id: {}", x_amzn_trace_id);

    println!(
        "Initial trace_id: {}",
        Context::current().span().span_context().trace_id()
    );

    let context = global::get_text_map_propagator(|propagator| {
        propagator.extract_with_context(&Context::current(), &XRayExtractor::new())
    });

    println!(
        "trace_id pre attach: {}",
        Context::current().span().span_context().trace_id()
    );

    let _guard = context.attach();

    println!(
        "trace_id post attach: {}",
        Context::current().span().span_context().trace_id()
    );

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    global::set_text_map_propagator(XrayPropagator::new());
    let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder().build();
    let tracer = tracer_provider.tracer(env::var("OTEL_SERVICE_NAME").unwrap());
    tracing_subscriber::registry()
        .with(tracing_opentelemetry::OpenTelemetryLayer::new(tracer))
        .init();

    Runtime::new(service_fn(handler))
        .layer(OTELLayer::new(|| {}))
        .run()
        .await
}

Start with OTEL_SERVICE_NAME=HTTPRustLambda cargo lambda watch, invoke with cargo lambda invoke --data-ascii '{}', see how the initial Trace ID differs from the environment variable and how after attaching the context the Trace ID is correct then.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@johannesfloriangeiger
johannesfloriangeiger requested a review from a team as a code owner January 4, 2026 17:14
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jan 4, 2026

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov

codecov Bot commented Jan 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.0%. Comparing base (c3e3914) to head (b1829f3).

Additional details and impacted files
@@           Coverage Diff           @@
##            main    #510     +/-   ##
=======================================
+ Coverage   76.4%   77.0%   +0.5%     
=======================================
  Files        127     130      +3     
  Lines      24255   24795    +540     
=======================================
+ Hits       18539   19093    +554     
+ Misses      5716    5702     -14     
Flag Coverage Δ
etw-integration 51.1% <ø> (?)
user-events-integration 82.6% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cijothomas

Copy link
Copy Markdown
Member

@jj22ee Could you review?

@cijothomas cijothomas closed this Jan 30, 2026
@cijothomas cijothomas reopened this Jan 30, 2026
@cijothomas

Copy link
Copy Markdown
Member

@jj22ee Could you review?

@jj22ee Gentle reminder!

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

Not an AWS expert to formally review. There isn't anyone from AWS reviewing either.

Comment thread opentelemetry-aws/src/trace/mod.rs Outdated
Comment thread opentelemetry-aws/Cargo.toml Outdated
Comment thread opentelemetry-aws/src/trace/xray_extractor.rs Outdated
Comment thread opentelemetry-aws/src/trace/xray_extractor.rs Outdated
@johannesfloriangeiger

Copy link
Copy Markdown
Contributor Author

LGTM once the comments are addressed and the CHANGELOG is updated.

All comments addressed and CHANGELOG updated!

@johannesfloriangeiger

Copy link
Copy Markdown
Contributor Author

After some more work with X-Ray Trace ID (based off SQS messages, to come) I decided to restructure the logic around Trace ID - instead of using the environment variable as basis and overwriting it with the header value, the map with values will now contain both values and it's the responsibility of the propagator to select which value is used (preferable the header and using the environment variable as fallback). This will pave the way for the next step which will add the Trace ID based off SQS message attributes as a third source of Trace ID.

Comment thread opentelemetry-aws/src/trace/xray_extractor.rs
@chinmaychahar

Copy link
Copy Markdown
Contributor

Please update the branch to resolve merge conflicts

@cijothomas

Copy link
Copy Markdown
Member

@johannesfloriangeiger Can you rebase and resolve conflicts please? This is good to merge then.

…-lambda-trace-id

# Conflicts:
#	opentelemetry-aws/CHANGELOG.md
#	opentelemetry-aws/Cargo.toml
@johannesfloriangeiger

Copy link
Copy Markdown
Contributor Author

Apologies @chinmaychahar and @cijothomas, as this has been sitting around for some time I forgot to check in!

@cijothomas

Copy link
Copy Markdown
Member

Apologies @chinmaychahar and @cijothomas, as this has been sitting around for some time I forgot to check in!

No worries. We are still sorting out this repo's processes and @chinmaychahar has stepped up to own AWS components recently!

@cijothomas
cijothomas added this pull request to the merge queue Jun 20, 2026
Merged via the queue into open-telemetry:main with commit 7098c4b Jun 20, 2026
28 checks passed
@johannesfloriangeiger
johannesfloriangeiger deleted the 494-opentelemetry-aws-lambda-trace-id branch June 20, 2026 20:04
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.

[Feature]: Extend XrayPropagator to support _X_AMZN_TRACE_ID environment variable

4 participants