From c7c2fe9a40e91c2c17f588bd2e8640d45c7d8e53 Mon Sep 17 00:00:00 2001 From: Jacky Lam Date: Thu, 24 Jul 2025 05:30:29 +0100 Subject: [PATCH 1/2] feat(tracing): Add span links from SNS messages --- elasticapm/contrib/serverless/aws.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/elasticapm/contrib/serverless/aws.py b/elasticapm/contrib/serverless/aws.py index 1717d57cc..e2af1a735 100644 --- a/elasticapm/contrib/serverless/aws.py +++ b/elasticapm/contrib/serverless/aws.py @@ -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 = [] From 7b4ee481711a8f70a8c62214c6875b0336ebfcbd Mon Sep 17 00:00:00 2001 From: Jacky Lam Date: Thu, 24 Jul 2025 12:05:09 +0100 Subject: [PATCH 2/2] fix: Update SNS capture_serverless test --- tests/contrib/serverless/aws_sns_test_data.json | 4 ++++ tests/contrib/serverless/aws_tests.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tests/contrib/serverless/aws_sns_test_data.json b/tests/contrib/serverless/aws_sns_test_data.json index e6c7a89ef..a1900c54b 100644 --- a/tests/contrib/serverless/aws_sns_test_data.json +++ b/tests/contrib/serverless/aws_sns_test_data.json @@ -27,6 +27,10 @@ "City": { "Type": "String", "Value": "Any City" + }, + "traceparent": { + "Type": "String", + "Value": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00" } } } diff --git a/tests/contrib/serverless/aws_tests.py b/tests/contrib/serverless/aws_tests.py index df062a378..f52636dcf 100644 --- a/tests/contrib/serverless/aws_tests.py +++ b/tests/contrib/serverless/aws_tests.py @@ -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):