从Spring Cloud探测切换到测微仪跟踪后勇敢的行李不工作

z9smfwbn  于 2023-05-27  发布在  Spring
关注(0)|答案(1)|浏览(181)

我正在将我的spring项目更新到较新的版本,更新后我收到以下消息:
由于以下原因,您的项目设置与我们的要求不兼容:Spring Cloud Sleuth与此Spring Cloud发布系列不兼容,操作:考虑应用以下操作:从Spring Cloud Sleuth迁移到Micrometer Tracing。
因此,我删除了sleuth并添加了micrometer-tracing和micrometer-tracing-bridge-brave依赖项,并切换到以下配置:

management:
  tracing:
    enabled: true
    baggage:
      enabled: true
      correlation:
        enabled: true
        fields: USER_ID
      remote-fields: USER_ID

现在我的BaggageField不像以前那样被添加到MDC中了。我已经把它缩小到tracing.currentTraceContext().get()在调用BaggageField.updateValue()时返回null,但我不知道为什么会这样。

@Nullable static TraceContext currentTraceContext() {
    Tracing tracing = Tracing.current();
    return tracing != null ? tracing.currentTraceContext().get() : null;
}

下面是我创建BaggageField bean的代码:

import brave.baggage.BaggageField;
import brave.baggage.CorrelationScopeConfig;
import brave.context.slf4j.MDCScopeDecorator;
import brave.propagation.CurrentTraceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BaggageConfig {

    public static final String USER_ID_KEY = "USER_ID";

    @Bean
    BaggageField userIdField() {
        return BaggageField.create(USER_ID_KEY);
    }
    

    @Bean
    public CurrentTraceContext.ScopeDecorator mdcScopeDecorator(BaggageField userIdField) {
        return MDCScopeDecorator.newBuilder()
                .clear()
                .add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(userIdField).flushOnUpdate().build())
                .build();
    }
}

当我尝试更新BaggageField中的值时:

private final BaggageField userIdField;

并更新:

userIdField.updateValue(requestMetadataContext.getUserId());

版本:

org.springframework.boot:3.0.6
org.springframework.cloud:spring-cloud-starter-bootstrap:4.0.2
org.springframework.cloud:spring-cloud-starter-consul-all:4.0.2
io.micrometer:micrometer-tracing:1.1.0
io.micrometer:micrometer-tracing-bridge-brave:1.1.0

编辑:小应用程序复制Baggage Example

ua4mk5z4

ua4mk5z41#

我通过自动装配一个Tracer并在Tracer内部添加行李来完成这项工作。

@Autowired
private final Tracer tracer;

tracer.createBaggageInScope("USER_ID", "USER_ID_VALUE");

相关问题