All Products
Search
Document Center

Application Real-Time Monitoring Service:Associate trace IDs with business logs

Last Updated:Aug 14, 2024

You can associate trace IDs with the business logs of an application. This way, if an error occurs in the application, you can access the business logs associated with trace IDs to find out and troubleshoot the error.

Prerequisites

Note

Only Application Monitoring Pro Edition supports this feature.

Background information

In ARMS, trace IDs can be associated with the business logs of an application based on the Mapped Diagnostic Context (MDC) mechanism. Mainstream log frameworks such as Log4j, Log4j 2, and Logback are supported.

Procedure

  1. Log on to the ARMS console. In the left-side navigation pane, choose Application Monitoring > Application List.

  2. On the Application List page, select a region in the top navigation bar and click the name of the application that you want to manage.

    Note

    If the Java图标 icon is displayed in the Language column, the application is connected to Application Monitoring. If a hyphen (-) is displayed, the application is connected to Managed Service for OpenTelemetry.

  3. In the top navigation bar, choose Configuration > Custom Configurations.

  4. In the Application log Association configuration section of the Custom Configurations tab, set the Log Source parameter to Log service SLS, turn on Automatically associate business logs with TraceId, select the region where your SLS service is deployed, and then select a project and a Logstore.

    You can also turn on Logs are automatically populated with traceId and Logs are automatically populated with spanId.

    image

    Note
  5. Add %X{EagleEye-TraceID} to the pattern properties in the layout of business logs.

    You can also associate a span ID by adding %X{span_id}.

    Important
    • Association of span IDs is supported only by the ARMS agent V4.x and later.

    • For information about how to obtain {EagleEye-TraceID} in the code, see ARMS SDKs.

    The following examples show how to modify the configuration files of Log4j, Log4j2, and Logback frameworks:

    • Modify the Log4j configuration file log4j.properties:

      log4j.appender.warn.layout=org.apache.log4j.PatternLayout
      log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %m%n
    • Modify the Log4j2 configuration file log4j2.xml:

      PatternLayout

      <console name="Console" target="SYSTEM_OUT">
          <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
          <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %m%n"/>
      </console>

      JsonLayout

      <JsonLayout   complete="false" compact="true" eventEol="true"
                    properties="false" locationInfo="true"
                    includeStacktrace="true" stacktraceAsString="true"
                    objectMessageAsJsonObject="true" >
          <KeyValuePair key="EagleEye-TraceID" value="${ctx:EagleEye-TraceID}" />
          <KeyValuePair key="span_id" value="${ctx:span_id}" />
      </JsonLayout>
    • Modify the Logback configuration file logback.xml:

      PatternLayout

      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
          <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %msg%n</pattern>
      </encoder>

      JsonLayout

      <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
      	<providers>
      		<timestamp/>
      		<threadName/>
      		<logLevel/>
      		<loggerName/>
      		<message/>
      		<callerData/>
      		<stackTrace/>
      		<pattern>
      			<pattern>
      				{
      				"EagleEye-TraceID": "%X{EagleEye-TraceID}",
                                      "span_id": "%X{span_id}"
      				}
      			</pattern>
      		</pattern>
      	</providers>
      </encoder>
  6. Restart the application.

    If trace IDs are displayed in the business logs of the application, the business logs are associated with the trace IDs, as shown in the following figure.dg_am_log_traceid

  7. Optional. Configure log collection and upload application logs to the project and Logstore.

    By default, ARMS does not collect application logs.