Skip to content

Commit

Permalink
Adjust logic for context propagation for the inferred spans to avoid …
Browse files Browse the repository at this point in the history
…creating multiple aws apigateway spans for one request and adjust the test.
  • Loading branch information
wantsui committed Jan 24, 2025
1 parent 7d6b12d commit 85bcab2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ddtrace/_trace/trace_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ def _on_inferred_proxy_start(ctx, tracer, span_kwargs, call_trace, distributed_h
if not config._inferred_proxy_services_enabled:
return

# Skip creating another inferred span if one has already been created for this request
if ctx.get_item("inferred_proxy_span"):
return

# Inferred Proxy Spans
if distributed_headers_config and ctx.get_item("distributed_headers", None):
# Extract distributed tracing headers if they exist
Expand All @@ -225,7 +229,7 @@ def _on_inferred_proxy_start(ctx, tracer, span_kwargs, call_trace, distributed_h
inferred_proxy_span = ctx.get_item("inferred_proxy_span")

# use the inferred proxy span as the new parent span
if inferred_proxy_span and not call_trace:
if inferred_proxy_span:
span_kwargs["child_of"] = inferred_proxy_span
ctx.set_item("span_kwargs", span_kwargs)

Expand Down
3 changes: 2 additions & 1 deletion tests/contrib/django/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ def test_inferred_spans_api_gateway_distributed_tracing(client, test_spans):
trace_id=1,
parent_id=2,
metrics={
SAMPLING_PRIORITY_KEY: USER_KEEP,
_SAMPLING_PRIORITY_KEY: USER_KEEP,
},
sampled=True,
)
Expand All @@ -1876,6 +1876,7 @@ def test_inferred_spans_api_gateway_distributed_tracing(client, test_spans):
trace_id=1,
sampled=True,
)
assert len(test_spans.spans) == 27
assert aws_gateway_span.trace_id == 1
assert aws_gateway_span.parent_id == 2
assert aws_gateway_span.sampled is True
Expand Down

0 comments on commit 85bcab2

Please sign in to comment.