Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to obtain AWS Trace Header as part JMSListener headers ? #109

Open
ghoshrahul opened this issue May 6, 2022 · 1 comment

Comments

@ghoshrahul
Copy link

ghoshrahul commented May 6, 2022

Hi,

Amazon SQS supports tracing of message passed through it's queue using the system attribute. However on the consumer/receiver end, we are using JMSListener, with the basic configuration to receive message from the queue.

Is there a way to obtain the system attribute by using JMSListener? We would want to trace the entire message flow and figure the bottlenecks in our application.

Kindly point us to the documentation, or sample code that can be used for achieving this.

<dependency> <groupId>com.amazonaws</groupId> <artifactId>amazon-sqs-java-messaging-lib</artifactId> <version>1.0.8</version> </dependency>

Let me know if any additional information is required

@susverwimp
Copy link

I have a similar problem. We use the Java Opentelemetry agent spring-jms auto-instrumentation to get a full trace of producer -> consumer. This instrumentation expects either a traceparent or AWSTraceHeader property to be present in the properties map of the SQSMessage.

It seems the current implementation of com.amazon.sqs.javamessaging.message.SQSMessage does not allow for custom properties depending on System Attributes or Message Attributes.

I've managed to fix this by overloading the class in the Classpath with my own implementation. Just a copy-paste of the whole class, but in the constructer, I've added the following:

package com.amazon.sqs.javamessaging.message;

public class SQSMessage implements Message {
    ...
    SQSMessage(Acknowledger acknowledger, String queueUrl, software.amazon.awssdk.services.sqs.model.Message sqsMessage) throws JMSException {
        ...
        if(systemAttributes.containsKey(MessageSystemAttributeName.fromValue("AWSTraceHeader"))) {
            String awsTraceHeader = systemAttributes.get(MessageSystemAttributeName.fromValue("AWSTraceHeader"));
            String[] parts = awsTraceHeader.split(";");
            String root = null, parent = null, sampled = null;
            for (String part : parts) {
                if (part.startsWith("Root=")) {
                    root = part.substring(5);
                } else if (part.startsWith("Parent=")) {
                    parent = part.substring(7);
                } else if (part.startsWith("Sampled=")) {
                    sampled = part.substring(8);
                }
            }

            if(root != null && parent != null && sampled != null) {
                String[] rootParts = root.split("-");
                String traceId = rootParts[1] + rootParts[2];

                String spanId = parent;

                String flags = "00";
                if ("1".equals(sampled)) {
                    flags = "01";
                }

                properties.put("traceparent", new JMSMessagePropertyValue(String.format("00-%s-%s-%s", traceId, spanId, flags), STRING));
            }
        }
        ...
    }
    ...
}

This is pretty dirty fix as it does not handle upgrades very well as we need to diff possible changes (luckily this class does not change very often) + we have to make sure that our overloaded class is the first one the classloader will find.

It would be very useful to have some kind of message post-processing function that can do this during the processReceivedMessages(List messages) call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants