- java.lang.Object
-
- co.elastic.apm.api.AbstractSpanImpl
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Scopeactivate()Makes this span the active span on the current thread untilScope.close()has been called.StringcaptureException(Throwable throwable)Captures an exception and reports it to the APM server.SpancreateSpan()NOTE: THIS METHOD IS DEPRECATED AND WILL BE REMOVED IN VERSION 2.0.voiddoSetStartTimestamp(long epochMicros)voidend()Ends the span and schedules it to be reported to the APM Server.voidend(long epochMicros)Ends the span and schedules it to be reported to the APM Server.StringgetId()Returns the id of this span (nevernull)StringgetTraceId()Returns the id of this trace (nevernull)voidinjectTraceHeaders(HeaderInjector headerInjector)Allows for manual propagation of the tracing headers.booleanisSampled()Returns true if this span is recorded and sent to the APM ServerSpanstartSpan()Start and return a new custom span with no type, as a child of this span.SpanstartSpan(String type, String subtype, String action)Start and return a new typed custom span as a child of this span.
-
-
-
Method Detail
-
createSpan
@Nonnull public Span createSpan()
Description copied from interface:SpanNOTE: 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
-
startSpan
@Nonnull public Span startSpan(String type, @Nullable String subtype, @Nullable String action)
Description copied from interface:SpanStart and return a new typed custom span as a child of this span.It is important to call
Span.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(); }NOTE: Spans created via this method can not be retrieved by calling
ElasticApm.currentSpan(). SeeSpan.activate()on how to achieve that.The type, subtype and action strings are used to group similar spans together, with increasing 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 the above 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.
-
startSpan
@Nonnull public Span startSpan()
Description copied from interface:SpanStart and return a new custom span with no type, as a child of this span.It is important to call
Span.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(); }NOTE: Spans created via this method can not be retrieved by calling
ElasticApm.currentSpan(). SeeSpan.activate()on how to achieve that.
-
doSetStartTimestamp
public void doSetStartTimestamp(long epochMicros)
-
end
public void end()
Description copied from interface:SpanEnds 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 andSpan.startSpan().
-
end
public void end(long epochMicros)
Description copied from interface:SpanEnds 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 andSpan.startSpan().
-
captureException
public String captureException(Throwable throwable)
Description copied from interface:SpanCaptures an exception and reports it to the APM server.- Specified by:
captureExceptionin interfaceSpan- Parameters:
throwable- the exception to report- Returns:
- the id of reported error
-
getId
@Nonnull public String getId()
Description copied from interface:SpanReturns the id of this span (nevernull)If this span represents a noop, this method returns an empty string.
-
getTraceId
@Nonnull public String getTraceId()
Description copied from interface:SpanReturns the id of this trace (nevernull)The trace-ID is consistent across all transactions and spans which belong to the same logical trace, even for 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.
- Specified by:
getTraceIdin interfaceSpan- Returns:
- the id of this span (never
null)
-
activate
public Scope activate()
Description copied from interface:SpanMakes this span the active span 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:
Span span = parent.startSpan(); // 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"); span.setType("db.mysql.query"); // do your thing... } catch (Exception e) { span.captureException(e); throw e; } finally { span.end(); }Note:
Span.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
-
isSampled
public boolean isSampled()
Description copied from interface:SpanReturns true if this span is recorded and sent to the APM Server
-
injectTraceHeaders
public void injectTraceHeaders(HeaderInjector headerInjector)
Description copied from interface:SpanAllows 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(); } }- Specified by:
injectTraceHeadersin interfaceSpan- Parameters:
headerInjector- tells the agent how to inject a header into the request object
-
-