Enable trace to log correlation
You can enable trace to log correlation in Application Signals. This automatically injects trace IDs and span IDs into the relevant application logs. Then, when you open a trace detail page in the Application Signals console, the relevant log entries (if any) that correlate with the current trace automatically appear at the bottom of the page.
For example, suppose you notice a spike in a latency graph. You can choose the point on the graph to load the diagnostics information for that point in time. You then choose the relevant trace to get more information. When you view the trace information, you can scroll down to see the logs associated with the trace. These logs might reveal patterns or error codes associated with the issues causing the latency spike.
To achieve trace log correlation, Application Signals relies on the following:
Logger MDC auto-instrumentation
for Java. OpenTelemetry Logging Instrumentation
for Python. The Pino
, Winston , or Bunyan auto-instrumentations for Node.js.
All of these isntrumentations are provided by OpenTelemetry community. Application Signals uses them to inject trace contexts such as trace ID and span ID into application logs. To enable this, you must manually change your logging configuration to enable the auto-instrumentation.
Depending on the architecture that your application runs on, you might have to also set an environment variable to enable trace log correlation, in addition to following the steps in this section.
On Amazon EKS, no further steps are needed.
On Amazon ECS, no further steps are needed.
On Amazon EC2, see the step 4 in the procedure in Step 3: Instrument your application and start it.
After you enable trace log correlation,
Trace log correlation setup examples
This section contains examples of setting up trace log correlation in several environments.
Spring Boot for Java
Suppose you have a Spring Boot application in a folder called custom-app
. The application configuration is usually a YAML file
named custom-app/src/main/resources/application.yml
that might look like this:
spring: application: name: custom-app config: import: optional:configserver:${CONFIG_SERVER_URL:http://localhost:8888/} ...
To enable trace log correlation, add the following logging configuration.
spring: application: name: custom-app config: import: optional:configserver:${CONFIG_SERVER_URL:http://localhost:8888/} ... logging: pattern: level: trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
Logback for Java
In the logging configuration (such as logback.xml), insert the trace context trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
into pattern
of Encoder. For example, the following configuration prepends the trace context before the log message.
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>app.log</file> <append>true</append> <encoder> <pattern>trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p - %m%n</pattern> </encoder> </appender>
For more information about encoders in Logback, see
Encoders
Log4j2 for Java
In the logging configuration (such as log4j2.xml), insert the trace context trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
into PatternLayout
.
For example, the following configuration prepends the trace context before the log message.
<Appenders> <File name="FILE" fileName="app.log"> <PatternLayout pattern="trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p - %m%n"/> </File> </Appenders>
For more information about pattern layouts in Log4j2, see
Pattern Layout
Log4j for Java
In the logging configuration (such as log4j.xml), insert the trace context trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
into PatternLayout
.
For example, the following configuration prepends the trace context before the log message.
<appender name="FILE" class="org.apache.log4j.FileAppender">; <param name="File" value="app.log"/>; <param name="Append" value="true"/>; <layout class="org.apache.log4j.PatternLayout">; <param name="ConversionPattern" value="trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p - %m%n"/>; </layout>; </appender>;
For more information about pattern layouts in Log4j, see
Class Pattern Layout
Python
Set the environment variable OTEL_PYTHON_LOG_CORRELATION
to true
while running your application. For more information, see
Enable trace context injection
Node.js
For more information about enabling trace context injection in Node.js for the logging libraries that support it,
see the NPM usage documentations of the
Pino