Java Custom Instrumentation

AppSignal is compatible with OpenTelemetry instrumentation packages. Some of these instrumentation packages may require additional setup.

These instrumentations do not not always include all the information you need to debug issues. For more fine-grained reporting, you can add custom instrumentation.

We support OpenTelemetry tracing as laid out in the OpenTelemetry documentation. It's possible to add more data to span using OpenTelemetry span attributes.

There are also AppSignal span attributes that can change how traces are grouped and some attributes that allow for setting sensitive data that is filtered before being sent to our servers.

Creating a new span

Using the tracer from the global tracer provider, create a new span.

Java
import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Scope; public void someFunction() { // Get OpenTelemetry tracer Tracer tracer = GlobalOpenTelemetry.getTracer("my-app"); // Create a new span Span span = tracer.spanBuilder("My span").startSpan(); // Set attributes span.setAttribute("operation.name", "handle_request"); // Your application logic here // Close the span span.end(); }

Note: In these examples we're using the global tracer provider set by OpenTelemetry. You may want to inject the tracer into your classes for better testability.

Get the current span

You can also fetch the current active span:

Java
import io.opentelemetry.api.trace.Span; public void someFunction() { Span span = Span.current(); // Set attributes on the current span span.setAttribute("user.id", userId); span.setAttribute("operation.name", "database_query"); }

Note: This will only return a span if one is already active.

More ways to create and get spans are documented on the OpenTelemetry Java Instrumentation page.

AppSignal attributes

Once you have created, or fetched, a span, you can set certain attributes that are specific to AppSignal to customize how the data arrives in AppSignal.

Java
Span span = // Create a new span or fetch the current span span.setAttribute("attribute_name", "attribute_value");

See our guides for more information on what custom AppSignal attributes are supported: