API/Code
Use the span API to manually create spans for methods of interest. The API is extremely flexible, and offers the ability to customize your spans, by adding labels to them, or by changing the type, name, or timestamp.
Tip
OpenTracing fan? You can use the OpenTracing API, instead of the Agent API, to manually create spans.
- Get the current span with
currentSpan()
, which may or may not have been created with auto-instrumentation. - Create a child span with
startSpan()
. - Activate the span with
activate()
. - Customize the span with the span API.
import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Span;
Span parent = ElasticApm.currentSpan(); 1
Span span = parent.startSpan(); 2
try (Scope scope = span.activate()) { 3
span.setName("SELECT FROM customer"); 4
span.addLabel("foo", "bar"); 5
// do your thing...
} catch (Exception e) {
span.captureException(e);
throw e;
} finally {
span.end();
}
- Get current span
- Create a child span
- Make this span the active span on the current thread
- Override the default span name
- Add labels to the span
You can combine annotations with the span API to increase their flexibility. Just get the current span on an annotated method and customize the span to your liking.
@CaptureSpan 1
private static void spanWithAnnotation(String foo) {
Span span = ElasticApm.currentSpan(); 2
span.setTag("foo", foo); 3
}
- Use
@CaptureSpan
annotation to create a span - Get the current span (the one created via the
@CaptureSpan
annotation) - Customize the span