Skip to content

Commit ab2381a

Browse files
ref(aws-lambda): Drop the producer links that #8257 already sets
#8257 links SQS/SNS invocations to their producers on the invocation span. Remove this PR's second copy of that code from _trigger.py, keep the queue.process span without links, and reuse the test helpers and records already on major/3.0. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent a922aa6 commit ab2381a

2 files changed

Lines changed: 9 additions & 196 deletions

File tree

‎sentry_sdk/integrations/aws_lambda/_trigger.py‎

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import json
21
from contextlib import nullcontext
32
from typing import TYPE_CHECKING
43

54
import sentry_sdk
65
from sentry_sdk.consts import OP, SPANDATA
76
from sentry_sdk.integrations.aws_lambda.consts import ORIGIN
8-
from sentry_sdk.traces import SENTRY_TRACE_HEADER_NAME
9-
from sentry_sdk.tracing_utils import extract_sentrytrace_data
10-
from sentry_sdk.utils import capture_internal_exceptions
117

128
if TYPE_CHECKING:
13-
from typing import Any, ContextManager, Optional
14-
15-
from sentry_sdk._types import SpanLink
9+
from typing import Any, ContextManager
1610

1711
# `faas.trigger` of each `eventSource` of `Records` or of a top-level `eventSource`.
1812
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html
@@ -32,13 +26,9 @@
3226
"aws:docdb": "datasource",
3327
}
3428

35-
# `messaging.system` of the event sources whose producers can propagate a trace.
29+
# `messaging.system` of the messaging event sources that get a `queue.process` span.
3630
_MESSAGING_SYSTEMS = {"aws:sqs": "aws_sqs", "aws:sns": "aws.sns"}
3731

38-
# OTel's default limit; Lambda delivers up to 10,000 SQS messages at once.
39-
# https://opentelemetry.io/docs/specs/otel/trace/sdk/#span-limits
40-
_MAX_PRODUCER_LINKS = 128
41-
4232
# https://docs.aws.amazon.com/eventbridge/latest/ref/overiew-event-structure.html
4333
_EVENTBRIDGE_KEYS = frozenset(
4434
(
@@ -106,72 +96,6 @@ def _get_trigger(aws_event: "Any") -> str:
10696
return _TRIGGERS.get(aws_event.get("eventSource", ""), "other")
10797

10898

109-
def _get_producer_trace(record: "dict[str, Any]") -> "Optional[str]":
110-
"""The `sentry-trace` the producer put in a record's message attributes."""
111-
# https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html
112-
if record.get("EventSource") == "aws:sns":
113-
attribute = (
114-
record["Sns"].get("MessageAttributes", {}).get(SENTRY_TRACE_HEADER_NAME, {})
115-
)
116-
return attribute.get("Value")
117-
118-
# https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
119-
attribute = record.get("messageAttributes", {}).get(SENTRY_TRACE_HEADER_NAME)
120-
if attribute is not None:
121-
return attribute.get("stringValue")
122-
123-
# without raw message delivery, an SNS subscription delivers the notification,
124-
# with its message attributes, as the body.
125-
# https://docs.aws.amazon.com/sns/latest/dg/sns-sqs-as-subscriber.html
126-
body = record.get("body", "")
127-
if '"TopicArn"' not in body:
128-
return None
129-
try:
130-
notification = json.loads(body)
131-
except ValueError:
132-
return None
133-
if not isinstance(notification, dict) or notification.get("Type") != "Notification":
134-
return None
135-
attribute = notification.get("MessageAttributes", {}).get(
136-
SENTRY_TRACE_HEADER_NAME, {}
137-
)
138-
return attribute.get("Value")
139-
140-
141-
def _get_producer_links(records: "list[Any]") -> "list[SpanLink]":
142-
"""One link per distinct producer context; messages never parent the invocation."""
143-
links: "list[SpanLink]" = []
144-
linked = set()
145-
for record in records:
146-
with capture_internal_exceptions():
147-
producer = extract_sentrytrace_data(_get_producer_trace(record))
148-
if (
149-
producer is None
150-
or not producer["trace_id"]
151-
or not producer["parent_span_id"]
152-
):
153-
continue
154-
155-
trace_id = str(producer["trace_id"])
156-
span_id = str(producer["parent_span_id"])
157-
if (trace_id, span_id) in linked:
158-
continue
159-
linked.add((trace_id, span_id))
160-
161-
link: "SpanLink" = {
162-
"trace_id": trace_id,
163-
"span_id": span_id,
164-
"attributes": {SPANDATA.SENTRY_LINK_TYPE: "message_origin"},
165-
}
166-
if producer["parent_sampled"] is not None:
167-
link["sampled"] = bool(producer["parent_sampled"])
168-
links.append(link)
169-
if len(links) == _MAX_PRODUCER_LINKS:
170-
break
171-
172-
return links
173-
174-
17599
def _start_process_span(aws_event: "Any") -> "ContextManager[Any]":
176100
"""A `queue.process` span around the handler for an SQS or SNS delivery."""
177101
records = aws_event.get("Records") if isinstance(aws_event, dict) else None
@@ -203,5 +127,4 @@ def _start_process_span(aws_event: "Any") -> "ContextManager[Any]":
203127
SPANDATA.MESSAGING_DESTINATION_NAME: destination,
204128
SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT: len(records),
205129
},
206-
links=_get_producer_links(records),
207130
)

‎tests/integrations/aws_lambda/test_aws_lambda.py‎

Lines changed: 7 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
DOCKER_NETWORK_NAME = "lambda-test-network"
2525
SAM_TEMPLATE_FILE = "sam.template.yaml"
2626
SAM_SHUTDOWN_TIMEOUT = 10
27-
MESSAGE_ORIGIN_LINK = {
28-
SPANDATA.SENTRY_LINK_TYPE: {"value": "message_origin", "type": "string"}
29-
}
3027

3128
SQS_RECORD = {
3229
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
@@ -853,43 +850,6 @@ def test_error(lambda_client, test_environment):
853850
assert _get_span_attr(attrs, SPANDATA.FAAS_TRIGGER) == "other"
854851

855852

856-
def _sqs_record(sentry_trace=None):
857-
record = {
858-
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
859-
"body": "{}",
860-
"messageAttributes": {},
861-
"eventSource": "aws:sqs",
862-
"eventSourceARN": "arn:aws:sqs:us-east-1:012345678912:orders",
863-
}
864-
if sentry_trace is not None:
865-
record["messageAttributes"]["sentry-trace"] = {
866-
"stringValue": sentry_trace,
867-
"stringListValues": [],
868-
"binaryListValues": [],
869-
"dataType": "String",
870-
}
871-
return record
872-
873-
874-
def _sns_record(sentry_trace=None):
875-
record = {
876-
"EventSource": "aws:sns",
877-
"Sns": {
878-
"Type": "Notification",
879-
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
880-
"TopicArn": "arn:aws:sns:us-east-1:012345678912:alerts",
881-
"Message": "{}",
882-
"MessageAttributes": {},
883-
},
884-
}
885-
if sentry_trace is not None:
886-
record["Sns"]["MessageAttributes"]["sentry-trace"] = {
887-
"Type": "String",
888-
"Value": sentry_trace,
889-
}
890-
return record
891-
892-
893853
@pytest.mark.parametrize(
894854
"aws_event,trigger",
895855
[
@@ -957,85 +917,16 @@ def test_trigger(lambda_client, test_environment, aws_event, trigger):
957917

958918

959919
@pytest.mark.parametrize(
960-
"aws_event,system,destination,links",
920+
"aws_event,system,destination",
961921
[
962922
pytest.param(
963-
{
964-
"Records": [
965-
_sqs_record("471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"),
966-
# the same send context: linked once.
967-
_sqs_record("471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"),
968-
_sqs_record("0f4c2a5b8d9e4f1a2b3c4d5e6f7a8b9c-1a2b3c4d5e6f7a8b-0"),
969-
_sqs_record(),
970-
# incomplete context: not linked.
971-
_sqs_record("-1"),
972-
]
973-
},
974-
"aws_sqs",
975-
"orders",
976-
[
977-
{
978-
"trace_id": "471a43a4192642f0b136d5159a501701",
979-
"span_id": "6e8f22c393e68f19",
980-
"attributes": MESSAGE_ORIGIN_LINK,
981-
"sampled": True,
982-
},
983-
{
984-
"trace_id": "0f4c2a5b8d9e4f1a2b3c4d5e6f7a8b9c",
985-
"span_id": "1a2b3c4d5e6f7a8b",
986-
"attributes": MESSAGE_ORIGIN_LINK,
987-
"sampled": False,
988-
},
989-
],
990-
id="sqs",
991-
),
992-
pytest.param(
993-
{
994-
"Records": [
995-
_sns_record("471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1")
996-
]
997-
},
998-
"aws.sns",
999-
"alerts",
1000-
[
1001-
{
1002-
"trace_id": "471a43a4192642f0b136d5159a501701",
1003-
"span_id": "6e8f22c393e68f19",
1004-
"attributes": MESSAGE_ORIGIN_LINK,
1005-
"sampled": True,
1006-
}
1007-
],
1008-
id="sns",
1009-
),
1010-
pytest.param(
1011-
{
1012-
"Records": [
1013-
{
1014-
**_sqs_record(),
1015-
"body": json.dumps(
1016-
_sns_record(
1017-
"471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"
1018-
)["Sns"]
1019-
),
1020-
}
1021-
]
1022-
},
1023-
"aws_sqs",
1024-
"orders",
1025-
[
1026-
{
1027-
"trace_id": "471a43a4192642f0b136d5159a501701",
1028-
"span_id": "6e8f22c393e68f19",
1029-
"attributes": MESSAGE_ORIGIN_LINK,
1030-
"sampled": True,
1031-
}
1032-
],
1033-
id="sns-to-sqs",
923+
{"Records": [SQS_RECORD, SQS_RECORD]}, "aws_sqs", "orders", id="sqs"
1034924
),
925+
pytest.param({"Records": [SNS_RECORD]}, "aws.sns", "alerts", id="sns"),
1035926
],
1036927
)
1037928
def test_messaging_process_span(
1038-
lambda_client, test_environment, aws_event, system, destination, links
929+
lambda_client, test_environment, aws_event, system, destination
1039930
):
1040931
lambda_client.invoke(
1041932
FunctionName="BasicOkSpanStreaming",
@@ -1049,12 +940,11 @@ def test_messaging_process_span(
1049940
for s in span_items
1050941
if _get_span_attr(s["attributes"], SPANDATA.SENTRY_OP) == OP.QUEUE_PROCESS
1051942
]
1052-
# messages are linked, never the parent of the invocation.
1053-
assert "parent_span_id" not in segment_span
1054943
assert process_span["parent_span_id"] == segment_span["span_id"]
1055944
assert process_span["trace_id"] == segment_span["trace_id"]
1056945
assert process_span["name"] == f"process {destination}"
1057-
assert process_span["links"] == links
946+
# producer links stay on the invocation span, see `test_producer_links`.
947+
assert "links" not in process_span
1058948

1059949
attrs = process_span["attributes"]
1060950
assert _get_span_attr(attrs, SPANDATA.SENTRY_KIND) == "consumer"
@@ -1080,7 +970,7 @@ def test_no_process_span_without_messages(lambda_client, test_environment):
1080970
def test_process_span_error(lambda_client, test_environment):
1081971
response = lambda_client.invoke(
1082972
FunctionName="RaiseErrorSpanStreaming",
1083-
Payload=json.dumps({"Records": [_sqs_record()]}),
973+
Payload=json.dumps({"Records": [SQS_RECORD]}),
1084974
)
1085975

1086976
# the handler's exception still reaches the Lambda runtime.

0 commit comments

Comments
 (0)