Skip to content

Commit 8fb2039

Browse files
jackylamhkxrmx
andauthored
feat(tracing): Add span links from SNS messages (#2363)
* feat(tracing): Add span links from SNS messages * fix: Update SNS capture_serverless test --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 090e841 commit 8fb2039

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

elasticapm/contrib/serverless/aws.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,20 @@ def __enter__(self):
235235
transaction_name = "RECEIVE {}".format(record["eventSourceARN"].split(":")[5])
236236

237237
if "Records" in self.event:
238+
# SQS
238239
links = [
239240
TraceParent.from_string(record["messageAttributes"]["traceparent"]["stringValue"])
240241
for record in self.event["Records"][:1000]
241242
if "messageAttributes" in record and "traceparent" in record["messageAttributes"]
242243
]
244+
# SNS
245+
links += [
246+
TraceParent.from_string(record["Sns"]["MessageAttributes"]["traceparent"]["Value"])
247+
for record in self.event["Records"][:1000]
248+
if "Sns" in record
249+
and "MessageAttributes" in record["Sns"]
250+
and "traceparent" in record["Sns"]["MessageAttributes"]
251+
]
243252
else:
244253
links = []
245254

tests/contrib/serverless/aws_sns_test_data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"City": {
2828
"Type": "String",
2929
"Value": "Any City"
30+
},
31+
"traceparent": {
32+
"Type": "String",
33+
"Value": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00"
3034
}
3135
}
3236
}

tests/contrib/serverless/aws_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def test_func(event, context):
344344
assert transaction["span_count"]["started"] == 1
345345
assert transaction["context"]["message"]["headers"]["Population"] == "1250800"
346346
assert transaction["context"]["message"]["headers"]["City"] == "Any City"
347+
assert len(transaction["links"]) == 1
348+
assert transaction["links"][0] == {"trace_id": "0af7651916cd43dd8448eb211c80319c", "span_id": "b7ad6b7169203331"}
347349

348350

349351
def test_capture_serverless_sqs(event_sqs, context, elasticapm_client):

0 commit comments

Comments
 (0)