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

Java Brave升级后Tracing构建器spanReporter方法弃用,求替代方案

新版Brave等效写法如下

原有spanReporter方法已弃用,需将AsyncReporter包装为ZipkinSpanHandler后通过addSpanHandler方法注入,完整代码如下:

import brave.Tracing;
import brave.handler.SpanHandler;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import zipkin2.reporter.okhttp3.OkHttpSender;

// Sender构造逻辑和原有一致
OkHttpSender sender = OkHttpSender.newBuilder()
        .endpoint("https://zipkin-instance.com:9411/api/v2/spans")
        .build();
// 注意AsyncReporter的泛型调整为zipkin2.Span
AsyncReporter<Span> reporter = AsyncReporter.builder(sender).build();
// 包装为ZipkinSpanHandler
SpanHandler zipkinSpanHandler = ZipkinSpanHandler.create(reporter);
// 构建Tracing实例,用addSpanHandler替代弃用方法
Tracing tracing = Tracing.newBuilder()
        .localServiceName("service")
        .addSpanHandler(zipkinSpanHandler)
        .build();

注意事项

  • 你现有依赖已经包含所需类,无需额外引入依赖,zipkin-sender-okhttp3会传递依赖zipkin-reporter-brave模块,提供ZipkinSpanHandler实现
  • 支持添加多个SpanHandler实现,可同时实现多链路数据上报、自定义span处理等需求,灵活性优于旧版单spanReporter设计
  • 如果原有代码中AsyncReporter的泛型为brave.Span,必须调整为zipkin2.Span,否则会出现类型不匹配报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 04:00:01