Interface Transaction
- All Superinterfaces:
Span
To get a reference to the current transaction, call ElasticApm.currentTransaction().
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionactivate()Makes this transaction the active transaction on the current thread untilScope.close()has been called.addCustomContext(String key, boolean value) Custom context is used to add non-indexed, custom contextual information to transactions.addCustomContext(String key, Number value) Custom context is used to add non-indexed, custom contextual information to transactions.addCustomContext(String key, String value) Custom context is used to add non-indexed, custom contextual information to transactions.Deprecated.Deprecated.usesetLabel(String, Number)insteadDeprecated.usesetLabel(String, String)insteadDeprecated.useaddLabel(String, String)insteadDeprecated.voidend()End tracking the transaction.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`.getId()Returns the id of this transaction (nevernull)setFrameworkName(String frameworkName) Provides a way to manually set theservice.framework.namefield.Labels are used to add indexed information to transactions, spans, and errors.Labels are used to add indexed information to transactions, spans, and errors.Labels are used to add indexed information to transactions, spans, and errors.Override the name of the current transaction.setOutcome(Outcome outcome) Sets the transaction outcomeA string describing the result of the transaction.setServiceInfo(String serviceName, String serviceVersion) Sets the service name and version for this transaction and its child spans.setStartTimestamp(long epochMicros) Sets the start timestamp of this event.The type of the transaction.Call this to enrich collected performance data and errors with information about the user/client.Call this to enrich collected performance data and errors with information about the user/client.useServiceInfoForClassLoader(ClassLoader classLoader) 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.Methods inherited from interface co.elastic.apm.api.Span
captureException, end, getTraceId, injectTraceHeaders, isSampled, setDestinationAddress, setDestinationService, setNonDiscardable, setServiceTarget, startExitSpan, startSpan, startSpan
-
Field Details
-
TYPE_REQUEST
- See Also:
-
-
Method Details
-
setName
Override the name of the current transaction.For supported frameworks, the transaction name is determined automatically, and can be overridden using this function.
-
setType
The type of the transaction.There’s a special type called
requestwhich is used by the agent for the transactions automatically created when an incoming HTTP request is detected. -
setFrameworkName
Provides a way to manually set theservice.framework.namefield. Any value set through this method will take precedence over automatically-set value for supported frameworks.- Parameters:
frameworkName- The name of the framework.nulland empty values will cause the exclusion of the framework name from the transaction context.
-
setServiceInfo
Sets the service name and version for this transaction and its child spans.NOTE: If this method is called after child spans are already created, they may have the wrong service name and version.
- Parameters:
serviceName- the service nameserviceVersion- the service version
-
useServiceInfoForClassLoader
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.NOTE: If this method is called after child spans are already created, they may have the wrong service name and version.
- Parameters:
classLoader- the class loader that should be used to set the service name and version
-
addTag
Deprecated.useaddLabel(String, String)instead -
addLabel
Deprecated.usesetLabel(String, String)instead -
addLabel
Deprecated.usesetLabel(String, Number)instead -
addLabel
Deprecated.usesetLabel(String, boolean)instead -
setLabel
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(previouslycontext.tagsin stack version< 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}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.
-
setLabel
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(previouslycontext.tagsin stack version< 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.
-
setLabel
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(previouslycontext.tagsin stack version< 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.
-
addCustomContext
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.- Parameters:
key- The custom context key.value- The custom context value.
-
addCustomContext
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.- Parameters:
key- The custom context key.value- The custom context value.
-
addCustomContext
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.- Parameters:
key- The custom context key.value- The custom context value.
-
setUser
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.
- Parameters:
id- The user's id ornull, if not applicable.email- The user's email address ornull, if not applicable.username- The user's name ornull, if not applicable.
-
setUser
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.
- Parameters:
id- The user's id ornull, if not applicable.email- The user's email address ornull, if not applicable.username- The user's name ornull, if not applicable.domain- The user's domain ornull, if not applicable.
-
setResult
A string describing the result of the transaction. This is typically the HTTP status code, or e.g. "success" for a background task- Parameters:
result- a string describing the result of the transaction
-
setStartTimestamp
Description copied from interface:SpanSets the start timestamp of this event.- Specified by:
setStartTimestampin interfaceSpan- Parameters:
epochMicros- the timestamp of when this event happened, in microseconds (µs) since epoch- Returns:
thisfor chaining
-
setOutcome
Sets the transaction outcome- Specified by:
setOutcomein interfaceSpan- Parameters:
outcome-Outcome.SUCCESSto indicate success,Outcome.FAILUREfor failure,Outcome.UNKNOWNto indicate unknown outcome- Returns:
- this
-
end
void end()End tracking the transaction.Should be called e.g. at the end of a request or when ending a background task.
-
createSpan
Deprecated.NOTE: THIS METHOD IS DEPRECATED AND WILL BE REMOVED IN VERSION 2.0. Instead, start a new span throughSpan.startSpan()orSpan.startSpan(String, String, String).- Specified by:
createSpanin interfaceSpan- Returns:
- the started span, never
null
-
getId
Returns the id of this transaction (nevernull)If this transaction represents a noop, this method returns an empty string.
-
ensureParentId
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 pages, 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.
- Returns:
- the parent-ID for this transaction. Updates the transaction to use a new parent-ID if it has previously been unset.
-
activate
Scope activate()Makes this transaction the active transaction on the current thread untilScope.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:
activate()andScope.close()have to be called on the same thread.- Specified by:
activatein interfaceSpan- Returns:
- a scope which has to be
Scope.close()d
-
setLabel(String, boolean)instead