Skip to content

Commit fd9f1cb

Browse files
jchrostek-ddclaude
andcommitted
fix(cold_start): capture trace context before finishing spans
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>
1 parent 8fe789e commit fd9f1cb

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

datadog_lambda/wrapper.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ def _after(self, event, context):
305305

306306
status_code = extract_http_status_code_tag(self.trigger_tags, self.response)
307307

308+
# Skip creating cold start spans in managed instances mode
309+
# In managed instances, the tracer library handles cold start independently
310+
should_trace_cold_start = (
311+
config.cold_start_tracing
312+
and is_new_sandbox()
313+
and not is_managed_instances_mode()
314+
)
315+
if should_trace_cold_start:
316+
trace_ctx = tracer.current_trace_context()
317+
308318
if self.span:
309319
if config.appsec_enabled and not self.blocking_response:
310320
asm_start_response(
@@ -342,15 +352,6 @@ def _after(self, event, context):
342352
create_dd_dummy_metadata_subsegment(
343353
self.trigger_tags, XraySubsegment.LAMBDA_FUNCTION_TAGS_KEY
344354
)
345-
# Skip creating cold start spans in managed instances mode
346-
# In managed instances, the tracer library handles cold start independently
347-
should_trace_cold_start = (
348-
config.cold_start_tracing
349-
and is_new_sandbox()
350-
and not is_managed_instances_mode()
351-
)
352-
if should_trace_cold_start:
353-
trace_ctx = tracer.current_trace_context()
354355

355356
if self.inferred_span:
356357
if status_code:

tests/test_cold_start.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,18 @@ def handler(event, context):
310310
handler.cold_start_tracing = True
311311
handler({}, lambda_context)
312312

313-
function_span = import_span = None
313+
function_span = import_span = load_span = None
314314
for span in spans:
315315
if span.resource == "tabnanny":
316316
import_span = span
317317
elif span.name == "aws.lambda":
318318
function_span = span
319+
elif span.name == "aws.lambda.load":
320+
load_span = span
319321

320322
assert function_span is not None
321323
assert import_span is not None
322324
assert import_span.parent_id == function_span.span_id
325+
assert import_span.trace_id == function_span.trace_id
326+
assert load_span is not None
327+
assert load_span.trace_id == function_span.trace_id

0 commit comments

Comments
 (0)