简体   繁体   English

打开遥测记录器 MDC 自动检测

[英]Open Telemetry Logger MDC auto-instrumentation

The following Open Telemetry starters have been added to the Spring Boot project ( v2.7.2 ) to instrument the application:以下 Open Telemetry starter 已添加到 Spring Boot 项目 ( v2.7.2 ) 以检测应用程序:

<dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-spring-boot-starter</artifactId>
    <version>1.22.1-alpha</version>
</dependency>
<dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-jaeger-spring-boot-starter</artifactId>
    <version>1.22.1-alpha</version>
</dependency>

Traces and spans are successfully exported to a Jaeger collector.跟踪和跨度已成功导出到 Jaeger 收集器。 The problem is those traces and spans cannot be correlated with log statements because logs don't contain the current trace_id and span_id .问题是那些跟踪和跨度无法与日志语句关联,因为日志不包含当前的trace_idspan_id

By following the documentation I added the logging.pattern.level property to application.properties but it seems like information about the current span is not injected into the logging event's MDC copy.按照文档,我将logging.pattern.level属性添加到application.properties ,但似乎有关当前跨度的信息未注入到日志记录事件的 MDC 副本中。

logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p

For example:例如:

log.info(
    "traceId: {}, spanId: {}",
    Span.current().getSpanContext().getTraceId(),
    Span.current().getSpanContext().getSpanId());

2023-01-25 12:21:36.774 trace_id= span_id= trace_flags= INFO 34272 --- [nio-8080-exec-2] h.c.DemoController: traceId: 1bccb6a4fea8345026ca87a202f0091f, spanId: c59a5d44ee40cd2c 2023-01-25 12:21:36.774 trace_id= span_id= trace_flags= INFO 34272 --- [nio-8080-exec-2] h.c.DemoController: traceId: 1bccb6a4fea8345026ca87a202f0091f, spanId: c49a5

Have I missed anything?我错过了什么吗?

opentelemetry-spring-boot-starter is missing this feature right now (version 1.22.1-alpha ), but it can be added very easily. opentelemetry-spring-boot-starter现在缺少此功能(版本1.22.1-alpha ),但可以很容易地添加它。

Firstly, MDC Instrumentation for Logback dependency should be added to the project.首先,应将MDC Instrumentation for Logback依赖项添加到项目中。

  <dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-logback-mdc-1.0</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
  </dependency>

Then add a custom logback.xml configuration to the classpath, which will automatically inject context information from the span context to logging events.然后在classpath中添加一个自定义的logback.xml配置,它会自动将span上下文中的上下文信息注入到日志事件中。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--    Default logback configuration    -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
    <!--   -->
    <appender name="OTEL" class="io.opentelemetry.instrumentation.logback.v1_0.OpenTelemetryAppender">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </appender>
    <root level="INFO">
        <appender-ref ref="OTEL"/>
    </root>
</configuration>

Finally, add the property to application.properties (as mentioned in the question):最后,将属性添加到application.properties (如问题中所述):

logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p

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

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