JAVA¶
Link Guance to JAVA applications:
- Collect custom Metrics data from the application;
- Collect APM data from the application;
- Manage all LOGs of the application.
Configuration¶
Metric Collection¶
You can refer to the JVM documentation
APM Data Collection¶
Enable the corresponding collector in the application, then use the JAVA agent tool to obtain trace information and send it to Guance.
Log Correlation¶
opentelemetry-java
(hereinafter referred to as "Agent") is injected into the application via the javaagent
method. After the application generates trace information, by setting MDC, traceId
and spanId
can be passed as parameters to log
, so that the log
will carry traceId
and spanId
when outputting.
About MDC
Mapping Diagnostic Context (MDC) is a tool used to distinguish interleaved log outputs from different sources.— log4j MDC documentation
It contains thread-local context information and copies it into each log event captured by the logging library.
The OTEL JAVA Agent injects several pieces of information about the current span into the MDC copy of each log record event:
- trace_id The current trace id (the same as
Span.current().getSpanContext().getTraceId()
); - span_id The current span id (the same as
Span.current().getSpanContext().getSpanId()
); - trace_flags Current trace flags, formatted according to the W3C Trace Flags format (the same as
Span.current().getSpanContext().getTraceFlags().asHex()
).
These three pieces of information can be included in log statements generated by the logging library by specifying them in the pattern/format.
Tip: For SpringBoot configurations using logback
, you can add MDC to log lines by simply overriding the following content for logging.pattern.level
:
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
This way, any service or tool parsing application logs can associate traces/spans with log statements.
Log Configuration¶
Log pattern
configurations differ for various APM tools. Here's an example using logback.xml
:
OpenTelemetry MDC parameters:
- trace_id
- span_id
<property name="log.pattern" value="%d{HH:mm:ss} [%thread] %-5level %logger{10} [traceId=%X{trace_id} spanId=%X{span_id} userId=%X{user-id}] %msg%n" />
<!-- %m outputs information, %p log level, %t thread name, %d date, %c full class name,,,, -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
DDTrace MDC parameters:
- dd.service
- dd.trace_id
- dd.span_id
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - [%method,%line] %X{dd.service} %X{dd.trace_id} %X{dd.span_id} - %msg%n" />
<!-- %m outputs information, %p log level, %t thread name, %d date, %c full class name,,,, -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>-