1

I have microservice running in AWS ECS and listens to AWS SQS messages. I am using zipkin-aws to sent the traces to AWS Kinesis and collected in S3. When there is any REST Operation, the traces are sent and collected in S3 perfectly. But it doesnt capture the traces when the Microservice listens or send message to AWS queue. Could anyone help in configuring zipkin to listen to SQS messages.

Jennings
  • 506
  • 2
  • 9
  • 22

1 Answers1

1

According to Sleuth documentation, AWS SQS is "natively" supported only on the consumer's side:

https://docs.spring.io/spring-cloud-sleuth/docs/current-SNAPSHOT/reference/html/#spring-cloud-aws-messaging-sqs

In order to add seamless tracing over AWS SQS I resorted to Brave SQS instrumentation (aka SqsMessageTracing) and had to add another dependency:

    <dependency>
      <groupId>io.zipkin.aws</groupId>
      <artifactId>brave-instrumentation-aws-java-sdk-sqs</artifactId>
      <version>0.21.2</version>
    </dependency>

and have the following configuration:

@Configuration
public class SQSConfig {

  @Autowired
  private Tracing tracing;

  @Autowired
  private AWSCredentialsProvider awsCredentialsProvider;

  @Autowired
  private RegionProvider regionProvider;

  @Bean
  public AmazonSQSAsync amazonSQS() {
    SqsMessageTracing sqsMessageTracing = SqsMessageTracing.create(tracing);

    return AmazonSQSAsyncClientBuilder.standard()
      .withRegion(regionProvider.getRegion().getName())
      .withCredentials(awsCredentialsProvider)
      .withRequestHandlers(sqsMessageTracing.requestHandler())
      .build();
  }

  @Bean
  public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync sqsClient) {
    QueueMessagingTemplate template = new QueueMessagingTemplate(sqsClient);
    template.setMessageConverter(getMappingJackson2MessageConverter());
    return template;
  }
}

This is just because I didn't want to do the SQS producer instrumentation myself nor add the tracing headers programmatically. Little reference for the Brave instrumentation can be found here:

https://github.com/spring-cloud/spring-cloud-sleuth/issues/1550#issuecomment-589686583

My SQS message producer looks like this:

@Component
public class MessageAdapter {

  @Autowired
  private final QueueMessagingTemplate queueMessagingTemplate;


  public void sendSqsMessage(Object payload) {
    HashMap<String, Object> headers = new HashMap<>();
    headers.put("message-group-id", UUID.randomUUID().toString());
    queueMessagingTemplate.convertAndSend("sqs-queue-1", payload, headers);
  }
}

FINAL NOTE

Not required but I also excluded the

org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration

Since it performs an AWS environment configuration scan at application startup which wasn't required for me (and also raised a lengthy error log)

@SpringBootApplication
// Unwanted autoconfiguration, which raises a lengthy warning at startup,
// brought in by Brave AWS SQS instrumentation
@EnableAutoConfiguration(exclude = ContextInstanceDataAutoConfiguration.class)
public class MainSpringBootApplication {

  public static void main(String[] args) {
    SpringApplication.run(MainSpringBootApplication.class);
  }

}
Twinkie
  • 86
  • 1
  • 5