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

Spring Boot+OpenTelemetry接入Jaeger仅显示一级Trace问题

问题诊断与解决方案

核心问题分析

当前配置仅启用了基础链路追踪桥接,但缺少OpenTelemetry对Spring组件(Service、Repository、JDBC等)的自动 instrumentation支持,同时自定义配置干扰了默认的上下文传播和Span创建逻辑,导致仅能采集到Controller层的一级Span。


步骤1:补充必要依赖

在build.gradle中添加OpenTelemetry Spring Boot Starter,它会自动引入所有组件的instrumentation包,实现多层级链路追踪:

dependencies {
    // 保留原有依赖,新增以下内容
    implementation 'io.opentelemetry:opentelemetry-spring-boot-starter'
    // 若需细粒度控制,也可单独引入特定组件的instrumentation(可选):
    // implementation 'io.opentelemetry.instrumentation:opentelemetry-spring-webmvc-5.3'
    // implementation 'io.opentelemetry.instrumentation:opentelemetry-spring-data-jpa-2.2'
}

步骤2:移除自定义OpenTelemetry配置类

你的OpenTelemetryConfiguration类会覆盖Spring Boot自动配置的默认行为,尤其是JaegerPropagator的设置可能与W3C Trace Context规范冲突,导致Span上下文无法正确传递。直接删除该配置类,Spring Boot会自动处理OTLP Exporter和上下文传播逻辑。

步骤3:调整application配置

修改application.yml,完善OpenTelemetry相关配置,确保Span正确上报到Jaeger:

spring:
  application:
    name: ms-spring-product-api
  # 保留原有数据源、JPA配置

logging:
  level:
    org.springframework.web: TRACE
    io.opentelemetry: TRACE

management:
  tracing:
    sampling:
      probability: 1.0

# OpenTelemetry 配置
otel:
  service:
    name: ${spring.application.name}
  exporter:
    otlp:
      trace:
        endpoint: http://localhost:4317
        protocol: grpc

步骤4:验证Span自动创建逻辑

确保你的Service、Repository类带有@Service、@Repository注解(属于Spring管理的Bean),OpenTelemetry会自动为这些Bean的公共方法创建子Span。若存在异步方法,需确保通过Context.current()正确传递链路上下文。


验证方法

重启应用后发起请求,访问Jaeger UI(http://localhost:16686),选择你的服务名称查看Trace详情,此时应能看到controller -> service -> repository -> jdbc的完整链路层级。

内容的提问来源于stack exchange,提问作者Cícero Ednilson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 17:52:23