Java/Spring boot/ActiveMq/Sleuth Consumer app do not have traceId/spanId in logs

60 views Asked by At

I have 2 Spring boot project in java, one for Producing messages(Producer.app) and the second one for Consuming messages(Consumer.app)

Plese help me to understand how to get traceId/spanId in the logs for ActiveMq Consumer app?

My task is to have traceId/spanId in logs for both application, The max requirement of course that traceId from Consumer thread will be the same as for Producer thread with the same msg(But not critical so far). For now I need to make traceId/spanId working for Consumer app at least.

Everything working fine for Producer app, I trigger Rest endpoint from Postman and send the msg to the ActiveMq (all logs for this thread will have traceId/spanId)

But when Cosumer app consume the msg from ActiveMq, there no traceId/spanId in logs, and tracer.currentSpan() is null.

application file for Consumer and Producer apps has the same configuration:

logging:
    level:
        root: info
server:
    port: 7071
spring:
    application:
        name: consumer-app
    sleuth:
        sampler:
            probability: 1.0
        messaging:
            jms:
                enabled: false
    activemq:
        broker-url: tcp://127.0.0.1:61616
        user: ****
        password: *******
        packages:
            trust-all: true
        pool:
            enabled: true
            max-connections: 1000

I built 2 applciations, one for Producer and one for Consumer. Configured Sleuth for both. I am expecting that both apps will have traceId/spanId in the logs, but only Producer has it.

I found similar issue: https://stackoverflow.com/questions/68111806/sleuth-traceid-and-spanid-not-logged-in-activemq-listener

My Configuration file:

@Configuration
public class JmsConfig {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String brokerUser;

    @Value("${spring.activemq.password}")
    private String brokerPassword;

    @Bean
    public PooledConnectionFactory pooledConnectionFactory(ActiveMQConnectionFactory activeMQConnectionFactory) {
        PooledConnectionFactory factory = new PooledConnectionFactory();
        factory.setConnectionFactory(activeMQConnectionFactory);
        factory.setMaxConnections(60);
        factory.setReconnectOnException(true);
        factory.setExpiryTimeout(0);
        factory.setCreateConnectionOnStartup(true);
        return factory;
    }
    @Bean
    @Primary
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(brokerUrl);
        factory.setPassword(brokerPassword);
        factory.setUserName(brokerUser);
        return factory;
    }

    @Bean
    public JmsTemplate myJmsTemplate(PooledConnectionFactory pooledConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(pooledConnectionFactory);
        return jmsTemplate;
    }
}

I need to keep sleuth.messaging.jms.enabled = false, because we use ActiveMQConnectionFactory in confuguration, and we cannot convert it to ConnectionFactory as suggested in the comment from the link.

Is there a way to export the traceId/spanId with the msg from Producer app maybe?

1

There are 1 answers

0
Oleks Oleks On

I managed to start the consumer application with spring.sleuth.messaging.jms.enabled = true, if I used such configuration:

import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

@Configuration
public class JmsConfig {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String brokerUser;

    @Value("${spring.activemq.password}")
    private String brokerPassword;

    @Bean
    public ConnectionFactory pooledConnectionFactory(ConnectionFactory activeMQConnectionFactory) {
        PooledConnectionFactory factory = new PooledConnectionFactory();
        factory.setConnectionFactory(activeMQConnectionFactory);
        factory.setMaxConnections(60);
        factory.setReconnectOnException(true);
        factory.setExpiryTimeout(0);
        factory.setCreateConnectionOnStartup(true);
        return factory;
    }
    @Bean
    @Primary
    public ConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(brokerUrl);
        factory.setPassword(brokerPassword);
        factory.setUserName(brokerUser);
        return factory;
    }

    @Bean
    public JmsTemplate myJmsTemplate(ConnectionFactory pooledConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(pooledConnectionFactory);
        return jmsTemplate;
    }
}

but traceId and span id remain empty. Does sleuth support ActiveMq trace/span id tracking?