PAYATHON 2026

Spring Boot/OpenTelemetry 跨微服务统一 operation_Id

数据小陈

operation_Id 在 Spring Boot 和 Azure App Insights 里都不是那种可以直接“强制设置”的普通字段。Application Insights 的 operation_Id 基本对应 OpenTelemetry/W3C Trace Context 里的 traceId。要让两边的链路对上,靠的是传播:bf 的客户端 span 把 traceparent 传给 talent,talent 的入站请求沿用同一个 traceId。传播成功之后,两个 App Insights 资源里的 telemetry 就会共享同一个 operation_Id,cloud_RoleName 只负责区分服务名。

先检查客户端是否自动注入 traceparent

最常见的失败原因,是 bf 发请求时没走 OpenTelemetry 已经插桩的 HTTP 客户端。直接 new RestTemplate()、自己创建 HttpClient、自定义 Feign Client,或者在线程池里把 Context 弄丢了,出站请求都可能不带 traceparent。

优先用 Spring Boot 自动配置的客户端:

@Configuration
public class HttpClientConfig {

    @Bean
    RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    WebClient webClient(WebClient.Builder builder) {
        return builder.build();
    }
}

如果用的是 Feign、Apache HttpClient、OkHttp 或自定义 ClientHttpRequestFactory,先确认这个客户端在 OpenTelemetry 的自动插桩范围内;不在的话,需要显式开启观测/插桩,或者手动传播上下文。服务间调用的主通道不要用未插桩的客户端。

检查 talent 是否收到并提取 traceparent

在 talent 侧临时加一个过滤器,看入站请求头里有没有 traceparent:

@Component
public class TraceparentLoggingFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
            throws ServletException, IOException {
        String traceparent = request.getHeader("traceparent");
        // 临时日志:确认 traceparent 是否到达 talent
        filterChain.doFilter(request, response);
    }
}

如果 bf 确实发出了 traceparent,talent 却收不到,就去查 Nginx、Ingress、API Gateway、Service Mesh 这些中间层有没有过滤或改写 traceparent、tracestate。这两个头不能被删除或改写。

配置上容易出的两个问题:SDK 被关闭、service.name 写错

你给的配置里,application.yaml 写了 otel.sdk.disabled: true,application-dev.yaml 又覆盖成 false。这时要确认 bf 和 talent 都激活了 dev profile。talent 要是没激活,SDK 就是关着的,自然不会有同链路的 span。

另外,application-dev.yaml 里如果两个服务都把 service.name 写死成 bf,cloud_RoleName 就会显示错。让每个服务用各自的 spring.application.name:

otel:
  sdk:
    disabled: false
  resource:
    attributes:
      deployment.environment: dev
      service.name: ${spring.application.name}
      service.namespace: rh
  exporter:
    otlp:
      protocol: http/protobuf
      traces:
        endpoint: ${OTEL_TRACES_ENDPOINT}
      metrics:
        endpoint: ${OTEL_METRICS_ENDPOINT}
      logs:
        endpoint: ${OTEL_LOGS_ENDPOINT}

如果继续用 spring-cloud-azure-starter-monitor,要注意它可能已经带了 Azure Monitor 的 OpenTelemetry 自动配置。再显式导入 OTel BOM 和 OTLP exporter 时,确认没有重复初始化多个 SDK/exporter。重复初始化会让不同组件拿到不同的 Context,表现出来就是链路断掉。建议只保留一种主接入方式,或者至少确认它们不会互相覆盖。

异步调用要传播 Context

bf 调 talent 之前如果经过 @Async、CompletableFuture、自定义线程池、Reactor 之类的异步逻辑,要保证 OpenTelemetry Context 被传到执行线程。否则出站请求拿不到当前 span,也就带不出正确的 traceparent。这类问题改 App Insights 的查询语句没用,得让调用线程带上 Context。

查询时怎么判断成功

不要手动写 operation_Id。正确的结果是:bf 的客户端 span、talent 的 server span、talent 的 requests/dependencies 在同一个 trace 下共享 operation_Id,并且 cloud_RoleName 分别是 bf 和 talent。跨 Application Insights 资源查询时,union 和 app() 要指向正确的资源;同一个 operation_Id 能在两边同时查到,前提还是 trace context 传播成功。

简单说:bf 用被 OpenTelemetry 插桩的 HTTP 客户端,traceparent 不被中间层去掉,talent 能正常提取上下文,两个服务都启用 SDK。这几步做到,operation_Id 自然就一致了,不需要在 Spring Boot 里手动赋值。

备注:内容仅供参考。