Class TraceContext

  • All Implemented Interfaces:
    Recyclable

    public class TraceContext
    extends java.lang.Object
    implements Recyclable
    This is an implementation of the w3c traceparent header draft.

    As this is just a draft at the moment, we don't use the official header name but prepend the custom prefix Elastic-Apm-.

    Textual representation (e.g. HTTP header):

     elastic-apm-traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
     (_____________________)  () (______________________________) (______________) ()
                v             v                 v                        v         v
           Header name     Version           Trace-Id                Span-Id     Flags
     

    Binary representation (e.g. 0.11.0.0+ Kafka record header), based on https://github.com/elastic/apm/blob/master/docs/agent-development.md#binary-fields:

          traceparent     = version version_format
          version         = 1BYTE                   ; version is 0 in the current spec
          version_format  = "{ 0x0 }" trace-id "{ 0x1 }" parent-id "{ 0x2 }" trace-flags
          trace-id        = 16BYTES
          parent-id       = 8BYTES
          trace-flags     = 1BYTE  ; only the least significant bit is used
     
    For example:
     elasticapmparent:   [0,
                          0, 75, 249, 47, 53, 119, 179, 77, 166, 163, 206, 146, 157, 0, 14, 71, 54,
                          1, 52, 240, 103, 170, 11, 169, 2, 183,
                          2, 1]
     
    • Field Detail

      • ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME

        public static final java.lang.String ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME
        See Also:
        Constant Field Values
      • W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME

        public static final java.lang.String W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME
        See Also:
        Constant Field Values
      • TRACESTATE_HEADER_NAME

        public static final java.lang.String TRACESTATE_HEADER_NAME
        See Also:
        Constant Field Values
      • TRACE_PARENT_BINARY_HEADER_NAME

        public static final java.lang.String TRACE_PARENT_BINARY_HEADER_NAME
        See Also:
        Constant Field Values
      • BINARY_FORMAT_EXPECTED_LENGTH

        public static final int BINARY_FORMAT_EXPECTED_LENGTH
        See Also:
        Constant Field Values
    • Method Detail

      • containsTraceContextTextHeaders

        public static <C> boolean containsTraceContextTextHeaders​(C carrier,
                                                                  TextHeaderGetter<C> headerGetter)
      • removeTraceContextHeaders

        public static <C> void removeTraceContextHeaders​(C carrier,
                                                         HeaderRemover<C> headerRemover)
      • copyTraceContextTextHeaders

        public static <S,​D> void copyTraceContextTextHeaders​(S source,
                                                                   TextHeaderGetter<S> headerGetter,
                                                                   D destination,
                                                                   TextHeaderSetter<D> headerSetter)
      • with128BitId

        public static TraceContext with128BitId​(ElasticApmTracer tracer)
        Creates a new TraceContext with a 128 bit id, suitable for errors, as those might not have a trace reference and therefore require a larger id in order to be globally unique.
        Parameters:
        tracer - a valid tracer
      • asRootSpan

        public void asRootSpan​(Sampler sampler)
      • asChildOf

        public void asChildOf​(TraceContext parent)
      • resetState

        public void resetState()
        Description copied from interface: Recyclable
        resets pooled object state so it can be reused
        Specified by:
        resetState in interface Recyclable
      • getTraceId

        public Id getTraceId()
        The ID of the whole trace forest
        Returns:
        the trace id
      • getId

        public Id getId()
      • getParentId

        public Id getParentId()
        The ID of the caller span (parent)
        Returns:
        the parent id
      • getTransactionId

        public Id getTransactionId()
      • isSampled

        public boolean isSampled()
        An alias for isRecorded()
        Returns:
        true when this span should be sampled, false otherwise
      • getSampleRate

        public double getSampleRate()
        Returns the sample rate used for this transaction/span between 0.0 and 1.0 or Double.NaN if sample rate is unknown
        Returns:
        sample rate
      • isChildOf

        public boolean isChildOf​(TraceContext other)
      • hasContent

        public boolean hasContent()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isRoot

        public boolean isRoot()
      • getServiceName

        @Nullable
        public java.lang.String getServiceName()
      • setServiceName

        public void setServiceName​(@Nullable
                                   java.lang.String serviceName)
        Overrides the co.elastic.apm.agent.impl.payload.Service#name property sent via the meta data Intake V2 event.
        Parameters:
        serviceName - the service name for this event
      • createSpan

        public Span createSpan()
      • createSpan

        public Span createSpan​(long epochMicros)
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • idEquals

        public boolean idEquals​(@Nullable
                                TraceContext o)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getApplicationClassLoader

        @Nullable
        public java.lang.ClassLoader getApplicationClassLoader()
      • getTraceState

        public TraceState getTraceState()
      • serialize

        public byte[] serialize()
      • serialize

        public void serialize​(byte[] buffer)
      • deserialize

        public void deserialize​(byte[] buffer,
                                @Nullable
                                java.lang.String serviceName)
      • deserializeSpanId

        public static void deserializeSpanId​(Id id,
                                             byte[] buffer)
      • getSpanId

        public static long getSpanId​(byte[] serializedTraceContext)
      • traceIdAndIdEquals

        public boolean traceIdAndIdEquals​(byte[] serialized)
      • getFlags

        public byte getFlags()