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 ist 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();

Span currentSpan()edit

Returns the currently active span. 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.

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

Span span = ElasticApm.currentSpan();

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 = tracer.startTransaction()
try {
    transaction.setName("MyController#myAction");
    span.setType(Transaction.TYPE_REQUEST);
    // do your thing...
} catch (Exception e) {
    ElasticApm.captureException(e);
    throw e;
} finally {
    transaction.end();
}

Span startSpan()edit

Start and return a new custom span associated with the current active 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 = tracer.startSpan()
try {
    span.setName("SELECT FROM customer");
    span.setType("db.mysql.query");
    // do your thing...
} catch (Exception e) {
    ElasticApm.captureException(e);
    throw e;
} finally {
    span.end();
}

void captureException(Exception e)edit

Captures an exception and reports it to the APM server.

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.

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

void 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

void 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

void addTag(String key, String value)edit

A flat mapping of user-defined tags with string values. Note: the tags 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.

transaction.setTag("foo", "bar");
  • key: The tag key
  • value: The tag value

void 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 end()edit

End tracking the transaction. Should be called e.g. at the end of a request or when ending a background task. Example:

transaction.end();

As Transaction also implements the java.lang.AutoCloseable interface, you can use it in try-with-resource blocks. See Transaction startTransaction()edit.

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.

void setName(String name)edit

Override the name of the current span.

Example:

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

void setType(String type)edit

Sets the type of span. The type is a hierarchical string used to group similar spans together. For instance, all spans of MySQL queries are given the type db.mysql.query.

In the above example db is considered the type prefix. Though there are no naming restrictions for this prefix, the following are standardized across all Elastic APM agents: app, db, cache, template, and ext.

  • type: the type of the span

void end()edit

Ends the span. If the span has already ended, nothing happens.

As Span also implements the java.lang.AutoCloseable interface, you can use it in try-with-resource blocks. See Span startSpan()edit.