Skip to content

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

Open
jchrostek-dd wants to merge 1 commit intomainfrom
john/python-cold-start-traceid
Open

[WIP] fix(cold_start): capture trace context before finishing spans#746
jchrostek-dd wants to merge 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

  • Integration tests in datadog-lambda-extension validate this fix (all 22 tests passing)
  • Python cold_start span tests now pass with the correct trace ID

🤖 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
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.

1 participant