BaggageField UpdateValue方法在Junit测试中返回False

发布于 2025-01-22 01:44:43 字数 3026 浏览 4 评论 0 原文

编辑:一个问题是 tracing.current()是无效的。我用新的 @beforeach 而不是旧的 @beforetestmethod

Tracer tracer;

@BeforeEach
void init() {
    tracer = Tracing.newBuilder().build().tracer();
    TraceContext ctx = TraceContext.newBuilder().traceId(17).spanId(17).build();
    Span span = tracer.toSpan(ctx);
    tracer.withSpanInScope(span);
}

然而,updateValue仍然无法使用,因为上下文中没有附加功能,所以没有什么可更新的...


按照 BaggageField 。所有对象均不为空,而是 updateValue 返回false,并且测试失败了 baggagetest.baggagetest:40预期:< hello>但是:< null> 。如前所述,我不知道为什么 updateValue 方法不起作用。

import static org.junit.jupiter.api.Assertions.assertEquals;

import brave.baggage.BaggageField;
import brave.baggage.CorrelationScopeConfig;
import brave.context.slf4j.MDCScopeDecorator;
import brave.propagation.CurrentTraceContext;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.ScopedSpan;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.event.annotation.BeforeTestMethod;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Slf4j
@ExtendWith(SpringExtension.class)
@ContextConfiguration
public class BaggageTest {

    @Autowired
    ApplicationContext context;
    
    @BeforeTestMethod
    void init() {
        Tracer tracer = context.getBean(Tracer.class);
        ScopedSpan span = tracer.startScopedSpan("test");
    }

    @Test
    void baggageTest() {
        BaggageField fooBar = context.getBean("fooBar", BaggageField.class);
        log.info("updateValue {}", fooBar.updateValue("hello"));
        assertEquals("hello", fooBar.getValue());
    }

    @Configuration
    static class Config {

        private BaggageField findOrCreate(String name) {
            BaggageField field = BaggageField.getByName(name);
            if (field == null) {
                field = BaggageField.create(name);
            }
            return field;
        }

        @Bean("fooBar")
        BaggageField fooBar() {
            return findOrCreate("fooBar");
        }

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

EDIT: One problem was that Tracing.current() was null. I fixed this with the new @BeforeEach instead of the old @BeforeTestMethod:

Tracer tracer;

@BeforeEach
void init() {
    tracer = Tracing.newBuilder().build().tracer();
    TraceContext ctx = TraceContext.newBuilder().traceId(17).spanId(17).build();
    Span span = tracer.toSpan(ctx);
    tracer.withSpanInScope(span);
}

Yet, updateValue still doesn't work as there are no extras in the context, so nothing to update...


Following the ideas like in those answers, I'm trying to use BaggageFields in one of my tests. All the objects are not null, but updateValue returns false and the test fails with BaggageTest.baggageTest:40 expected: <hello> but was: <null>. As said, I have no idea why the updateValue method is not working.

import static org.junit.jupiter.api.Assertions.assertEquals;

import brave.baggage.BaggageField;
import brave.baggage.CorrelationScopeConfig;
import brave.context.slf4j.MDCScopeDecorator;
import brave.propagation.CurrentTraceContext;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.ScopedSpan;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.event.annotation.BeforeTestMethod;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Slf4j
@ExtendWith(SpringExtension.class)
@ContextConfiguration
public class BaggageTest {

    @Autowired
    ApplicationContext context;
    
    @BeforeTestMethod
    void init() {
        Tracer tracer = context.getBean(Tracer.class);
        ScopedSpan span = tracer.startScopedSpan("test");
    }

    @Test
    void baggageTest() {
        BaggageField fooBar = context.getBean("fooBar", BaggageField.class);
        log.info("updateValue {}", fooBar.updateValue("hello"));
        assertEquals("hello", fooBar.getValue());
    }

    @Configuration
    static class Config {

        private BaggageField findOrCreate(String name) {
            BaggageField field = BaggageField.getByName(name);
            if (field == null) {
                field = BaggageField.create(name);
            }
            return field;
        }

        @Bean("fooBar")
        BaggageField fooBar() {
            return findOrCreate("fooBar");
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2025-01-29 01:44:43

我的解决方案是使用以下片段手动构建 traceContext ,请注意 baggagefields is brave。 .baggage.baggagefields

@Autowired
BaggageField field;

Tracer tracer;

@BeforeEach
void init() {
    tracer = Tracing.newBuilder().build().tracer();
    ArrayList<BaggageField> list = new ArrayList<>();
    list.add(field);
    TraceContext ctx = TraceContext.newBuilder()
        .addExtra(BaggageFields.newFactory(list,2).create())
        .traceId(17).spanId(17).build();
    Span span = tracer.toSpan(ctx);
    tracer.withSpanInScope(span);
}

My solution is to manually build the TraceContext, using the following snippet, being careful that BaggageFields is brave.internal.baggage.BaggageFields.

@Autowired
BaggageField field;

Tracer tracer;

@BeforeEach
void init() {
    tracer = Tracing.newBuilder().build().tracer();
    ArrayList<BaggageField> list = new ArrayList<>();
    list.add(field);
    TraceContext ctx = TraceContext.newBuilder()
        .addExtra(BaggageFields.newFactory(list,2).create())
        .traceId(17).spanId(17).build();
    Span span = tracer.toSpan(ctx);
    tracer.withSpanInScope(span);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文