Skip to content

fix(cold_start): capture trace context before finishing spans#746

Merged
jchrostek-dd merged 1 commit intomainfrom
john/python-cold-start-traceid
Mar 16, 2026
Merged

fix(cold_start): capture trace context before finishing spans#746
jchrostek-dd merged 1 commit intomainfrom
john/python-cold-start-traceid

Conversation

@jchrostek-dd
Copy link
Contributor

@jchrostek-dd jchrostek-dd commented Mar 13, 2026

Summary

  • Fixes aws.lambda.cold_start spans having a different trace ID than the main aws.lambda span

Root Cause

tracer.current_trace_context() was being called after span.finish(), which could return a stale or empty context since the span was no longer active in the context provider.

How Node.js Correctly Handles This

The Node.js tracer library (datadog-lambda-js) passes the live parent span object to ColdStartTracer before calling finish():

// datadog-lambda-js/src/trace/listener.ts (lines 196-199)
const coldStartConfig: ColdStartTracerConfig = {
  parentSpan: didFunctionColdStart() ? this.inferredSpan || this.wrappedCurrentSpan : this.wrappedCurrentSpan,
  // ...
};

Then in cold-start-tracer.ts, the cold_start span is created as a child of this live span:

// datadog-lambda-js/src/trace/cold-start-tracer.ts (lines 49-64)
if (parentSpan) {
  options.childOf = parentSpan.span;  // Inherits correct trace_id
}

Changes

Moved trace context capture to before any spans are finished, ensuring aws.lambda.cold_start and aws.lambda.load spans inherit the correct trace ID. This mirrors the Node.js approach of capturing context while spans are still active.

Test plan

  • Added integration tests to datadog-lambda-extension and verified this worked. (will eventually merge these over to serverless-e2e-testsing).

🤖 Generated with Claude Code

@jchrostek-dd jchrostek-dd requested review from a team as code owners March 13, 2026 13:51
@jchrostek-dd jchrostek-dd requested review from shreyamalpani and removed request for shreyamalpani March 13, 2026 13:51
@jchrostek-dd jchrostek-dd changed the title fix(cold_start): capture trace context before finishing spans [WIP] fix(cold_start): capture trace context before finishing spans Mar 13, 2026
@jchrostek-dd jchrostek-dd force-pushed the john/python-cold-start-traceid branch 3 times, most recently from 76fa712 to 137668c Compare March 13, 2026 18:27
Move trace context capture BEFORE span.finish() so ColdStartTracer
creates spans with the correct trace_id.

Previously, tracer.current_trace_context() was called AFTER span.finish(),
which returned a stale context, causing cold start spans (aws.lambda.load
and import spans) to have a different trace_id than the main aws.lambda span.

Added unit test assertions to verify trace_id consistency.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jchrostek-dd jchrostek-dd force-pushed the john/python-cold-start-traceid branch from 137668c to fd9f1cb Compare March 13, 2026 18:28
@jchrostek-dd jchrostek-dd changed the title [WIP] fix(cold_start): capture trace context before finishing spans fix(cold_start): capture trace context before finishing spans Mar 16, 2026
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

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

:shipit:

@jchrostek-dd jchrostek-dd merged commit 64eba2f into main Mar 16, 2026
105 checks passed
@jchrostek-dd jchrostek-dd deleted the john/python-cold-start-traceid branch March 16, 2026 15:12
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.

2 participants