The annotation API allows you to place annotations on top of methods to automatically create spans for them. This method of creating spans is easier, more robust, and typically more performant than using the API; there’s nothing you can do wrong like forgetting to end a span or close a scope.

Annotations are less flexible when used on their own, but can be combined with the span API for added flexibility.

How-to create spans with the annotations APIedit

Here’s an example that uses the @CaptureSpan annotation to create a span for the spanWithAnnotation() method. The span is named spanName, is of type ext, and subtype http.

@CaptureSpan(value = "spanName", type = "ext", subtype = "http")
private static void spanWithAnnotation() {
    // do your thing...

Combine with the span APIedit

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.

private static void spanWithAnnotation(String foo) {
    Span span = ElasticApm.currentSpan(); 
    span.setTag("foo", foo); 

Use @CaptureSpan annotation to create a span

Get the current span (the one created via the @CaptureSpan annotation)

Customize the span