Updated on 2024-04-08 GMT+08:00

Enabling Message Tracing

Scenario

To query message traces, you must first enable message tracing on clients.

The following procedures describe how to enable message tracing in Java and Go.

Prerequisites

  • Transactional message tracing is supported only if the producer Java client is v4.9.0 or later. If your client is earlier than v4.9.0, upgrade it to a later version.
  • For RocketMQ instances with SSL enabled, message tracing is supported only when the producer and consumer Java clients are v4.9.2 or later. If the version does not meet the requirement, upgrade it first.

Procedure (Java)

Do as follows to enable message tracing on clients:

  • Enabling message tracing on a producer client (tracing messages other than transactional messages)

    Set enableMsgTrace of the constructor to true. For example:

    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName", true);
  • Enabling message tracing on a producer client (tracing transactional messages)

    Set enableMsgTrace of the constructor to true. For example:

    TransactionMQProducer producer = new TransactionMQProducer(null, "ProducerGroupName", null, true, null);
  • Enabling message tracing on a consumer

    Set enableMsgTrace of the constructor to true. For example:

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName", true);

Procedure (Go)

Do as follows to enable message tracing on clients:

  1. Run the following command to check whether Go has been installed:

    go version

    If the following information is displayed, Go has been installed.

    [root@ecs-test sarama]# go version
    go version go1.16.5 linux/amd64

    If Go is not installed, download and install it.

  2. Create a go.mod file and add the following code to it to add the dependency:

    module rocketmq-example-go
    
    go 1.13
    
    require (
    	github.com/apache/rocketmq-client-go/v2 v2.1.0
    )

  3. Enable message tracing on the producer. Replace the information in bold with the actual values.

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"time"
    
    	"github.com/apache/rocketmq-client-go/v2"
    	"github.com/apache/rocketmq-client-go/v2/primitive"
    	"github.com/apache/rocketmq-client-go/v2/producer"
    )
    
    func main() {
    	namesrvs := []string{"192.168.0.1:8100"}
    	traceCfg := &primitive.TraceConfig{
    		Access:   primitive.Local,
    		Resolver: primitive.NewPassthroughResolver(namesrvs),
    	}
    
    	p, _ := rocketmq.NewProducer(
    		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"192.168.0.1:8100"})),
    		producer.WithRetry(2),
    		producer.WithTrace(traceCfg))
    	err := p.Start()
    	if err != nil {
    		fmt.Printf("start producer error: %s", err.Error())
    		os.Exit(1)
    	}
    	res, err := p.SendSync(context.Background(), primitive.NewMessage("topic1",
    		[]byte("Hello RocketMQ Go Client!")))
    
    	if err != nil {
    		fmt.Printf("send message error: %s\n", err)
    	} else {
    		fmt.Printf("send message success: result=%s\n", res.String())
    	}
    
    	time.Sleep(10 * time.Second)
    
    	err = p.Shutdown()
    	if err != nil {
    		fmt.Printf("shutdown producer error: %s", err.Error())
    	}
    }

  4. Enable message tracing on the consumer. Replace the information in bold with the actual values.

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"time"
    
    	"github.com/apache/rocketmq-client-go/v2"
    	"github.com/apache/rocketmq-client-go/v2/consumer"
    	"github.com/apache/rocketmq-client-go/v2/primitive"
    )
    
    func main() {
    	namesrvs := []string{"192.168.0.1:8100"}
    	traceCfg := &primitive.TraceConfig{
    		Access:   primitive.Local,
    		Resolver: primitive.NewPassthroughResolver(namesrvs),
    	}
    
    	c, _ := rocketmq.NewPushConsumer(
    		consumer.WithGroupName("testGroup"),
    		consumer.WithNsResolver(primitive.NewPassthroughResolver([]string{"192.168.0.1:8100"})),
    		consumer.WithTrace(traceCfg),
    	)
    	err := c.Subscribe("TopicTest", consumer.MessageSelector{}, func(ctx context.Context,
    		msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
    		fmt.Printf("subscribe callback: %v \n", msgs)
    		return consumer.ConsumeSuccess, nil
    	})
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	// Note: start after subscribe
    	err = c.Start()
    	if err != nil {
    		fmt.Println(err.Error())
    		os.Exit(-1)
    
    	}
    	time.Sleep(time.Hour)
    	err = c.Shutdown()
    	if err != nil {
    		fmt.Printf("shutdown Consumer error: %s", err.Error())
    	}
    }