Record Class TraceContext
- Record Components:
traceId- 32 lowercase hexadecimal digits, not all zeroparentId- 16 lowercase hexadecimal digits, not all zero: the caller's spanflags- trace flags, 0 to 255; bit 0 is "sampled"traceState- vendor-specific members to propagate;TraceState.EMPTYwhen the caller sent none
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe request header that carries the trace context.static final StringThe response header written byresponseHeader(). -
Constructor Summary
ConstructorsConstructorDescriptionTraceContext(String traceId, String parentId, int flags) Creates a context without trace state.TraceContext(String traceId, String parentId, int flags, TraceState traceState) Validates the components. -
Method Summary
Modifier and TypeMethodDescriptionchild()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.static Stringcorrelation(String requestId, Request request) Likecorrelation(String, Optional), reading the trace headers of a request.static Stringcorrelation(String requestId, Optional<TraceContext> trace) Describes a request for a log line: its framework ID, followed bytrace=<id>when the caller sent a validtraceparent.final booleanIndicates whether some other object is "equal to" this one.intflags()Returns the value of theflagsrecord component.final inthashCode()Returns a hash code value for this object.headers()The headers that continue this trace on an outgoing call:traceparent, andtracestatewhen there are members.parentId()Returns the value of theparentIdrecord component.static Optional<TraceContext> Parses atraceparentheader value strictly.static Optional<TraceContext> Parses atraceparentheader and the accompanyingtracestate.static MiddlewareReturns a middleware that, for requests with a validtraceparent, adds atraceresponseheader to the response: the same format astraceparent, 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.booleansampled()Reports whether the caller recorded this trace.final StringtoString()Returns a string representation of this record class.traceId()Returns the value of thetraceIdrecord component.Formats the context as atraceparentvalue.Returns the value of thetraceStaterecord component.
-
Field Details
-
HEADER
The request header that carries the trace context.- See Also:
-
RESPONSE_HEADER
The response header written byresponseHeader().- See Also:
-
-
Constructor Details
-
TraceContext
Creates a context without trace state.- Parameters:
traceId- 32 lowercase hexadecimal digits, not all zeroparentId- 16 lowercase hexadecimal digits, not all zeroflags- trace flags, 0 to 255- Throws:
IllegalArgumentException- as for the canonical constructor
-
TraceContext
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
Parses atraceparentheader and the accompanyingtracestate. An invalid or absenttracestateleaves the trace state empty; an invalidtraceparentdiscards both.- Parameters:
traceparent- thetraceparentvalue, or nulltracestate- thetracestatevalue, or null- Returns:
- the trace context, or empty if
traceparentis absent or not valid
-
parse
Parses atraceparentheader 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
Formats the context as atraceparentvalue.- Returns:
- version 00 header value
-
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. Sendheaders()of the result.- Returns:
- a child context
-
headers
The headers that continue this trace on an outgoing call:traceparent, andtracestatewhen there are members. Usually called onchild().- Returns:
- the header names and values
-
responseHeader
Returns a middleware that, for requests with a validtraceparent, adds atraceresponseheader to the response: the same format astraceparent, 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 validtraceparentget no header, since Axiom starts no traces of its own. Register it withapp.use(...). Like all middleware it does not decorate responses made from exceptions (seeMiddleware).- Returns:
- the opt-in middleware
-
correlation
Describes a request for a log line: its framework ID, followed bytrace=<id>when the caller sent a validtraceparent.- Parameters:
requestId- the framework's request IDtrace- the caller's trace context, if any- Returns:
requestIdorrequestId trace=traceId
-
correlation
Likecorrelation(String, Optional), reading the trace headers of a request.- Parameters:
requestId- the framework's request IDrequest- the request- Returns:
- the description
-
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. -
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. -
equals
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 withObjects::equals(Object,Object); primitive components are compared with '=='. -
traceId
Returns the value of thetraceIdrecord component.- Returns:
- the value of the
traceIdrecord component
-
parentId
Returns the value of theparentIdrecord component.- Returns:
- the value of the
parentIdrecord component
-
flags
public int flags()Returns the value of theflagsrecord component.- Returns:
- the value of the
flagsrecord component
-
traceState
Returns the value of thetraceStaterecord component.- Returns:
- the value of the
traceStaterecord component
-