All Products
Search
Document Center

Application Real-Time Monitoring Service:Use the OpenTelemetry SDK for Java to asynchronously pass the process-specific context

Last Updated:Jan 06, 2025

After you integrate the Application Monitoring sub-service of Application Real-Time Monitoring Service (ARMS) to monitor applications that use common Java frameworks, the ARMS agent automatically instruments the frameworks. This way, traces are collected without the need to modify your code. However, if you want to asynchronously pass the process-specific context, you can install the OpenTelemetry SDK for Java.

For information about the components and frameworks supported by the ARMS agent, see Java components and frameworks supported by ARMS.

Scenarios

If your application performs asynchronous programming by using any of the following methods, the ARMS agent automatically passes the trace context asynchronously, without the need for code modification.

  • Submit asynchronous tasks using JDK, Spring, or Netty thread pools.

  • Program with React or RxJava frameworks.

  • Use Spring @Async annotations.

  • Program with frameworks such as Spring Webflux or Spring Gateway.

However, in the following asynchronous processing scenarios, you must manually pass the context using the OpenTelemetry SDK for Java:

  • Producer-consumer scenario

    If thread T1 places a message in a queue and thread T2 retrieves it for processing, thread T2 must reuse the trace context of thread T1.

  • Database batch processing

    If process P1 writes a batch of data to the database and process P2 reads and processes the data, process P2 must reuse the trace context of process P1.

Prerequisites

Add dependencies

Add the following Maven dependencies to introduce OpenTelemetry SDK for Java. For more information, see Instrumentation.

<dependencies>
    <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-api</artifactId>
    </dependency>
    <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-sdk-trace</artifactId>
    </dependency>
    <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-sdk</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>io.opentelemetry</groupId>
      <artifactId>opentelemetry-bom</artifactId>
      <version>1.23.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Asynchronously pass the context

The following sample code shows a producer-consumer scenario. During event production, the producer records the trace context of its thread. During event consumption, it retrieves and restores the context.

class Event {
    private Context context;
    private String msg;

    public Event(Context context, String msg) {
        this.context = context;
        this.msg = msg;
    }
}

private final LinkedBlockingQueue<Event> linkedBlockingQueue = new LinkedBlockingQueue<Event>();

public void produce(String msg) {
    linkedBlockingQueue.add(new Event(Context.current(), msg));
}

public void consume() throws Exception {
    Event event = linkedBlockingQueue.take();
    try(Scope scope = event.context.makeCurrent()) {
        processEvent(event);
    }
}

public void processEvent(Event event) {
    //todo process event
}

Related step

You can associate trace IDs with application logs to promptly locate, analyze, and resolve errors. For more information, see Associate trace IDs with logs for a Java application.