feat(sqs): persist and return AWSTraceHeader system attribute (#909)

* feat(sqs): persist and return AWSTraceHeader system attribute

SendMessage's MessageSystemAttributes parameter is restricted by AWS
to AWSTraceHeader (the X-Ray trace context). Both the JSON and Query
handlers were silently dropping it, so a subsequent ReceiveMessage
with MessageSystemAttributeNames=["All"] returned only the four
auto-derived attributes instead of the expected five.

Add awsTraceHeader to the Message model, parse it from the
MessageSystemAttributes.AWSTraceHeader.StringValue location on the
wire (both JSON 1.0 and Query protocol), thread a new sendMessage
overload through SqsService, and surface it as a system attribute
when "All" or "AWSTraceHeader" is requested on receive. The existing
overloads remain so internal callers (SNS delivery, ESM polling, etc.)
are unaffected.

* feat(sqs): parse AWSTraceHeader on SendMessageBatch entries and add wire tests

Review feedback on #909:

- SendMessageBatch (both JSON 1.0 and Query) was still ignoring per-entry
  MessageSystemAttributes, so AWSTraceHeader was dropped in batched
  sends even after the single-SendMessage fix. Parse it the same way as
  the single-message path on both protocols and pass it through to the
  existing sendMessage overload.

- Coverage was service-level only. Add wire-level tests that assert
  AWSTraceHeader is parsed off MessageSystemAttribute(s) and emitted
  under Attributes.AWSTraceHeader on ReceiveMessage for both single
  SendMessage and SendMessageBatch on both protocols.

- Reword the awsTraceHeader field comment to make clear the value is
  caller-supplied via MessageSystemAttributes, not server-generated.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-20 17:13:56 -05:00
1 parent ce1f565003
commit 9bdb5436f0
7 files changed
+207 -8

No files matched your search

@@ -568,4 +568,90 @@ class SqsIntegrationTest {
.when().post("/");
}
}
@Test
void sendMessage_queryProtocol_persistsAwsTraceHeader() {
String traceQueueName = "query-trace-queue";
String traceQueueUrl = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateQueue")
.formParam("QueueName", traceQueueName)
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
try {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "SendMessage")
.formParam("QueueUrl", traceQueueUrl)
.formParam("MessageBody", "hi")
.formParam("MessageSystemAttribute.1.Name", "AWSTraceHeader")
.formParam("MessageSystemAttribute.1.Value.DataType", "String")
.formParam("MessageSystemAttribute.1.Value.StringValue", "Root=1-query-single")
.when().post("/").then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ReceiveMessage")
.formParam("QueueUrl", traceQueueUrl)
.formParam("MaxNumberOfMessages", "1")
.formParam("VisibilityTimeout", "0")
.formParam("MessageSystemAttributeName.1", "AWSTraceHeader")
.when().post("/").then().statusCode(200)
.body(containsString("<Name>AWSTraceHeader</Name>"))
.body(containsString("<Value>Root=1-query-single</Value>"));
} finally {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteQueue")
.formParam("QueueUrl", traceQueueUrl)
.when().post("/");
}
}
@Test
void sendMessageBatch_queryProtocol_persistsAwsTraceHeaderPerEntry() {
String traceQueueName = "query-batch-trace-queue";
String traceQueueUrl = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateQueue")
.formParam("QueueName", traceQueueName)
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
try {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "SendMessageBatch")
.formParam("QueueUrl", traceQueueUrl)
.formParam("SendMessageBatchRequestEntry.1.Id", "a")
.formParam("SendMessageBatchRequestEntry.1.MessageBody", "first")
.formParam("SendMessageBatchRequestEntry.1.MessageSystemAttribute.1.Name", "AWSTraceHeader")
.formParam("SendMessageBatchRequestEntry.1.MessageSystemAttribute.1.Value.DataType", "String")
.formParam("SendMessageBatchRequestEntry.1.MessageSystemAttribute.1.Value.StringValue", "Root=1-aaa")
.formParam("SendMessageBatchRequestEntry.2.Id", "b")
.formParam("SendMessageBatchRequestEntry.2.MessageBody", "second")
.formParam("SendMessageBatchRequestEntry.2.MessageSystemAttribute.1.Name", "AWSTraceHeader")
.formParam("SendMessageBatchRequestEntry.2.MessageSystemAttribute.1.Value.DataType", "String")
.formParam("SendMessageBatchRequestEntry.2.MessageSystemAttribute.1.Value.StringValue", "Root=1-bbb")
.when().post("/").then().statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ReceiveMessage")
.formParam("QueueUrl", traceQueueUrl)
.formParam("MaxNumberOfMessages", "10")
.formParam("VisibilityTimeout", "0")
.formParam("MessageSystemAttributeName.1", "AWSTraceHeader")
.when().post("/").then().statusCode(200)
.body(containsString("<Value>Root=1-aaa</Value>"))
.body(containsString("<Value>Root=1-bbb</Value>"));
} finally {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteQueue")
.formParam("QueueUrl", traceQueueUrl)
.when().post("/");
}
}
}
@@ -196,6 +196,48 @@ class SqsJsonProtocolTest {
.body("__type", containsString("BatchRequestTooLong"));
}
@Test
@Order(6)
void sendMessageBatchPersistsAwsTraceHeaderPerEntry() {
String traceQueueName = "json-batch-trace-queue";
String createBody = "{\"QueueName\":\"" + traceQueueName + "\"}";
String traceQueueUrl = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
.body(createBody)
.when().post("/").then().statusCode(200)
.extract().jsonPath().getString("QueueUrl");
String batchBody = "{\"QueueUrl\":\"" + traceQueueUrl + "\","
+ "\"Entries\":["
+ "{\"Id\":\"a\",\"MessageBody\":\"first\","
+ "\"MessageSystemAttributes\":{"
+ "\"AWSTraceHeader\":{\"DataType\":\"String\",\"StringValue\":\"Root=1-aaa\"}}},"
+ "{\"Id\":\"b\",\"MessageBody\":\"second\","
+ "\"MessageSystemAttributes\":{"
+ "\"AWSTraceHeader\":{\"DataType\":\"String\",\"StringValue\":\"Root=1-bbb\"}}}"
+ "]}";
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.SendMessageBatch")
.body(batchBody)
.when().post("/").then().statusCode(200)
.body("Successful", hasSize(2));
String receiveBody = "{\"QueueUrl\":\"" + traceQueueUrl + "\","
+ "\"MaxNumberOfMessages\":10,"
+ "\"MessageSystemAttributeNames\":[\"AWSTraceHeader\"]}";
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.ReceiveMessage")
.body(receiveBody)
.when().post("/").then().statusCode(200)
.body("Messages", hasSize(2))
.body("Messages.Attributes.AWSTraceHeader", hasItems("Root=1-aaa", "Root=1-bbb"));
}
@Test
@Order(7)
void receiveMessageOnEmptyQueueOmitsMessagesField() {
@@ -792,6 +792,16 @@ class SqsServiceTest {
assertEquals("BatchRequestTooLong", ex.getErrorCode());
}
@Test
void sendMessage_withAwsTraceHeader_isStoredAndReturnedOnReceive() {
Queue q = sqsService.createQueue("traced", null);
sqsService.sendMessage(q.getQueueUrl(), "hi", 0, null, null, null,
"Root=1-abc-def", "us-east-1");
var received = sqsService.receiveMessage(q.getQueueUrl(), 1, 30, 0);
assertEquals(1, received.size());
assertEquals("Root=1-abc-def", received.get(0).getAwsTraceHeader());
}
@Test
void purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {
final var sns = mock(SnsService.class);