Skip to content

feat(tracing): Add span links from SNS messages #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions elasticapm/contrib/serverless/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,20 @@ def __enter__(self):
transaction_name = "RECEIVE {}".format(record["eventSourceARN"].split(":")[5])

if "Records" in self.event:
# SQS
links = [
TraceParent.from_string(record["messageAttributes"]["traceparent"]["stringValue"])
for record in self.event["Records"][:1000]
if "messageAttributes" in record and "traceparent" in record["messageAttributes"]
]
# SNS
links += [
TraceParent.from_string(record["Sns"]["MessageAttributes"]["traceparent"]["Value"])
for record in self.event["Records"][:1000]
if "Sns" in record
and "MessageAttributes" in record["Sns"]
and "traceparent" in record["Sns"]["MessageAttributes"]
]
else:
links = []

Expand Down
4 changes: 4 additions & 0 deletions tests/contrib/serverless/aws_sns_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"City": {
"Type": "String",
"Value": "Any City"
},
"traceparent": {
"Type": "String",
"Value": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/contrib/serverless/aws_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ def test_func(event, context):
assert transaction["span_count"]["started"] == 1
assert transaction["context"]["message"]["headers"]["Population"] == "1250800"
assert transaction["context"]["message"]["headers"]["City"] == "Any City"
assert len(transaction["links"]) == 1
assert transaction["links"][0] == {"trace_id": "0af7651916cd43dd8448eb211c80319c", "span_id": "b7ad6b7169203331"}


def test_capture_serverless_sqs(event_sqs, context, elasticapm_client):
Expand Down
Loading