OpenTelemetry集成Spring Boot Reactive时Span异常问题咨询
问题解答
1. 修复反应式应用Span层级关联异常
核心原因
反应式场景下,追踪上下文依赖Reactor的Context传递,若Micrometer Tracing与OTel桥接的上下文传播配置错误,或WebFlux埋点与Micrometer默认追踪逻辑冲突,会导致Span无法正确关联。
具体解决方案
- 统一依赖版本:确保Micrometer与OTel依赖版本兼容,
micrometer-tracing-bridge-otel:1.2.1对应OTel版本建议为1.28.x,统一配置如下:ext { opentelemetryVersion = "1.28.0" } runtimeOnly 'io.micrometer:micrometer-registry-otlp:1.12.1' implementation 'io.micrometer:micrometer-tracing-bridge-otel:1.2.1' implementation "io.opentelemetry:opentelemetry-exporter-logging:${opentelemetryVersion}" implementation "io.opentelemetry:opentelemetry-exporter-otlp:${opentelemetryVersion}" runtimeOnly "io.opentelemetry:opentelemetry-exporter-otlp-common:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-resources:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-mongo-3.1:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-spring-web-3.1:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-spring-webflux-5.3:${opentelemetryVersion}" implementation "io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:${opentelemetryVersion}" - 开启Reactor上下文自动传播:添加配置类确保追踪上下文在Reactor流中传递:
import io.micrometer.tracing.ReactorContextAccessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.core.publisher.Hooks; @Configuration public class TracingContextConfig { static { Hooks.enableAutomaticContextPropagation(); } @Bean public ReactorContextAccessor reactorContextAccessor() { return new ReactorContextAccessor(); } } - 禁用Micrometer默认Web追踪:避免与OTel埋点冲突,在
application.yml中添加:management: tracing: web: flux: enabled: false - 验证反应式Mongo埋点:确认使用
ReactiveMongoTemplate等反应式客户端,opentelemetry-mongo-3.1会自动为其生成Span并关联上下文。
2. 移除org.springframework.boot生成的重复Span
生成来源
该Span是Spring Boot Actuator内置的Micrometer Web追踪逻辑生成的,由WebMvcTracingFilter(阻塞式)或WebFluxTracingFilter(反应式)触发,与OTel官方埋点库的Span内容重复。
禁用方法
在application.yml中关闭Micrometer的Web自动追踪功能:
management: tracing: web: enabled: false mvc: enabled: false flux: enabled: false
配置后将仅保留OTel官方埋点生成的Span,消除重复。
内容的提问来源于stack exchange,提问作者vkourt
相关产品推荐
相关产品推荐

