Skip to content

Commit 8a2c8b5

Browse files
Second draft of implementation for extracting trace context from event['request']['headers']
1 parent 870ed87 commit 8a2c8b5

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

datadog_lambda/tracing.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -536,26 +536,26 @@ def extract_context_from_step_functions(event, lambda_context):
536536
logger.debug("The Step Functions trace extractor returned with error %s", e)
537537
return extract_context_from_lambda_context(lambda_context)
538538

539-
def extract_context_from_appsync_event(event, lambda_context):
539+
540+
def extract_context_from_request_header(event, lambda_context):
540541
"""
541-
Extract Datadog trace context from an AppSync event. Intended to replace
542-
need for customers to provide custom extractor function in their code.
542+
Attempt to extract Datadog trace context from an event payload where Datadog information
543+
is under the event["request"]["header"] section. Intended to replace some of the
544+
need for customers to provide custom extractor functions in their code for workflows such as
545+
RUM -> AppSync -> Datadog.
543546
"""
544-
nested_json = event.get("request", {}).get("headers", {})
545-
trace_id = nested_json.get("x-datadog-trace-id")
546-
parent_id = nested_json.get("x-datadog-parent-id")
547-
sampling_priority = nested_json.get("x-datadog-sampling-priority")
548-
context = Context(
549-
trace_id=int(trace_id),
550-
span_id=int(parent_id),
551-
sampling_priority=int(sampling_priority),
552-
)
547+
try:
548+
nested_json = event.get("request", {}).get("headers", {})
549+
context = propagator.extract(nested_json)
553550

554-
if not _is_context_complete(context):
555-
return extract_context_from_lambda_context(lambda_context)
551+
if not _is_context_complete(context):
552+
context = extract_context_from_lambda_context(lambda_context)
553+
except Exception:
554+
context = extract_context_from_lambda_context(lambda_context)
556555

557556
return context
558557

558+
559559
def extract_context_custom_extractor(extractor, event, lambda_context):
560560
"""
561561
Extract Datadog trace context using a custom trace extractor function
@@ -660,10 +660,8 @@ def extract_dd_trace_context(
660660
context = extract_context_from_kinesis_event(event, lambda_context)
661661
elif event_source.equals(EventTypes.STEPFUNCTIONS):
662662
context = extract_context_from_step_functions(event, lambda_context)
663-
elif event_source.equals(EventTypes.APPSYNC):
664-
context = extract_context_from_appsync_event(event, lambda_context)
665663
else:
666-
context = extract_context_from_lambda_context(lambda_context)
664+
context = extract_context_from_request_header(event, lambda_context)
667665

668666
if _is_context_complete(context):
669667
logger.debug("Extracted Datadog trace context from event or context")

0 commit comments

Comments
 (0)