Skip to content

Commit fc7064f

Browse files
beniwohliColton Myers
andauthored
add span links from SQS messages in AWS lambda transactions (#1662)
* add span links from SQS messages in AWS lambda transactions closes #1473 * update changelog Co-authored-by: Colton Myers <[email protected]>
1 parent a7104da commit fc7064f

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ endif::[]
3939
* Add support for instrumenting the Elasticsearch 8 Python client {pull}1642[#1642]
4040
* Add `*principal*` to default `sanitize_field_names` configuration {pull}1664[#1664]
4141
* Add docs and better support for custom metrics, including in AWS Lambda {pull}1643[#1643]
42+
* Add support for capturing span links from AWS SQS in AWS Lambda {pull}1662[#1662]
4243
4344
[float]
4445
===== Bug fixes

elasticapm/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def begin_transaction(
302302
:param trace_parent: an optional TraceParent object for distributed tracing
303303
:param start: override the start timestamp, mostly useful for testing
304304
:param auto_activate: whether to set this transaction in execution_context
305+
:param links: a sequence of traceparent objects to causally link this transaction with
305306
:return: the started transaction object
306307
"""
307308
if self.config.is_recording:

elasticapm/contrib/serverless/aws.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,16 @@ def __enter__(self):
184184
transaction_type = "messaging"
185185
transaction_name = "RECEIVE {}".format(record["eventSourceARN"].split(":")[5])
186186

187-
self.transaction = self.client.begin_transaction(transaction_type, trace_parent=trace_parent)
187+
if "Records" in self.event:
188+
links = [
189+
TraceParent.from_string(record["messageAttributes"]["traceparent"]["stringValue"])
190+
for record in self.event["Records"][:1000]
191+
if "messageAttributes" in record and "traceparent" in record["messageAttributes"]
192+
]
193+
else:
194+
links = []
195+
196+
self.transaction = self.client.begin_transaction(transaction_type, trace_parent=trace_parent, links=links)
188197
elasticapm.set_transaction_name(transaction_name, override=False)
189198
if self.source in SERVERLESS_HTTP_REQUEST:
190199
elasticapm.set_context(

tests/contrib/serverless/aws_sqs_test_data.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
"stringListValues": [],
2929
"binaryListValues": [],
3030
"dataType": "String"
31+
},
32+
"traceparent": {
33+
"stringValue": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00",
34+
"stringListValues": [],
35+
"binaryListValues": [],
36+
"dataType": "String"
3137
}
3238
},
3339
"md5OfBody": "5eb63bbbe01eeed093cb22bb8f5acdc3",
@@ -36,4 +42,4 @@
3642
"awsRegion": "us-east-1"
3743
}
3844
]
39-
}
45+
}

tests/contrib/serverless/aws_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def test_func(event, context):
324324
assert transaction["span_count"]["started"] == 1
325325
assert transaction["context"]["message"]["headers"]["Population"] == "1250800"
326326
assert transaction["context"]["message"]["headers"]["City"] == "Any City"
327+
assert len(transaction["links"]) == 1
328+
assert transaction["links"][0] == {"trace_id": "0af7651916cd43dd8448eb211c80319c", "span_id": "b7ad6b7169203331"}
327329

328330

329331
def test_capture_serverless_s3_batch(event_s3_batch, context, elasticapm_client):

0 commit comments

Comments
 (0)