Logging opentelemetry trace in spring boot reactive server.

huangapple go评论69阅读模式
英文:

Logging opentelemetry tracid in spring boot reactive server

问题

我正在开发一个基于Spring Boot 3的Java REST API服务器,使用spring-boot-starter-webflux。

如何集成Opentelemetry TraceID,以便每个日志行都包含TraceID?

谢谢,

英文:

I am developing a java rest api server with spring boot 3 based on spring-boot-starter-webflux.

How to integrate opentelemetry traceid so that each log line contains the traceid?

Thanks,

答案1

得分: 1

答案取决于您选择的仪器。

如果您正在使用 java agent,则无需包含任何依赖项,您可以直接使用 thread_idspan_id 来配置您的记录器,就像在此 log4j 示例中一样:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout
          pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="Console" level="All"/>
    </Root>
  </Loggers>
</Configuration>

如果您没有使用 java agent,您需要包含一个库,将跨度和跟踪ID注入上下文或MDC对象中。
除此之外,它与上面的示例一样工作。

请注意,还可以使用OTLP协议将日志发送到OpenTelemetry后端。从技术上讲,这意味着添加一个与控制台附加程序独立的不同附加程序。此附加程序将跨度和跟踪ID与消息正文分开传输(作为属性)。

  • 如果您正在使用java代理,此附加程序将自动为您添加。
  • 如果您没有使用java代理,您可以在grafana OpenTelemetry starter中找到一个完整的示例(完全披露:我是这个starter的作者)。
英文:

The answer depends on your choice of instrumentation.
If you're using the java agent, you don't need to include any dependencies, you can use thread_id and span_id directly to configure your logger, such as in this log4j example:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout
          pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="Console" level="All"/>
    </Root>
  </Loggers>
</Configuration>

If you're not using the java agent, you need to include a library that injects the span and trace ID into the context or MDC object.
Other than that, it works as in the example above.

Note that it's also possible to send logs to an OpenTelemetry backend using the OTLP protocol. Technically this means adding a different appender independent from the console appender. This appender will transmit the span and trace ID separately from the message body (as attributes).

  • If you're using the java agent, this appender is automatically added for you.
  • If you're not using the java agent, you can find a full example in the grafana OpenTelemetry starter (full disclosure: I'm the author of this starter).

huangapple
  • 本文由 发表于 2023年6月26日 17:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定