Skip to content

fix: honor upstream trace context in Step Functions Execution.Input._datadog (APMSVLS-513)#830

Merged
chemystery09 merged 1 commit into
mainfrom
ishara/step-trace-context
Jun 12, 2026
Merged

fix: honor upstream trace context in Step Functions Execution.Input._datadog (APMSVLS-513)#830
chemystery09 merged 1 commit into
mainfrom
ishara/step-trace-context

Conversation

@chemystery09

@chemystery09 chemystery09 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix legacy Step Functions trace extraction to read upstream Datadog headers from Execution.Input._datadog before falling back to hashing Execution.Id
  • Add _extract_dd_trace_id_from_dd_data helper and reuse it for the v1 lambda-root path
  • Add unit tests covering _datadog present (with/without _dd.p.tid) and absent fallback behavior

Jira: APMSVLS-513

Test plan

  • pytest tests/test_tracing.py::TestExtractAndGetDDTraceContext::test_step_function_trace_data_with_input_datadog
  • pytest tests/test_tracing.py::TestExtractAndGetDDTraceContext::test_step_function_trace_data_input_datadog_without_tid
  • pytest tests/test_tracing.py::TestExtractAndGetDDTraceContext::test_step_function_trace_data_input_without_datadog
  • Full tests/test_tracing.py suite in CI

Test Traces

Pre-fix (not connected):
https://ddserverless.datadoghq.com/apm/trace/141128433036377877332057539723763765387 -- upstream

https://ddserverless.datadoghq.com/apm/trace/173847457712912341096140011075384478337 -- downstream

Post-fix (connected, downstream lambda has an upstream parent):
https://ddserverless.datadoghq.com/apm/trace/141128412120142973548948775510277637565

Made with Cursor

…datadog

Legacy Step Functions invocations were ignoring upstream Datadog trace
headers nested under Execution.Input._datadog and always hashing
Execution.Id instead. Extract shared helper and read _datadog before
falling back to deterministic trace id generation.

APMSVLS-513

Co-authored-by: Cursor <cursoragent@cursor.com>
@datadog-official

datadog-official Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Pipelines

Fix all issues with BitsAI

⚠️ Warnings

🚦 6 Pipeline jobs failed

DataDog/datadog-lambda-python | publish-layer-sandbox (python310-amd64): [us-west-2]   View in Datadog   GitLab

DataDog/datadog-lambda-python | publish-layer-sandbox (python312-amd64): [us-west-2]   View in Datadog   GitLab

DataDog/datadog-lambda-python | publish-layer-sandbox (python313-amd64): [us-west-2]   View in Datadog   GitLab

View all 6 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c0d1379 | Docs | Datadog PR Page | Give us feedback!

@chemystery09 chemystery09 marked this pull request as ready for review June 11, 2026 18:23
@chemystery09 chemystery09 requested review from a team as code owners June 11, 2026 18:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Step Functions trace context extraction to prefer upstream Datadog trace headers found in Execution.Input._datadog (legacy JSONPath-style), and refactors shared parsing logic into a helper to reuse across extraction paths.

Changes:

  • Add _extract_dd_trace_id_from_dd_data helper to parse x-datadog-trace-id and optional _dd.p.tid from x-datadog-tags.
  • Update Step Functions extraction to read upstream trace context from Execution.Input._datadog before falling back to deterministic hashing of Execution.Id.
  • Add unit tests covering _datadog present (with/without _dd.p.tid) and absent fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
datadog_lambda/tracing.py Adds a helper for extracting Datadog trace id / tid and updates Step Functions extraction precedence to honor upstream context in Execution.Input._datadog.
tests/test_tracing.py Adds tests for legacy Step Functions events with Execution.Input._datadog and ensures correct fallback when absent.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread datadog_lambda/tracing.py
Comment on lines +545 to +547
execution = event.get("Execution", {})
dd_input = execution.get("Input", {}).get("_datadog")
trace_id = _extract_dd_trace_id_from_dd_data(dd_input, meta)

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.

Same here, should be fine

Comment thread datadog_lambda/tracing.py
Comment on lines +485 to +497
def _extract_dd_trace_id_from_dd_data(dd_data, meta):
"""
Read native Datadog trace headers from a `_datadog` dict.
Returns the 64-bit trace_id and populates meta["_dd.p.tid"] when present.
Returns None if no usable trace id is found.
"""
if not dd_data or "x-datadog-trace-id" not in dd_data:
return None
trace_id = int(dd_data.get("x-datadog-trace-id"))
high_64_bit_trace_id = _parse_high_64_bits(dd_data.get("x-datadog-tags"))
if high_64_bit_trace_id:
meta["_dd.p.tid"] = high_64_bit_trace_id
return trace_id

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.

I think this is fine. You're reusing the existing code so should be ok

@chemystery09 chemystery09 marked this pull request as draft June 12, 2026 17:16
@chemystery09 chemystery09 marked this pull request as ready for review June 12, 2026 17:41

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

LGTM! :shipit:

@chemystery09 chemystery09 merged commit f09eaa6 into main Jun 12, 2026
106 checks passed
@chemystery09 chemystery09 deleted the ishara/step-trace-context branch June 12, 2026 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants