Public APIedit

The public API of the Elastic APM Java agent lets you customize and manually create spans and transactions, as well as track errors.

The first step in getting started with the API is to declare a dependency to the API:

pom.xml. 

<dependency>
    <groupId>co.elastic.apm</groupId>
    <artifactId>apm-agent-api</artifactId>
    <version>${elastic-apm.version}</version>
</dependency>

build.gradle. 

compile "co.elastic.apm:apm-agent-api:$elasticApmVersion"

Replace the version placeholders with the latest version from maven central: Maven Central

Tracer APIedit

The tracer gives you access to the currently active transaction and span. It can also be used to track an exception.

To use the API, you can just invoke the static methods on the class co.elastic.apm.api.ElasticApm.

Transaction currentTransaction()edit

Returns the currently active transaction. See Transaction APIedit on how to customize the current transaction.

If there is no current transaction, this method will return a noop transaction, which means that you never have to check for null values.

import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Transaction;

Transaction transaction = ElasticApm.currentTransaction();
Note

Transactions created via ElasticApm.startTransaction() can not be retrieved by calling this method. See span.activate() on how to achieve that.

Span currentSpan()edit

Returns the currently active span or transaction. See Span APIedit on how to customize the current span.

If there is no current span, this method will return a noop span, which means that you never have to check for null values.

Note that even if this method is returning a noop span, you can still capture exceptions on it. These exceptions will not have a link to a Span or a Transaction.

import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Span;

Span span = ElasticApm.currentSpan();
Note

Spans created via startSpan() or startSpan(String, String, String) can not be retrieved by calling this method. See span.activate() on how to achieve that.

Transaction startTransaction()edit

Use this method to create a custom transaction.

Note that the agent will do this for you automatically when ever your application receives an incoming HTTP request. You only need to use this method to create custom transactions.

It is important to call void end()edit when the transaction has ended. A best practice is to use the transaction in a try-catch-finally block. Example:

Transaction transaction = ElasticApm.startTransaction();
try {
    transaction.setName("MyController#myAction");
    transaction.setType(Transaction.TYPE_REQUEST);
    // do your thing...
} catch (Exception e) {
    transaction.captureException(e);
    throw e;
} finally {
    transaction.end();
}
Note

Transactions created via this method can not be retrieved by calling ElasticApm.currentSpan() or ElasticApm.currentTransaction(). See transaction.activate() on how to achieve that.

Transaction startTransactionWithRemoteParent(HeaderExtractor) [1.3.0] Added in 1.3.0. edit

Similar to Transaction startTransaction()edit but creates this transaction as the child of a remote parent.

  • headerExtractor: a functional interface which receives a header name and returns the fist header with that name

Example:

// Hook into a callback provided by the framework that is called on incoming requests
public Response onIncomingRequest(Request request) throws Exception {
    // creates a transaction representing the server-side handling of the request
    Transaction transaction = ElasticApm.startTransactionWithRemoteParent(key -> request.getHeader(key));
    try (final Scope scope = transaction.activate()) {
        String name = "a useful name like ClassName#methodName where the request is handled";
        transaction.setName(name);
        transaction.setType(Transaction.TYPE_REQUEST);
        return request.handle();
    } catch (Exception e) {
        transaction.captureException(e);
        throw e;
    } finally {
        transaction.end();
    }
}

Transaction startTransactionWithRemoteParent(HeaderExtractor, HeadersExtractor) [1.3.0] Added in 1.3.0. edit

Similar to Transaction startTransaction()edit but creates this transaction as the child of a remote parent.

  • headerExtractor: a functional interface which receives a header name and returns the fist header with that name
  • headersExtractor: a functional interface which receives a header name and returns all headers with that name

Example:

// Hook into a callback provided by the framework that is called on incoming requests
public Response onIncomingRequest(Request request) throws Exception {
    // creates a transaction representing the server-side handling of the request
    Transaction transaction = ElasticApm.startTransactionWithRemoteParent(request::getHeader, request::getHeaders);
    try (final Scope scope = transaction.activate()) {
        String name = "a useful name like ClassName#methodName where the request is handled";
        transaction.setName(name);
        transaction.setType(Transaction.TYPE_REQUEST);
        return request.handle();
    } catch (Exception e) {
        transaction.captureException(e);
        throw e;
    } finally {
        transaction.end();
    }
}

Annotation APIedit

The API comes with two annotations which make it easier to create custom spans and transactions. Just put the annotations on top of your methods and the agent will take care of creating and reporting the corresponding transaction and spans. It will also make sure to capture any uncaught exceptions.

Note

It is required to configure the application_packagesedit, otherwise these annotations will be ignored.

@CaptureTransactionedit

Annotating a method with @CaptureTransaction creates a transaction for that method.

Note that this only works when there is no active transaction on the same thread.

  • value: The name of the span. Defaults to ClassName#methodName
  • type: The type of the transaction. Defaults to request
Note

Using this annotation implicitly creates a Transaction and activates it when entering the annotated method. It also implicitly ends it and deactivates it before exiting the annotated method. See ElasticApm.startTransaction(), transaction.activate() and transaction.end()

@CaptureSpanedit

Annotating a method with @CaptureSpan creates a span as the child of the currently active span or transaction (Span currentSpan()edit).

When there is no current span, no span will be created.

  • value: The name of the span. Defaults to ClassName#methodName
  • type: The type of the span, e.g. db for DB span. Defaults to app
  • subtype: The subtype of the span, e.g. mysql for DB span. Defaults to empty string
  • action: The action related to the span, e.g. query for DB spans. Defaults to empty string
Note

Using this annotation implicitly creates a Span and activates it when entering the annotated method. It also implicitly ends it and deactivates it before exiting the annotated method. See startSpan(), startSpan(String, String, String), span.activate() and span.end()

Transaction APIedit

A transaction is the data captured by an agent representing an event occurring in a monitored service and groups multiple spans in a logical group. A transaction is the first Span of a service, and is also known under the term entry span.

See Transaction currentTransaction()edit on how to get a reference of the current transaction.

Transaction is a sub-type of Span. So it has all the methods a Span offers plus additional ones.

Note

Calling any of the transaction’s methods after void end()edit has been called is illegal. You may only interact with transaction when you have control over its lifecycle. For example, if a span is ended in another thread you must not add labels if there is a chance for a race between the void end()edit and the Transaction addLabel(String key, value) [1.5.0] Added in 1.5.0. Number and boolean labels require APM Server 6.7 edit method.

Transaction setName(String name)edit

Override the name of the current transaction. For supported frameworks, the transaction name is determined automatically, and can be overridden using this method.

Example:

transaction.setName("My Transaction");
  • name: (required) A string describing name of the transaction

Transaction setType(String type)edit

Sets the type of the transaction. There’s a special type called request, which is used by the agent for the transactions automatically created when an incoming HTTP request is detected.

Example:

transaction.setType(Transaction.TYPE_REQUEST);
  • type: The type of the transaction

Transaction addLabel(String key, value) [1.5.0] Added in 1.5.0. Number and boolean labels require APM Server 6.7 edit

Labels are used to add indexed information to transactions, spans, and errors. Indexed means the data is searchable and aggregatable in Elasticsearch. Multiple labels can be defined with different key-value pairs.

  • Indexed: Yes
  • Elasticsearch type: object
  • Elasticsearch field: labels (previously context.tags in <v.7.0)

Label values can be a string, boolean, or number. Because labels for a given key are stored in the same place in Elasticsearch, all label values of a given key must have the same data type. Multiple data types per key will throw an exception, e.g. {foo: bar} and {foo: 42}

Note

Number and boolean labels were only introduced in APM Server 6.7+. Using this API in combination with an older APM Server versions leads to validation errors.

Important

Avoid defining too many user-specified labels. Defining too many unique fields in an index is a condition that can lead to a mapping explosion.

transaction.addLabel("foo", "bar");
  • String key: The tag key
  • String|Number|boolean value: The tag value

Transaction setUser(String id, String email, String username)edit

Call this to enrich collected performance data and errors with information about the user/client. This method can be called at any point during the request/response life cycle (i.e. while a transaction is active). The given context will be added to the active transaction.

If an error is captured, the context from the active transaction is used as context for the captured error.

transaction.setUser(user.getId(), user.getEmail(), user.getUsername());
  • id: The user’s id or null, if not applicable.
  • email: The user’s email address or null, if not applicable.
  • username: The user’s name or null, if not applicable.

void captureException(Exception e)edit

Captures an exception and reports it to the APM server.

String getId()edit

Returns the id of this transaction (never null)

If this transaction represents a noop, this method returns an empty string.

String getTraceId()edit

Returns the trace-id of this transaction.

The trace-id is consistent across all transactions and spans which belong to the same logical trace, even for transactions and spans which happened in another service (given this service is also monitored by Elastic APM).

If this span represents a noop, this method returns an empty string.

String ensureParentId()edit

If the transaction does not have a parent-ID yet, calling this method generates a new ID, sets it as the parent-ID of this transaction, and returns it as a String.

This enables the correlation of the spans the JavaScript Real User Monitoring (RUM) agent creates for the initial page load with the transaction of the backend service. If your backend service generates the HTML page dynamically, initializing the JavaScript RUM agent with the value of this method allows analyzing the time spent in the browser vs in the backend services.

To enable the JavaScript RUM agent when using an HTML templating language like Freemarker, add ElasticApm.currentTransaction() with the key "transaction" to the model.

Also, add a snippet similar to this to the body of your HTML page, preferably before other JS libraries:

<script src="elastic-apm-js-base/dist/bundles/elastic-apm-js-base.umd.min.js"></script>
<script>
  elasticApm.init({
    serviceName: "service-name",
    serverUrl: "http://localhost:8200",
    pageLoadTraceId: "${transaction.traceId}",
    pageLoadSpanId: "${transaction.ensureParentId()}",
    pageLoadSampled: ${transaction.sampled}
  })
</script>

See the JavaScript RUM agent documentation for more information.

Span startSpan(String type, String subtype, String action)edit

Start and return a new span with a type, a subtype and an action, as a child of this transaction.

The type, subtype and action strings are used to group similar spans together, with different resolution. For instance, all DB spans are given the type db; all spans of MySQL queries are given the subtype mysql and all spans describing queries are given the action query. In this example db is considered the general type. Though there are no naming restrictions for the general types, the following are standardized across all Elastic APM agents: app, db, cache, template, and ext.

Note

. (dot) character is not allowed within type, subtype and action. Any such character will be replaced with a _ (underscore) character.

It is important to call void end()edit when the span has ended. A best practice is to use the span in a try-catch-finally block. Example:

Span span = parent.startSpan("db", "mysql", "query");
try {
    span.setName("SELECT FROM customer");
    // do your thing...
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}
Note

Spans created via this method can not be retrieved by calling ElasticApm.currentSpan(). See span.activate() on how to achieve that.

Span startSpan()edit

Start and return a new custom span with no type as a child of this transaction.

It is important to call void end()edit when the span has ended. A best practice is to use the span in a try-catch-finally block. Example:

Span span = parent.startSpan();
try {
    span.setName("SELECT FROM customer");
    // do your thing...
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}
Note

Spans created via this method can not be retrieved by calling ElasticApm.currentSpan(). See span.activate() on how to achieve that.

Transaction setResult(String result)edit

A string describing the result of the transaction. This is typically the HTTP status code, or e.g. "success" for a background task

  • result: a string describing the result of the transaction

Transaction setStartTimestamp(long epochMicros) [1.5.0] Added in 1.5.0. edit

Sets the start timestamp of this event.

  • epochMicros: the timestamp of when this event started, in microseconds (µs) since epoch

void end()edit

Ends the transaction and schedules it to be reported to the APM Server. It is illegal to call any methods on a transaction instance which has already ended. This also includes this method and Span startSpan()edit. Example:

transaction.end();

void end(long epochMicros) [1.5.0] Added in 1.5.0. edit

Ends the transaction and schedules it to be reported to the APM Server. It is illegal to call any methods on a transaction instance which has already ended. This also includes this method and Span startSpan()edit.

  • epochMicros: the timestamp of when this event ended, in microseconds (µs) since epoch

Example:

transaction.end(System.currentTimeMillis() * 1000);

Scope activate()edit

Makes this span the active span on the current thread until Scope#close() has been called. Scopes should only be used in try-with-resource statements in order to make sure the Scope#close() method is called in all circumstances. Failing to close a scope can lead to memory leaks and corrupts the parent-child relationships.

This method should always be used within a try-with-resources statement:

Transaction transaction = ElasticApm.startTransaction();
// Within the try block the transaction is available
// on the current thread via ElasticApm.currentTransaction().
// This is also true for methods called within the try block.
try (final Scope scope = transaction.activate()) {
    transaction.setName("MyController#myAction");
    transaction.setType(Transaction.TYPE_REQUEST);
    // do your thing...
} catch (Exception e) {
    transaction.captureException(e);
    throw e;
} finally {
    transaction.end();
}
Note

Scope activate()edit and Scope#close() have to be called on the same thread.

boolean isSampled()edit

Returns true if this transaction is recorded and sent to the APM Server

void injectTraceHeaders(HeaderInjector headerInjector) [1.3.0] Added in 1.3.0. edit

  • headerInjector: tells the agent how to inject a header into the request object

Allows for manual propagation of the tracing headers.

If you want to manually instrument an RPC framework which is not already supported by the auto-instrumentation capabilities of the agent, you can use this method to inject the required tracing headers into the header section of that framework’s request object.

Example:

// Hook into a callback provided by the RPC framework that is called on outgoing requests
public Response onOutgoingRequest(Request request) throws Exception {
    // creates a span representing the external call
    Span span = ElasticApm.currentSpan()
            .startSpan("external", "http", null)
            .setName(request.getMethod() + " " + request.getHost());
    try (final Scope scope = transaction.activate()) {
        span.injectTraceHeaders((name, value) -> request.addHeader(name, value));
        return request.execute();
    } catch (Exception e) {
        span.captureException(e);
        throw e;
    } finally {
        span.end();
    }
}

Span APIedit

A span contains information about a specific code path, executed as part of a transaction.

If for example a database query happens within a recorded transaction, a span representing this database query may be created. In such a case the name of the span will contain information about the query itself, and the type will hold information about the database type.

See Span currentSpan()edit on how to get a reference of the current span.

Span setName(String name)edit

Override the name of the current span.

Example:

span.setName("SELECT FROM customer");
  • name: the name of the span

Span addLabel(String key, value) [1.5.0] Added in 1.5.0. edit

A flat mapping of user-defined labels with string, number or boolean values.

Note

In version 6.x, labels are stored under context.tags in Elasticsearch. As of version 7.x, they are stored as labels to comply with the Elastic Common Schema (ECS).

Note

The labels are indexed in Elasticsearch so that they are searchable and aggregatable. By all means, you should avoid that user specified data, like URL parameters, is used as a tag key as it can lead to mapping explosions.

span.addLabel("foo", "bar");
  • String key: The tag key
  • String|Number|boolean value: The tag value

void captureException(Exception e)edit

Captures an exception and reports it to the APM server.

String getId()edit

Returns the id of this span (never null)

If this span represents a noop, this method returns an empty string.

String getTraceId()edit

Returns the trace-ID of this span.

The trace-ID is consistent across all transactions and spans which belong to the same logical trace, even for transactions and spans which happened in another service (given this service is also monitored by Elastic APM).

If this span represents a noop, this method returns an empty string.

Span setStartTimestamp(long epochMicros) [1.5.0] Added in 1.5.0. edit

Sets the start timestamp of this event.

  • epochMicros: the timestamp of when this event started, in microseconds (µs) since epoch

void end()edit

Ends the span and schedules it to be reported to the APM Server. It is illegal to call any methods on a span instance which has already ended. This also includes this method and Span startSpan()edit.

void end(long epochMicros) [1.5.0] Added in 1.5.0. edit

Ends the span and schedules it to be reported to the APM Server. It is illegal to call any methods on a span instance which has already ended. This also includes this method and Span startSpan()edit.

  • epochMicros: the timestamp of when this event ended, in microseconds (µs) since epoch

Example:

span.end(System.currentTimeMillis() * 1000);

Span startSpan(String type, String subtype, String action)edit

Start and return a new span with a type, a subtype and an action, as a child of this transaction.

The type, subtype and action strings are used to group similar spans together, with different resolution. For instance, all DB spans are given the type db; all spans of MySQL queries are given the subtype mysql and all spans describing queries are give the action query. In this example db is considered the general type. Though there are no naming restrictions for the general types, the following are standardized across all Elastic APM agents: app, db, cache, template, and ext.

Note

. (dot) character is not allowed within type, subtype and action. Any such character will be replaced with a _ (underscore) character.

It is important to call void end()edit when the span has ended. A best practice is to use the span in a try-catch-finally block. Example:

Span span = parent.startSpan("db", "mysql", "query");
try {
    span.setName("SELECT FROM customer");
    // do your thing...
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}
Note

Spans created via this method can not be retrieved by calling ElasticApm.currentSpan(). See span.activate() on how to achieve that.

Span startSpan()edit

Start and return a new custom span with no type as a child of this transaction.

It is important to call void end()edit when the span has ended. A best practice is to use the span in a try-catch-finally block. Example:

Span span = parent.startSpan();
try {
    span.setName("SELECT FROM customer");
    // do your thing...
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}
Note

Spans created via this method can not be retrieved by calling ElasticApm.currentSpan(). See span.activate() on how to achieve that.

Scope activate()edit

Makes this span the active span on the current thread until Scope#close() has been called. Scopes should only be used in try-with-resource statements in order to make sure the Scope#close() method is called in all circumstances. Failing to close a scope can lead to memory leaks and corrupts the parent-child relationships.

This method should always be used within a try-with-resources statement:

Span span = parent.startSpan("db", "mysql", "query");
// Within the try block the span is available
// on the current thread via ElasticApm.currentSpan().
// This is also true for methods called within the try block.
try (final Scope scope = span.activate()) {
    span.setName("SELECT FROM customer");
    // do your thing...
} catch (Exception e) {
    span.captureException(e);
    throw e;
} finally {
    span.end();
}
Note

Calling any of the span’s methods after void end()edit has been called is illegal. You may only interact with span when you have control over its lifecycle. For example, if a span is ended in another thread you must not add labels if there is a chance for a race between the void end()edit and the Span addLabel(String key, value) [1.5.0] Added in 1.5.0. edit method.

boolean isSampled()edit

Returns true if this span is recorded and sent to the APM Server

void injectTraceHeaders(HeaderInjector headerInjector) [1.3.0] Added in 1.3.0. edit

  • headerInjector: tells the agent how to inject a header into the request object

Allows for manual propagation of the tracing headers.

If you want to manually instrument an RPC framework which is not already supported by the auto-instrumentation capabilities of the agent, you can use this method to inject the required tracing headers into the header section of that framework’s request object.

Example:

// Hook into a callback provided by the RPC framework that is called on outgoing requests
public Response onOutgoingRequest(Request request) throws Exception {
    // creates a span representing the external call
    Span span = ElasticApm.currentSpan()
            .startSpan("external", "http", null)
            .setName(request.getMethod() + " " + request.getHost());
    try (final Scope scope = transaction.activate()) {
        span.injectTraceHeaders((name, value) -> request.addHeader(name, value));
        return request.execute();
    } catch (Exception e) {
        span.captureException(e);
        throw e;
    } finally {
        span.end();
    }
}