Spring Cloud Sleuth添加标签 [英] Spring cloud sleuth adding tag

查看:270
本文介绍了Spring Cloud Sleuth添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Cloud Sleuth在我的Kotlin应用程序中实现分布式跟踪.我正在将这些数据发送到数据狗.现在,我可以跟踪日志了,但是我想向跨度添加一些额外的数据.假设我想添加有关用户的信息,并能够在datadog中看到它.我说跨度标签对它有好处吗?我正在将json格式的日志发送到datadog,但无法在此处添加标签.(将注入traceId和spanId).登录配置:

I'm trying to implement distributed tracing in my kotlin app using spring cloud sleuth. I'm sending those data to the datadog. Now I'm able to trace my logs but I want to add some extra data to spans. Let's say I want to add info about user and be able to see it in datadog. Am I right that span tags are good for it? I'm sending the logs in json format to datadog but I cannot add tags here. (traceId and spanId are injected). Logback config:

        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp/>
                <version/>
                <message/>
                <loggerName/>
                <threadName/>
                <logLevel/>
                <logLevelValue/>
                <callerData/>
                <stackTrace/>
                <rootStackTraceElement/>
                <context/>
                <mdc/>
                <tags/>
                <logstashMarkers/>
                <arguments/>
            </providers>
        </encoder>

等级:

implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
implementation("org.springframework.cloud:spring-cloud-starter-vault-config")
implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("ch.qos.logback:logback-classic:1.2.3")
implementation("net.logstash.logback:logstash-logback-encoder:6.6")
implementation("org.zalando:logbook-spring-boot-starter:2.4.2")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("io.micrometer:micrometer-registry-datadog")
testImplementation("org.springframework.boot:spring-boot-starter-test")

并添加我正在尝试的标签

and to add the tag I'm trying

@NewSpan
fun newSpanTest() {
    tracer.currentSpan()!!.tag("user", "123")
    log.info("other span")
    otherTestService.sameSpanTest()
}

示例日志:

{"@timestamp":"2021-03-09T21:04:46.953+01:00","@version":"1","message":"other span","logger_name":"com.microservices.text.rpg.servicediscovery.TestService","thread_name":"http-nio-8761-exec-3","level":"INFO","level_value":20000,"caller_class_name":"com.microservices.text.rpg.servicediscovery.TestService","caller_method_name":"newSpanTest","caller_file_name":"TestService.kt","caller_line_number":25,"traceId":"e61fd165d7c84776","spanId":"5e61f9b51b51619b"}

那不是那个用户"注入到MDC中,然后注入到日志中吗?

shouldn't be that 'user' injected into MDC and then into logs?

推荐答案

TL; DR

spring.sleuth.baggage.correlation-fields 自动将行李值设置为Slf4j的MDC,因此您只需设置行李字段.

TL;DR

spring.sleuth.baggage.correlation-fields automatically sets baggage values to Slf4j’s MDC so you only need to set the baggage field.

我想您开箱即用Sleuth(使用Brave):

I suppose you use Sleuth out of the box (uses Brave):

  1. 拳头熟悉标签行李a>及其差异;您还可以在勇敢的文档
  2. 中阅读有关它们的信息.
  3. 检查Brave的 ScopeDecorator
  1. Fist get familiar with tag, baggage and their differences; you can also read about them in Brave docs
  2. Check Brave's ScopeDecorator, CorrelationScopeDecorator and MDCScopeDecorator

spring.sleuth.baggage.correlation-fields 属性会自动将行李值设置为Slf4j的MDC,因此您只需设置行李字段.

The spring.sleuth.baggage.correlation-fields property automatically sets baggage values to Slf4j’s MDC so you only need to set the baggage field.

还可以使用 MDCScopeDecorator ,以编程方式将行李值设置为Slf4j的MDC,您可以在

Also, using MDCScopeDecorator, you can set the baggage values to Slf4j’s MDC programmatically, you can see how to do it in Sleuth docs:

// configuration
@Bean
BaggageField countryCodeField() {
    return BaggageField.create("country-code");
}

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

// service
@Autowired
BaggageField countryCodeField;

countryCodeField.updateValue("new-value");

这篇关于Spring Cloud Sleuth添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆