简体   繁体   English

Spring 云侦探添加标签

[英]Spring cloud sleuth adding tag

I'm trying to implement distributed tracing in my kotlin app using spring cloud sleuth.我正在尝试使用 spring 云侦探在我的 kotlin 应用程序中实现分布式跟踪。 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.现在我可以跟踪我的日志,但我想向 span 添加一些额外的数据。 Let's say I want to add info about user and be able to see it in datadog.假设我想添加有关用户的信息并能够在 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.我正在将 json 格式的日志发送到 datadog,但我无法在此处添加标签。 (traceId and spanId are injected). (traceId 和 spanId 被注入)。 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>

gradle: gradle:

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()
}

example log:示例日志:

{"@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"}

shouldn't be that 'user' injected into MDC and then into logs?不应该是那个'用户'注入MDC然后注入日志吗?

TL;DR TL;博士

spring.sleuth.baggage.correlation-fields automatically sets baggage values to Slf4j's MDC so you only need to set the baggage field. spring.sleuth.baggage.correlation-fields自动将行李值设置为 Slf4j 的 MDC,因此您只需要设置行李字段。

Longer version with background加长版带背景

I suppose you use Sleuth out of the box (uses Brave):我想你使用 Sleuth 开箱即用(使用 Brave):

  1. Fist get familiar with tag , baggage and their differences ;熟悉标签行李及其区别 you can also read about them in Brave docs您还可以在Brave 文档中了解它们
  2. Check Brave's ScopeDecorator , CorrelationScopeDecorator and MDCScopeDecorator检查 Brave 的ScopeDecoratorCorrelationScopeDecoratorMDCScopeDecorator

The spring.sleuth.baggage.correlation-fields property automatically sets baggage values to Slf4j's MDC so you only need to set the baggage field. spring.sleuth.baggage.correlation-fields属性自动将行李值设置为 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 :此外,使用MDCScopeDecorator ,您可以以编程方式将行李值设置为 Slf4j 的 MDC,您可以在Sleuth 文档中查看如何执行此操作:

// 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM