繁体   English   中英

Spring 云侦探添加标签

[英]Spring cloud sleuth adding tag

我正在尝试使用 spring 云侦探在我的 kotlin 应用程序中实现分布式跟踪。 我正在将这些数据发送到数据狗。 现在我可以跟踪我的日志,但我想向 span 添加一些额外的数据。 假设我想添加有关用户的信息并能够在 datadog 中看到它。 我对跨度标签有好处吗? 我正在将 json 格式的日志发送到 datadog,但我无法在此处添加标签。 (traceId 和 spanId 被注入)。 回退配置:

        <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:

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

并添加我正在尝试的标签

@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然后注入日志吗?

TL;博士

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

加长版带背景

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

  1. 熟悉标签行李及其区别 您还可以在Brave 文档中了解它们
  2. 检查 Brave 的ScopeDecoratorCorrelationScopeDecoratorMDCScopeDecorator

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

此外,使用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