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

Spring Boot 2.7迁3.1:Sleuth转Micrometer Tracing无local fields等效配置?

在Micrometer Tracing中实现Sleuth sleuth.baggage.local-fields 的等效功能

Micrometer Tracing没有直接对应Sleuth sleuth.baggage.local-fields的配置属性,但可以通过自定义Bean的方式实现仅本地生效、不跨进程传播的baggage字段,具体方案如下:

方案一:单个本地Baggage字段定义

通过创建BaggageField Bean来声明本地字段,同时确保该字段不被加入远程传播列表:

import io.micrometer.tracing.BaggageField;
import io.micrometer.tracing.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LocalBaggageConfiguration {

    // 定义仅本地生效的baggage字段
    @Bean
    public BaggageField localRequestIdField() {
        return BaggageField.create("local-request-id");
    }

    // 可选:将字段关联到MDC,方便日志输出
    @Bean
    public Tracer customTracer(Tracer.Builder builder, BaggageField localRequestIdField) {
        return builder.addBaggageField(localRequestIdField)
                .withBaggageCorrelation(localRequestIdField)
                .build();
    }
}

方案二:批量配置本地Baggage字段

如果需要定义多个本地字段,使用BaggageManagerCustomizer统一处理:

import io.micrometer.tracing.baggage.BaggageManagerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LocalBaggageConfiguration {

    @Bean
    public BaggageManagerCustomizer localBaggageCustomizer() {
        return baggageManager -> {
            baggageManager.createBaggageField("local-user-id");
            baggageManager.createBaggageField("local-session-id");
        };
    }
}

核心注意事项

  • 不要将这些本地字段配置到management.tracing.baggage.remote-fields中,这样就能保证它们不会随请求跨进程传播,完全等价于Sleuth的local-fields。
  • 若需要让本地字段出现在日志MDC中,可将字段名加入management.tracing.baggage.correlation.fields配置,或者通过代码手动关联。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:12:10