JAVA¶
Link to the JAVA application using the observation cloud:
- Collect custom metrics data from applications;
- Collect link tracking data from applications;
- Manage all logs for the application.
Configuration¶
Metric Collection¶
You can view jvm doc
Link Tracking Collection¶
Apply Open Correspondence Collector, then use the JAVA agent tool to get traces information and send it to the observation cloud.
Logging¶
opentelemetry-java
(hereinafter referred to as "Agent") is injected into the application through javaagent
. After the application generates trace information, the traceId
and spanId
can be passed as parameters to log
so that log
will be output with traceId
and spanId
.
????+ Info "About MDC"
It contains thread local context information and copies it to each log event captured by the log library.
OTEL JAVA Agent injects several pieces of information about the current span into the MDC copy of each logged event:
- trace_idcurrent tracking ID (same as
Span.current().getSpanContext().getTraceId()
); - span_idcurrent span ID (same as
Span.current().getSpanContext().getSpanId()
); - trace_flagsCurrent trace flag, formatted according to W3C trace flag format (same as
Span.current().getSpanContext().getTraceFlags().asHex()
).
These three pieces of information can be included in the log statements generated by the log library by specifying them in the pattern/format.
Tip: For SpringBoot configurations that use logback
, you can add MDC to the log line logging.pattern.level
by simply overwriting:
logging.pattern.level = trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
This allows any service or tool that parses the application log to associate trace/span with log statements.
Log Configuration¶
The APM tool configuration logs pattern
differ from each other. Take logback.xml
for example:
OpenTelemetry MDC parameter:
- 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" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
DDTrace MDC parameter:
- 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" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>-