This topic describes how to use Application Real-Time Monitoring Service (ARMS) SDKs to dynamically obtain a trace ID and the properties of the trace from the service code.

Note ARMS allows you to use OpenTelemetry SDK for Java to instrument and monitor your application. We recommend that you use OpenTelemetry SDK for Java to instrument your application. For more information, see OpenTelemetry SDK for Java.

Prerequisites

  • Your Java application is connected to ARMS and the ARMS agent for application monitoring is installed and started in the application. For more information, see Manually install the ARMS agent for a Java application.
  • arms-sdk-1.7.3.jar is introduced to the application.
    <dependency>
        <groupId>com.alibaba.arms.apm</groupId>
        <artifactId>arms-sdk</artifactId>
        <version>1.7.3</version>
    </dependency>
    Note If you cannot obtain the pom.xml file, download arms-sdk-1.7.3.jar.

Obtain a trace ID and a RPC ID

You can run the following code to obtain a trace ID and a remote procedure call (RPC) ID:

Span span = Tracer.builder().getSpan();
String traceId = span.getTraceId();
String rpcId = span.getRpcId();

Pass through custom baggage items

To pass through custom baggage items, you must add the items to and obtain the items from the service code by performing the following steps:

  1. Add baggage items to the service code.

    Map<String, String> baggage = new HashMap<String, String>();
    baggage.put("key-01", "value-01");
    baggage.put("key-02", "value-02");
    baggage.put("key-03", "value-03");
    Span span = Tracer.builder().getSpan();
    span.withBaggage(baggage);
  2. Obtain the baggage items from the service code.

    Span span = Tracer.builder().getSpan();
    Map<String, String> baggage = span.baggageItems();

Add custom tags to a span

A custom tag can be added only to the current span and cannot be passed to other spans. To add custom tags to a span, add the tags to and obtain the tags from the service code by performing the following steps:

  1. Add tags to a span in the service code. You can specify multiple tags.

    Span span = Tracer.builder().getSpan();
    // Add a tag to the Span.
    span.setTag("tag-key1", "tag-value1");
    span.setTag("tag-key2", "tag-value2");
  2. Obtain the tags from the service code.

    Span span = Tracer.builder().getSpan();
    // Inspect the Span's tags.
    Map<String, String> tags = span.tags();

Query traces based on baggage items and tags

You can use baggage items and tags that are added to a span to query traces.

  • A baggage item can be passed to the downstream and is generally used to color business data. We recommend that you do not set a large number of baggage items.
  • A tag applies only to the current span. You can add multiple tags to a span.
  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Application Monitoring > Invocation Trace Query and select a region in the top navigation bar.
  3. On the Call link query page, set the Parameter type and Parameter value parameters and click Add to query criteria.
  4. From the returned results, find the required trace and click its ID.
  5. On the Traces tab, move the pointer over the value in the Service column to view the tags and baggage items of the current span. baggage items are automatically added to the tags of the span. trace_span_with_tags