Skip to content

Commit 364aca5

Browse files
fix(aws-lambda): Don't link producers in the continued trace
The span links spec says not to add a message_origin link when the consumer continues the producer's trace. Build the links after continue_trace and skip any whose trace_id matches the invocation's. Also test a sentry-trace without a sampling flag, which links without sampled. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent 6bf1e29 commit 364aca5

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎sentry_sdk/integrations/aws_lambda/_handler.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
MAX_PRODUCER_LINKS = 128
4545

4646

47-
def _get_producer_links(aws_event: "Any") -> "list[SpanLink]":
47+
def _get_producer_links(aws_event: "Any", trace_id: str) -> "list[SpanLink]":
4848
"""Links to the spans that sent the SQS or SNS messages this invocation got."""
4949
records: "list[Any]" = []
5050
if isinstance(aws_event, dict):
@@ -88,6 +88,9 @@ def _get_producer_links(aws_event: "Any") -> "list[SpanLink]":
8888
data = extract_sentrytrace_data(sentry_trace)
8989
if data is None or not data["trace_id"] or not data["parent_span_id"]:
9090
continue
91+
# the invocation continues this trace, so a link would point into its own trace.
92+
if data["trace_id"] == trace_id:
93+
continue
9194

9295
link: "SpanLink" = {
9396
"trace_id": str(data["trace_id"]),
@@ -253,11 +256,6 @@ def sentry_handler(
253256
}
254257
)
255258

256-
# SQS and SNS messages link to their producers; they never parent the invocation.
257-
producer_links: "list[SpanLink]" = []
258-
with capture_internal_exceptions():
259-
producer_links = _get_producer_links(aws_event)
260-
261259
# a synchronous `Invoke` passes the invoking span in `ClientContext`; it wins over
262260
# event headers, which a forwarded HTTP event keeps from further upstream.
263261
# https://docs.aws.amazon.com/lambda/latest/dg/python-context.html
@@ -267,6 +265,17 @@ def sentry_handler(
267265
sentry_sdk.continue_trace(custom)
268266
else:
269267
sentry_sdk.continue_trace(headers)
268+
269+
# SQS and SNS messages link to their producers; they never parent the invocation.
270+
producer_links: "list[SpanLink]" = []
271+
with capture_internal_exceptions():
272+
producer_links = _get_producer_links(
273+
aws_event,
274+
sentry_sdk.get_current_scope()
275+
.get_active_propagation_context()
276+
.trace_id,
277+
)
278+
270279
Scope.set_custom_sampling_context(sampling_context)
271280
with sentry_sdk.start_span(
272281
name=function_name,

‎tests/integrations/aws_lambda/test_aws_lambda.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@ def _sns_notification(sentry_trace):
918918
_sqs_record(),
919919
# incomplete context: not linked.
920920
_sqs_record("-1"),
921+
# no sampling decision: linked without `sampled`.
922+
_sqs_record("5c6d7e8f9a0b4c1d8e2f3a4b5c6d7e8f-2b3c4d5e6f7a8b9c"),
921923
# a body that isn't JSON: skipped, other links kept.
922924
_sqs_record(body='not json: "sentry-trace"'),
923925
]
@@ -935,6 +937,11 @@ def _sns_notification(sentry_trace):
935937
"attributes": MESSAGE_ORIGIN_LINK,
936938
"sampled": False,
937939
},
940+
{
941+
"trace_id": "5c6d7e8f9a0b4c1d8e2f3a4b5c6d7e8f",
942+
"span_id": "2b3c4d5e6f7a8b9c",
943+
"attributes": MESSAGE_ORIGIN_LINK,
944+
},
938945
],
939946
id="sqs",
940947
),
@@ -1020,6 +1027,11 @@ def test_trace_continuation_from_client_context(
10201027
"sentry-trace": "471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"
10211028
}
10221029
},
1030+
"Records": [
1031+
# a producer in the continued trace: not linked, it's already in the trace.
1032+
_sqs_record("471a43a4192642f0b136d5159a501701-1a2b3c4d5e6f7a8b-1"),
1033+
_sqs_record("5c6d7e8f9a0b4c1d8e2f3a4b5c6d7e8f-2b3c4d5e6f7a8b9c-1"),
1034+
],
10231035
}
10241036

10251037
lambda_client.invoke(FunctionName="ClientContextTrace", Payload=json.dumps(payload))
@@ -1029,6 +1041,14 @@ def test_trace_continuation_from_client_context(
10291041
]
10301042
assert segment_span["trace_id"] == "471a43a4192642f0b136d5159a501701"
10311043
assert segment_span["parent_span_id"] == "6e8f22c393e68f19"
1044+
assert segment_span["links"] == [
1045+
{
1046+
"trace_id": "5c6d7e8f9a0b4c1d8e2f3a4b5c6d7e8f",
1047+
"span_id": "2b3c4d5e6f7a8b9c",
1048+
"attributes": MESSAGE_ORIGIN_LINK,
1049+
"sampled": True,
1050+
}
1051+
]
10321052

10331053

10341054
def test_request_attributes(lambda_client, test_environment):

0 commit comments

Comments
 (0)