ARMS Java SDK (Not recommended)
The ARMS Java SDK retrieves trace IDs and related call chain properties from your application code.
Usage notes
For agent versions 4.x and later, we recommend using the OpenTelemetry Java SDK for custom instrumentation and to obtain the trace context:
-
Implement custom instrumentation by using the OpenTelemetry Java SDK
-
Implement cross-process context propagation by using the OpenTelemetry Java SDK
The ARMS SDK is no longer recommended for custom instrumentation or trace context retrieval, even if you cannot upgrade to a 4.x agent.
Prerequisites
-
Application monitoring is enabled in the Cloud Monitor 2.0 console, and the application monitoring agent is installed and started in your Java application. For more information, see Manually install an ARMS agent for Java applications.
-
The
arms-sdk-1.7.3.jardependency is added to your application.<dependency> <groupId>com.alibaba.arms.apm</groupId> <artifactId>arms-sdk</artifactId> <version>1.7.3</version> </dependency>NoteIf you cannot resolve the dependency from the pom.xml file, you can download arms-sdk-1.7.3.jar directly.
Obtain a trace ID and RPC ID
Run the following code to obtain a trace ID and RPC ID:
Span span = Tracer.builder().getSpan(); // No new Span is created here.
String traceId = span.getTraceId();
String rpcId = span.getRpcId();
Pass through custom baggage
In spring-cloud-gateway scenarios where baggage is added by using a filter, baggage propagation stops working after you upgrade the agent to version 4.2.x and enable the spring-bean plugin in custom configurations. To use baggage propagation, disable the spring-bean plugin or downgrade the agent to version 4.1.x or earlier.
To add and retrieve custom baggage items in your code:
-
Add custom baggage items in your application 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); -
Get the custom baggage items from your application code.
Span span = Tracer.builder().getSpan(); Map<String, String> baggage = span.baggageItems(); -
For agent versions 4.x and later, baggage is for pass-through purposes only and is not automatically added to the tags of each Span. If needed, you can manually add custom tags to a Span.
Add custom tags to a Span
Custom tags apply only to the Span on which they are set and are not propagated to downstream services. To set and retrieve custom tags:
-
Add one or more custom tags to a Span in your application code.
Span span = Tracer.builder().getSpan(); // Add a tag to the Span. span.setTag("tag-key1", "tag-value1"); span.setTag("tag-key2", "tag-value2"); -
Get the custom tags of a Span from your application code.
Span span = Tracer.builder().getSpan(); // Inspect the Span's tags. Map<String, String> tags = span.tags();
Query by custom baggage and tags
Use custom baggage and tags set on a Span to query traces in the Cloud Monitor 2.0 console.
-
A baggage item is passed to downstream services and is typically used for business context propagation. Avoid setting too many baggage items.
-
A tag is valid only within the scope of the current Span. You can set multiple business-related tags.
-
Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left-side navigation pane, choose .
-
On the Trace Query page, select a region at the top of the page, select a Parameter Type, enter a custom tag in the Parameter value field, and then click Add to Query Condition.
-
In the trace list, click the trace ID of the desired trace.
-
On the Trace details page, hover over the Service Name to view the tags for the current Span.
-
For agent versions 4.x and later, to add baggage content to a Span, get all baggage items as described in Pass through custom baggage and then write them to the Span as tags as described in Add custom tags to a Span.