gpt4 book ai didi

java - 如何使用brave(java)获取rabbitmq应用程序的currentSpan?

转载 作者:行者123 更新时间:2023-11-30 02:29:19 26 4
gpt4 key购买 nike

我的应用程序是一个基于 spring-rabbitmq 的应用程序(既不是 spring-cloud 也不是 spring-boot),从一个队列接收请求并将响应发送到另一个队列。

我想使用brave通过在发送消息之前注入(inject)Zipkin header 并在接收消息后立即提取Zipkin header 来跟踪系统。

问题是在以下场景的第3步中,如何在发送消息之前获取span1?

场景:

step1, app -> mq (new created span1, root span, client)
step2, mq -> app (receive span1, server)
step3, app -> mq (**new created span2, child span of span1, client**)
step4, mq -> app (receive span2, server)

发送消息之前的代码片段:

     try{
Span currentSpan = tracer.currentSpan();
Span newSpan = null;
if(currentSpan == null){
newSpan = tracer.newTrace();
}else{
newSpan = tracer.newChild(currentSpan.context());
}

Map<String, String> strHeaders = new HashMap<String, String>();
tracing.propagation().injector(Map<String, String>::put).inject(newSpan.context(),strHeaders);
messageProperties.setHeader("zipkin.brave.tracing.headers", strHeaders);
newSpan.name("send").kind(Kind.CLIENT).start().flush();
}catch(Exception e){
logger.warn("zipkin problem", e);
}

在上面的代码中,Span currentSpan = tracer.currentSpan();currentSpan始终为null

收到消息后的代码片段:

    try{
Map<String, Object> msgHeaders = messageProperties.getHeaders();
Object headersObj = msgHeaders.get("zipkin.brave.tracing.headers");
Map<String, String> strHeaders = null;
if(headersObj != null){
strHeaders = (HashMap<String, String>)headersObj;
}
TraceContextOrSamplingFlags result = tracing.propagation().extractor(Map<String, String>::get).extract(strHeaders);

if(result.context() != null){
Span clientSpan = tracer.joinSpan(result.context());
clientSpan.name("receive").kind(Kind.SERVER).start().flush();
}
}catch(Exception e){
logger.warn("zipkin problem", e);
}

Brave配置代码:

@Configuration
public class BraveSpringConfiguration {
private static final Logger logger = LoggerFactory.getLogger(BraveSpringConfiguration.class);

/** Configuration for how to send spans to Zipkin */
@Bean
public Sender sender() {
logger.debug("okhttpsender");
return OkHttpSender.create("http://127.0.0.1:9411/api/v1/spans");
}

/** Configuration for how to buffer spans into messages for Zipkin */
@Bean
public Reporter<Span> reporter() {
logger.debug("asyncreporter");
return AsyncReporter.builder(sender()).build();
}

/** Controls aspects of tracing such as the name that shows up in the UI */
@Bean
public Tracing tracing() {
logger.debug("one tracing");
return Tracing.newBuilder()
.localServiceName("spring-rabbitmq-brave")
//// log4j2, import brave.context.log4j2.ThreadContextCurrentTraceContext;
//.currentTraceContext(ThreadContextCurrentTraceContext.create()) // puts trace IDs into logs
.currentTraceContext(MDCCurrentTraceContext.create()) // puts trace IDs into logs
.sampler(Sampler.ALWAYS_SAMPLE) // always sampler
.reporter(reporter())
.build();
}

/** Controls aspects of tracing such as the name that shows up in the UI */
@Bean
public Tracer tracer() {
logger.debug("one tracer");
return tracing().tracer();
}
}

以下是我的引用资料:

  1. https://github.com/openzipkin/brave/tree/master/brave#one-way-tracing

  2. https://github.com/openzipkin/brave/blob/master/brave/src/test/java/brave/features/async/OneWaySpanTest.java

  3. https://gist.github.com/adriancole/76d94054b77e3be338bd75424ca8ba30

最佳答案

问题已解决。 tracer.withSpanInScope(clientSpan) 可以完成这项工作。

请注意,在发送消息之前尚未调用withSpanInScope(...)

关于java - 如何使用brave(java)获取rabbitmq应用程序的currentSpan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com