You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启用OpenTelemetry(OTEL)追踪后IBM MQ消息头部出现乱码

解决OpenTelemetry与IBM MQ集成时消息出现特殊字符的问题

一、禁用JMS队列的OTEL追踪传播

1. 全局禁用JMS Instrumentation

在Spring Boot配置文件中直接关闭OpenTelemetry对JMS的自动追踪:

  • application.properties:
otel.instrumentation.jms.enabled=false
  • application.yml:
otel:
  instrumentation:
    jms:
      enabled: false

2. 移除特定JMS模板的追踪拦截器

如果不想全局禁用,仅针对目标MQ队列的JmsTemplate移除OTEL追踪拦截器:

import io.opentelemetry.instrumentation.jms.JmsTracing;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class CustomJmsSetup {
    public CustomJmsSetup(JmsTemplate mqJmsTemplate, JmsTracing jmsTracing) {
        // 移除OTEL注入的TracingInterceptor
        mqJmsTemplate.getInterceptors().removeIf(interceptor -> 
            interceptor.getClass().getSimpleName().equals("TracingInterceptor")
        );
    }
}

3. 全局禁用追踪上下文传播(谨慎使用)

如果不需要任何场景的追踪上下文传播,可直接关闭传播器:

otel.propagators=none

注意:此配置会影响HTTP等其他链路的追踪传播,仅在确认不需要全局传播时使用。

二、其他解决建议

  • 清理消息头中的追踪字段:发送MQ消息前手动移除OTEL注入的traceparent、tracestate等头字段:
import jakarta.jms.Message;
import org.springframework.jms.core.MessageCreator;

jmsTemplate.send("target-queue", session -> {
    Message msg = session.createTextMessage("业务消息内容");
    msg.removeProperty("traceparent");
    msg.removeProperty("tracestate");
    return msg;
});
  • 升级OTEL JMS依赖版本:旧版本的OTEL JMS instrumentation可能存在编码兼容问题,尝试升级到最新稳定版(如io.opentelemetry.instrumentation:opentelemetry-jms-2.0,需匹配你的JMS版本)。
  • 检查消费者端编码逻辑:确认MQ消费者是否能正确处理新增的追踪头字段,若消费者无法解析这些字段导致乱码,可调整消费者的消息解析逻辑,忽略未知头字段。
  • 重置MQ连接工厂拦截器:检查OpenTelemetry是否自动修改了MQ连接工厂的拦截器链,手动重置连接工厂的拦截器列表,仅保留业务所需的拦截器。

内容的提问来源于stack exchange,提问作者Ramakrishna Patil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:21:01