Skip to content

Commit 05a922a

Browse files
authored
SQS: add automatic propagation of AWSTraceHeader attribute when sending message (#12810)
1 parent 27dd3b2 commit 05a922a

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

localstack-core/localstack/services/sqs/provider.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,13 @@ def _create_message_attributes(
16791679
MessageSystemAttributeName.SenderId: context.account_id, # not the account ID in AWS
16801680
MessageSystemAttributeName.SentTimestamp: str(now(millis=True)),
16811681
}
1682+
# we are not using the `context.trace_context` here as it is automatically populated
1683+
# AWS only adds the `AWSTraceHeader` attribute if the header is explicitly present
1684+
# TODO: check maybe with X-Ray Active mode?
1685+
if "X-Amzn-Trace-Id" in context.request.headers:
1686+
result[MessageSystemAttributeName.AWSTraceHeader] = str(
1687+
context.request.headers["X-Amzn-Trace-Id"]
1688+
)
16821689

16831690
if message_system_attributes is not None:
16841691
for attr in message_system_attributes:

tests/aws/services/sqs/test_sqs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,45 @@ def test_system_attributes_have_no_effect_on_attr_md5(self, sqs_create_queue, aw
35303530
== "5ae4d5d7636402d80f4eb6d213245a88"
35313531
)
35323532

3533+
@markers.aws.validated
3534+
def test_aws_trace_header_propagation(self, sqs_create_queue, aws_sqs_client, snapshot):
3535+
def add_xray_header(request, **_kwargs):
3536+
request.headers["X-Amzn-Trace-Id"] = (
3537+
"Root=1-3152b799-8954dae64eda91bc9a23a7e8;Parent=7fa8c0f79203be72;Sampled=1"
3538+
)
3539+
3540+
try:
3541+
aws_sqs_client.meta.events.register("before-send.sqs.SendMessage", add_xray_header)
3542+
3543+
queue_url = sqs_create_queue()
3544+
3545+
msg_attrs_provider = {
3546+
"timestamp": {"StringValue": "1493147359900", "DataType": "Number"}
3547+
}
3548+
3549+
aws_sqs_client.send_message(
3550+
QueueUrl=queue_url, MessageBody="test", MessageAttributes=msg_attrs_provider
3551+
)
3552+
3553+
response = aws_sqs_client.receive_message(
3554+
QueueUrl=queue_url,
3555+
AttributeNames=["SentTimestamp", "AWSTraceHeader"],
3556+
MaxNumberOfMessages=1,
3557+
MessageAttributeNames=["All"],
3558+
VisibilityTimeout=2,
3559+
WaitTimeSeconds=2,
3560+
)
3561+
3562+
assert len(response["Messages"]) == 1
3563+
message = response["Messages"][0]
3564+
snapshot.match("xray-msg", message)
3565+
assert (
3566+
message["Attributes"]["AWSTraceHeader"]
3567+
== "Root=1-3152b799-8954dae64eda91bc9a23a7e8;Parent=7fa8c0f79203be72;Sampled=1"
3568+
)
3569+
finally:
3570+
aws_sqs_client.meta.events.unregister("before-send.sqs.SendMessage", add_xray_header)
3571+
35333572
@markers.aws.validated
35343573
def test_inflight_message_requeue(self, sqs_create_queue, aws_client):
35353574
visibility_timeout = 3

tests/aws/services/sqs/test_sqs.snapshot.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,5 +3974,43 @@
39743974
}
39753975
}
39763976
}
3977+
},
3978+
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_aws_trace_header_propagation[sqs]": {
3979+
"recorded-date": "27-06-2025, 10:55:55",
3980+
"recorded-content": {
3981+
"xray-msg": {
3982+
"Attributes": {
3983+
"AWSTraceHeader": "Root=1-3152b799-8954dae64eda91bc9a23a7e8;Parent=7fa8c0f79203be72;Sampled=1",
3984+
"SentTimestamp": "timestamp"
3985+
},
3986+
"Body": "test",
3987+
"MD5OfBody": "098f6bcd4621d373cade4e832627b4f6",
3988+
"MD5OfMessageAttributes": "235c5c510d26fb653d073faed50ae77c",
3989+
"MessageAttributes": {
3990+
"timestamp": "timestamp"
3991+
},
3992+
"MessageId": "<uuid:1>",
3993+
"ReceiptHandle": "<receipt-handle:1>"
3994+
}
3995+
}
3996+
},
3997+
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_aws_trace_header_propagation[sqs_query]": {
3998+
"recorded-date": "27-06-2025, 10:55:56",
3999+
"recorded-content": {
4000+
"xray-msg": {
4001+
"Attributes": {
4002+
"AWSTraceHeader": "Root=1-3152b799-8954dae64eda91bc9a23a7e8;Parent=7fa8c0f79203be72;Sampled=1",
4003+
"SentTimestamp": "timestamp"
4004+
},
4005+
"Body": "test",
4006+
"MD5OfBody": "098f6bcd4621d373cade4e832627b4f6",
4007+
"MD5OfMessageAttributes": "235c5c510d26fb653d073faed50ae77c",
4008+
"MessageAttributes": {
4009+
"timestamp": "timestamp"
4010+
},
4011+
"MessageId": "<uuid:1>",
4012+
"ReceiptHandle": "<receipt-handle:1>"
4013+
}
4014+
}
39774015
}
39784016
}

tests/aws/services/sqs/test_sqs.validation.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
{
2+
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_aws_trace_header_propagation[sqs]": {
3+
"last_validated_date": "2025-06-27T10:55:55+00:00",
4+
"durations_in_seconds": {
5+
"setup": 0.47,
6+
"call": 0.71,
7+
"teardown": 0.15,
8+
"total": 1.33
9+
}
10+
},
11+
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_aws_trace_header_propagation[sqs_query]": {
12+
"last_validated_date": "2025-06-27T10:55:56+00:00",
13+
"durations_in_seconds": {
14+
"setup": 0.0,
15+
"call": 0.68,
16+
"teardown": 0.15,
17+
"total": 0.83
18+
}
19+
},
220
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_change_message_visibility_after_visibility_timeout_expiration[sqs]": {
321
"last_validated_date": "2024-04-30T13:33:26+00:00"
422
},

0 commit comments

Comments
 (0)