Public API

edit

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 API

edit

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

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

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

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() but creates this transaction as the child of a remote parent.

  • headerExtractor: a functional interface which receives a header name and returns the first 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() but creates this transaction as the child of a remote parent.

  • headerExtractor: a functional interface which receives a header name and returns the first 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();
    }
}

If the protocol does not support multi-value headers, use Transaction startTransactionWithRemoteParent(HeaderExtractor) [1.3.0] Added in 1.3.0.

void setServiceInfoForClassLoader(ClassLoader, String, String) [1.30.0] Added in 1.30.0.

edit

Associates a class loader with a service name and version.

The association is used to overwrite the autodetected service name and version when a transaction is started.

If the class loader already is associated with a service name and version, the existing information will not be overwritten.

  • classLoader: the class loader which should be associated with the given service name and version
  • serviceName: the service name
  • serviceVersion: the service version

Annotation API

edit

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.

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

@CaptureTransaction

edit

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 transaction. Defaults to ClassName#methodName
  • type: The type of the transaction. Defaults to request

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

@CaptureSpan

edit

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

When there is no current span or transaction, 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
  • discardable: By default, spans may be discarded in certain scenarios. Set this attribute to false to make this span non-discardable.
  • exit: By default, spans are internal spans, making it an exit span prevents the creation of nested spans and is intended to represent calls to an external system like a database or third-party service.

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

@Traced [1.11.0] Added in 1.11.0.

edit

Annotating a method with @Traced creates a span as the child of the currently active span or transaction.

When there is no current span, a transaction will be created instead.

Use this annotation over @CaptureSpan or @CaptureTransaction if a method can both be an entry point (a transaction) or a unit of work within a transaction (a span).

  • value: The name of the span or transaction. Defaults to ClassName#methodName
  • type: The type of the span or transaction. Defaults to request for transactions and app for spans
  • subtype: The subtype of the span, e.g. mysql for DB span. Defaults to empty string. Has no effect when a transaction is created.
  • action: The action related to the span, e.g. query for DB spans. Defaults to empty string. Has no effect when a transaction is created.
  • discardable: By default, spans may be discarded in certain scenarios. Set this attribute to false to make this span non-discardable. This attribute has no effect if the created event is a Transaction.

Using this annotation implicitly creates a span or transaction 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 API

edit

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() 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.

Calling any of the transaction’s methods after void end() 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() and the Transaction setLabel(String key, value) [1.5.0 as addLabel] Added in 1.5.0 as addLabel. Number and boolean labels require APM Server 6.7 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 setFrameworkName(String frameworkName) [1.25.0] Added in 1.25.0.

edit

Provides a way to manually set the service.framework.name field. For supported frameworks, the framework name is determined automatically, and can be overridden using this function. null or the empty string will make the agent omit this field.

Example:

transaction.setFrameworkName("My Framework");
  • frameworkName: The name of the framework

Transaction setServiceInfo(String serviceName, String serviceVersion) [1.30.0] Added in 1.30.0.

edit

Sets the service name and version for this transaction and its child spans.

If this method is called after child spans are already created, they may have the wrong service name and version.

  • serviceName: the service name
  • serviceVersion: the service version

Transaction useServiceInfoForClassLoader(ClassLoader classLoader) [1.30.0] Added in 1.30.0.

edit

Sets the service name and version, that are associated with the given class loader (see: ElasticApm#setServiceInfoForClassLoader(ClassLoader, String, String)), for this transaction and its child spans.

If this method is called after child spans are already created, they may have the wrong service name and version.

  • classLoader: the class loader that should be used to set the service name and version

Transaction setLabel(String key, value) [1.5.0 as addLabel] Added in 1.5.0 as addLabel. 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}

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.

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.setLabel("foo", "bar");
  • String key: The tag key
  • String|Number|boolean value: The tag value

Transaction addCustomContext(String key, value) [1.7.0] Added in 1.7.0.

edit

Custom context is used to add non-indexed, custom contextual information to transactions. Non-indexed means the data is not searchable or aggregatable in Elasticsearch, and you cannot build dashboards on top of the data. However, non-indexed information is useful for other reasons, like providing contextual information to help you quickly debug performance issues or errors.

The value can be a String, Number or boolean.

transaction.addCustomContext("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.

Transaction setUser(String id, String email, String username, String domain) [1.23.0] Added in 1.23.0.

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(), user.getDomain());
  • 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.
  • domain: The user’s domain or null, if not applicable.

String captureException(Exception e)

edit

Captures an exception and reports it to the APM server. Since version 1.14.0 - returns the id of reported error.

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://127.0.0.1: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.

. (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() 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();
}

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

Span startExitSpan(String type, String subtype, String action)

edit

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

Similar to startSpan(String, String, String), but the created span will be used to create a node in the Service Map and a downstream service in the Dependencies Table. The provided subtype will be used as the downstream service name, unless the service.target.type and service.target.name fields are explicitly set through setServiceTarget(String type, String name).

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

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

The result value set through API will have priority over the value that might be set by auto-instrumentation.

Span setOutcome(Outcome outcome) [1.21.0] Added in 1.21.0.

edit

Sets the transaction or span outcome. Use either FAILURE or SUCCESS to indicate success or failure, use UNKNOWN when the outcome can’t be properly known.

  • outcome: transaction or span outcome

Outcome is used to compute error rates between services, using UNKNOWN will not alter those rates. The value set through API will have higher priority over the value that might be set by auto-instrumentation.

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(). 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().

  • 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();
}

Scope activate() 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 API

edit

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() 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 setLabel(String key, value) [1.5.0 as addLabel] Added in 1.5.0 as addLabel.

edit

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

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).

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.setLabel("foo", "bar");
  • String key: The tag key
  • String|Number|boolean value: The tag value

String captureException(Exception e)

edit

Captures an exception and reports it to the APM server. Since version 1.14.0 - returns the id of reported error.

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

Span setDestinationService(String resource) [1.25.0] Added in 1.25.0.

edit

Provides a way to manually set the span’s destination.service.resource field, which is used for the construction of service maps and the identification of downstream services. Any value set through this method will take precedence over the automatically inferred one. Using null or empty resource string will result in the omission of this field from the span context.

Span setNonDiscardable() [1.32.0] Added in 1.32.0.

edit

Makes this span non-discardable. In some cases, spans may be discarded, for example if span_min_duration ( [1.16.0] Added in 1.16.0. ) is set and the span does not exceed the configured threshold. Use this method to make sure the current span is not discarded.

making a span non-discardable implicitly makes the entire stack of active spans non-discardable as well. Child spans can still be discarded.

Span setDestinationAddress(String address, int port) [1.25.0] Added in 1.25.0.

edit

Provides a way to manually set the span’s destination.address and destination.port fields. Values set through this method will take precedence over the automatically discovered ones. Using null or empty address or non-positive port will result in the omission of the corresponding field from the span context.

Span setServiceTarget(String type, String name) [1.32.0] Added in 1.32.0.

edit

Provides a way to manually set the span service.target.type and service.target.name fields. Values set through this method will take precedence over the automatically discovered ones. Using null or empty string values will result in the omission of those fields from the span context.

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().

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().

  • 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 span.

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.

. (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() 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();
}

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

Span startExitSpan(String type, String subtype, String action)

edit

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

Similar to startSpan(String, String, String), but the created span will be used to create a node in the Service Map and a downstream service in the Dependencies Table. The provided subtype will be used as the downstream service name, unless the service.target.type and service.target.name fields are explicitly set through setServiceTarget(String type, String name).

If invoked on a span which is already an exit span, this method will return a noop span.

Span startSpan()

edit

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

It is important to call void end() 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();
}

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

Calling any of the span’s methods after void end() 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() and the Span setLabel(String key, value) [1.5.0 as addLabel] Added in 1.5.0 as addLabel. 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();
    }
}