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

@@ -113,6 +113,9 @@ public class SqsJsonHandler {
&& (all || requested.contains("MessageDeduplicationId"))) {
attrs.put("MessageDeduplicationId", msg.getMessageDeduplicationId());
}
if (msg.getAwsTraceHeader() != null && (all || requested.contains("AWSTraceHeader"))) {
attrs.put("AWSTraceHeader", msg.getAwsTraceHeader());
}
if (!attrs.isEmpty()) {
msgNode.set("Attributes", attrs);
}
@@ -215,8 +218,13 @@ public class SqsJsonHandler {
});
}
// The AWS SDK only allows AWSTraceHeader to be set via MessageSystemAttributes;
// capture it (if present) so ReceiveMessage can return it as a system attribute.
String awsTraceHeader = request.path("MessageSystemAttributes")
.path("AWSTraceHeader").path("StringValue").asText(null);
Message msg = sqsService.sendMessage(queueUrl, messageBody, delaySeconds,
messageGroupId, messageDeduplicationId, messageAttributes, region);
messageGroupId, messageDeduplicationId, messageAttributes, awsTraceHeader, region);
ObjectNode response = objectMapper.createObjectNode();
response.put("MessageId", msg.getMessageId());
@@ -340,7 +348,7 @@ public class SqsJsonHandler {
ArrayNode failed = objectMapper.createArrayNode();
record ParsedEntry(String id, String body, int delay, String groupId, String dedupId,
Map<String, MessageAttributeValue> attributes) {}
Map<String, MessageAttributeValue> attributes, String awsTraceHeader) {}
List<ParsedEntry> parsedEntries = new ArrayList<>();
int totalSize = 0;
@@ -371,9 +379,13 @@ public class SqsJsonHandler {
});
}
String entryAwsTraceHeader = entry.path("MessageSystemAttributes")
.path("AWSTraceHeader").path("StringValue").asText(null);
totalSize += SqsService.computeMessageSize(messageBody, messageAttributes);
parsedEntries.add(new ParsedEntry(id, messageBody, delaySeconds,
messageGroupId, messageDeduplicationId, messageAttributes));
messageGroupId, messageDeduplicationId, messageAttributes,
entryAwsTraceHeader));
}
}
@@ -383,7 +395,8 @@ public class SqsJsonHandler {
String id = parsed.id();
try {
Message msg = sqsService.sendMessage(queueUrl, parsed.body(), parsed.delay(),
parsed.groupId(), parsed.dedupId(), parsed.attributes(), region);
parsed.groupId(), parsed.dedupId(), parsed.attributes(),
parsed.awsTraceHeader(), region);
ObjectNode success = objectMapper.createObjectNode();
success.put("Id", id);
success.put("MessageId", msg.getMessageId());
@@ -118,6 +118,10 @@ public class SqsQueryHandler {
xml.start("Attribute").elem("Name", "MessageDeduplicationId")
.elem("Value", msg.getMessageDeduplicationId()).end("Attribute");
}
if (msg.getAwsTraceHeader() != null && (all || requested.contains("AWSTraceHeader"))) {
xml.start("Attribute").elem("Name", "AWSTraceHeader")
.elem("Value", msg.getAwsTraceHeader()).end("Attribute");
}
}
private List<String> collectIndexed(MultivaluedMap<String, String> params, String prefix) {
@@ -213,7 +217,20 @@ public class SqsQueryHandler {
}
}
Message msg = sqsService.sendMessage(queueUrl, body, delaySeconds, messageGroupId, messageDeduplicationId, messageAttributes, region);
// The AWS SDK only allows AWSTraceHeader to be set via MessageSystemAttributes;
// capture it (if present) so ReceiveMessage can return it as a system attribute.
String awsTraceHeader = null;
for (int i = 1; ; i++) {
String name = getParam(params, "MessageSystemAttribute." + i + ".Name");
if (name == null) break;
if ("AWSTraceHeader".equals(name)) {
awsTraceHeader = getParam(params, "MessageSystemAttribute." + i + ".Value.StringValue");
break;
}
}
Message msg = sqsService.sendMessage(queueUrl, body, delaySeconds, messageGroupId,
messageDeduplicationId, messageAttributes, awsTraceHeader, region);
var xml = new XmlBuilder()
.elem("MessageId", msg.getMessageId())
@@ -315,7 +332,7 @@ public class SqsQueryHandler {
var xml = new XmlBuilder();
record ParsedEntry(String id, String body, int delay, String groupId, String dedupId,
Map<String, MessageAttributeValue> attributes) {}
Map<String, MessageAttributeValue> attributes, String awsTraceHeader) {}
List<ParsedEntry> parsedEntries = new ArrayList<>();
int totalSize = 0;
@@ -344,9 +361,20 @@ public class SqsQueryHandler {
}
}
String entryAwsTraceHeader = null;
for (int k = 1; ; k++) {
String name = getParam(params, "SendMessageBatchRequestEntry." + i + ".MessageSystemAttribute." + k + ".Name");
if (name == null) break;
if ("AWSTraceHeader".equals(name)) {
entryAwsTraceHeader = getParam(params,
"SendMessageBatchRequestEntry." + i + ".MessageSystemAttribute." + k + ".Value.StringValue");
break;
}
}
totalSize += SqsService.computeMessageSize(body, messageAttributes);
parsedEntries.add(new ParsedEntry(id, body, delaySeconds, messageGroupId,
messageDeduplicationId, messageAttributes));
messageDeduplicationId, messageAttributes, entryAwsTraceHeader));
}
sqsService.validateBatchPayloadSize(queueUrl, region, totalSize);
@@ -355,7 +383,8 @@ public class SqsQueryHandler {
String id = parsed.id();
try {
var msg = sqsService.sendMessage(queueUrl, parsed.body(), parsed.delay(),
parsed.groupId(), parsed.dedupId(), parsed.attributes(), region);
parsed.groupId(), parsed.dedupId(), parsed.attributes(),
parsed.awsTraceHeader(), region);
xml.start("SendMessageBatchResultEntry")
.elem("Id", id)
.elem("MessageId", msg.getMessageId())
@@ -374,6 +374,15 @@ public class SqsService {
String messageGroupId, String messageDeduplicationId,
Map<String, MessageAttributeValue> messageAttributes,
String region) {
return sendMessage(queueUrl, body, delaySeconds, messageGroupId, messageDeduplicationId,
messageAttributes, null, region);
}
public Message sendMessage(String queueUrl, String body, int delaySeconds,
String messageGroupId, String messageDeduplicationId,
Map<String, MessageAttributeValue> messageAttributes,
String awsTraceHeader,
String region) {
String storageKey = regionKey(region, queueUrl);
Queue queue = getQueueByUrl(storageKey, queueUrl)
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
@@ -459,6 +468,7 @@ public class SqsService {
message.setMessageGroupId(messageGroupId);
message.setMessageDeduplicationId(dedupId);
message.setSequenceNumber(sequenceCounter.incrementAndGet());
message.setAwsTraceHeader(awsTraceHeader);
if (effectiveDelaySeconds > 0) {
message.setVisibleAt(Instant.now().plusSeconds(effectiveDelaySeconds));
}
@@ -478,6 +488,7 @@ public class SqsService {
// Standard queue
Message message = new Message(body);
message.setAwsTraceHeader(awsTraceHeader);
if (effectiveDelaySeconds > 0) {
message.setVisibleAt(Instant.now().plusSeconds(effectiveDelaySeconds));
}
@@ -33,6 +33,11 @@ public class Message {
// surface.
private String originalSourceQueueUrl;
// Caller-supplied system attribute (the only entry AWS allows under
// MessageSystemAttributes on SendMessage). Returned under Attributes.AWSTraceHeader
// on ReceiveMessage when requested.
private String awsTraceHeader;
// Transient fields for visibility timeout tracking
@JsonIgnore
private String receiptHandle;
@@ -94,6 +99,9 @@ public class Message {
public String getOriginalSourceQueueUrl() { return originalSourceQueueUrl; }
public void setOriginalSourceQueueUrl(String originalSourceQueueUrl) { this.originalSourceQueueUrl = originalSourceQueueUrl; }
public String getAwsTraceHeader() { return awsTraceHeader; }
public void setAwsTraceHeader(String awsTraceHeader) { this.awsTraceHeader = awsTraceHeader; }
@JsonIgnore
public boolean isVisible() {
return visibleAt == null || !Instant.now().isBefore(visibleAt);