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

Spring Cloud Sleuth传播Span Context:设置字段后获取值为null求助

Troubleshooting ExtraFieldPropagation Returning Null in Spring Cloud Sleuth 2.0.RC1/RC2

Let's work through the common issues that might be causing your ExtraFieldPropagation.get("foo") to return null, along with fixes for each scenario:

1. Missing Propagation Key Configuration

Spring Cloud Sleuth doesn't automatically propagate arbitrary extra fields—you have to explicitly declare which fields should be included in span propagation. Without this setup, the fields you set won't be attached to the span context, resulting in null when you try to retrieve them.

Add these configurations to your application.properties or application.yml:

Properties format:

spring.sleuth.propagation-keys=foo,UPPER_CASE

YAML format:

spring:
  sleuth:
    propagation-keys:
      - foo
      - UPPER_CASE

2. Retrieving Values Outside the Active Span Scope

Your code uses a try-with-resources block for Tracer.SpanInScope, which automatically closes the span context when the block exits. If you're calling ExtraFieldPropagation.get("foo") outside of this try block, the active span context is gone, so the method will return null.

Make sure your retrieval logic runs within the active span scope:

Span initialSpan = _tracer.nextSpan().name("span").start();
try( Tracer.SpanInScope ws = _tracer.withSpanInScope(initialSpan) ) {
    ExtraFieldPropagation.set("foo", "bar");
    ExtraFieldPropagation.set("UPPER_CASE", "someValue");
    
    // Retrieve values HERE, inside the active scope
    String fooValue = ExtraFieldPropagation.get("foo"); // This will return "bar"
}
// Retrieving here will return null—scope is already closed

If you need to access these fields outside the initial scope, you'll need to reactivate the span context (e.g., by passing the span instance and creating a new Tracer.SpanInScope around your retrieval code).

3. Verify Span Lifecycle

Double-check that your span is properly started and not prematurely ended. Your code correctly calls initialSpan.start(), but ensure no other logic is finishing the span (via initialSpan.end()) before you attempt to retrieve the extra fields.

4. Consider Version-Specific Bugs

Since you're using release candidate versions (2.0.RC1/RC2), there's a small chance of edge-case bugs. If the above steps don't resolve the issue, try upgrading to the latest stable 2.0.x release of Spring Cloud Sleuth—RC versions often receive bug fixes before the final GA launch.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:12