Record Class TraceContext

java.lang.Object
java.lang.Record
com.jsgalactic.axiom.observability.TraceContext
Record Components:
traceId - 32 lowercase hexadecimal digits, not all zero
parentId - 16 lowercase hexadecimal digits, not all zero: the caller's span
flags - trace flags, 0 to 255; bit 0 is "sampled"
traceState - vendor-specific members to propagate; TraceState.EMPTY when the caller sent none

public record TraceContext(String traceId, String parentId, int flags, TraceState traceState) extends Record
A W3C Trace Context identity, the traceparent of a request: which distributed trace it belongs to and which caller span started it. It lets logs, metrics exemplars and downstream calls be correlated with the caller's trace. It is propagated information only: Axiom starts no spans and depends on no tracing library.

Parsing is strict. Only version 00 is accepted, in exactly the 55-character form 00-<32 lowercase hex>-<16 lowercase hex>-<2 lowercase hex>, with a nonzero trace id and parent id. Anything else, including uppercase digits, surrounding whitespace, extra fields, future versions and several headers joined into one value, is treated as absent, as the specification allows: the request simply has no trace context. Incoming values are validated but never trusted for anything beyond correlation; a client chooses them.

The caller's tracestate (see TraceState) is carried along and passed on by child(); it is read only together with a valid traceparent, as the specification requires, and an invalid one is dropped without failing the request.

In logs. The framework adds the trace id next to the request ID in its own log messages about a request, when the caller sent a valid traceparent. The id passed validation (32 lowercase hex digits), so it cannot inject log lines, but it is still the caller's choice. Applications get the same text without a thread-local map from Context.correlation(). In responses. responseHeader() is an opt-in middleware that returns the trace id and this service's span in a traceresponse header.

  • Field Details

  • Constructor Details

    • TraceContext

      public TraceContext(String traceId, String parentId, int flags)
      Creates a context without trace state.
      Parameters:
      traceId - 32 lowercase hexadecimal digits, not all zero
      parentId - 16 lowercase hexadecimal digits, not all zero
      flags - trace flags, 0 to 255
      Throws:
      IllegalArgumentException - as for the canonical constructor
    • TraceContext

      public TraceContext(String traceId, String parentId, int flags, TraceState traceState)
      Validates the components.
      Throws:
      IllegalArgumentException - if an id is not lowercase hex of the right length or is all zero, or the flags are out of range
  • Method Details

    • parse

      public static Optional<TraceContext> parse(String traceparent, String tracestate)
      Parses a traceparent header and the accompanying tracestate. An invalid or absent tracestate leaves the trace state empty; an invalid traceparent discards both.
      Parameters:
      traceparent - the traceparent value, or null
      tracestate - the tracestate value, or null
      Returns:
      the trace context, or empty if traceparent is absent or not valid
    • parse

      public static Optional<TraceContext> parse(String header)
      Parses a traceparent header value strictly.
      Parameters:
      header - the header value, or null if the request had none
      Returns:
      the trace context, or empty if the value is absent or not valid
    • sampled

      public boolean sampled()
      Reports whether the caller recorded this trace.
      Returns:
      the sampled flag
    • traceparent

      public String traceparent()
      Formats the context as a traceparent value.
      Returns:
      version 00 header value
    • child

      public TraceContext child()
      Continues the trace for a downstream call: the same trace id, flags and trace state with a new random parent id standing for this service's span. Send headers() of the result.
      Returns:
      a child context
    • headers

      public Map<String,String> headers()
      The headers that continue this trace on an outgoing call: traceparent, and tracestate when there are members. Usually called on child().
      Returns:
      the header names and values
    • responseHeader

      public static Middleware responseHeader()
      Returns a middleware that, for requests with a valid traceparent, adds a traceresponse header to the response: the same format as traceparent, with the caller's trace id and flags and a new span id for this service, so a client can find its request in the server's logs. Requests without a valid traceparent get no header, since Axiom starts no traces of its own. Register it with app.use(...). Like all middleware it does not decorate responses made from exceptions (see Middleware).
      Returns:
      the opt-in middleware
    • correlation

      public static String correlation(String requestId, Optional<TraceContext> trace)
      Describes a request for a log line: its framework ID, followed by trace=<id> when the caller sent a valid traceparent.
      Parameters:
      requestId - the framework's request ID
      trace - the caller's trace context, if any
      Returns:
      requestId or requestId trace=traceId
    • correlation

      public static String correlation(String requestId, Request request)
      Like correlation(String, Optional), reading the trace headers of a request.
      Parameters:
      requestId - the framework's request ID
      request - the request
      Returns:
      the description
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • traceId

      public String traceId()
      Returns the value of the traceId record component.
      Returns:
      the value of the traceId record component
    • parentId

      public String parentId()
      Returns the value of the parentId record component.
      Returns:
      the value of the parentId record component
    • flags

      public int flags()
      Returns the value of the flags record component.
      Returns:
      the value of the flags record component
    • traceState

      public TraceState traceState()
      Returns the value of the traceState record component.
      Returns:
      the value of the traceState record component