Class DmxTracing
Instruments Try and Result executions automatically, opening
a named span around each call and recording an outcome without manual span management:
DmxTracing dmx = DmxTracing.of(tracer); // io.micrometer.tracing.Tracer
Try<Response> result = dmx.traceTry("http.client.get",
() -> httpClient.get(url)
);
Signals recorded per call
- Span named after the
nameargument. outcometag —"success"or"failure".exceptiontag — classifier label of the cause (failure only).- Span marked as error via
Span.error(Throwable)(failure only).
Exception tag cardinality
By default, the exception tag uses getClass().getSimpleName(), which is
unbounded when arbitrary third-party exceptions can appear at runtime. In
production, supply an explicit exceptionClassifier via
of(Tracer, Function) that maps every reachable exception type to one of a small,
fixed set of labels:
DmxTracing dmx = DmxTracing.of(tracer, cause ->
switch (cause) {
case IOException _ -> "io";
case TimeoutException _ -> "timeout";
default -> "other";
}
);
For a fluent builder alternative see DmxTraced.
Requires micrometer-tracing on the classpath at runtime, plus a backend
bridge (micrometer-tracing-bridge-otel or micrometer-tracing-bridge-brave).
Spring Boot autoconfigures a Tracer bean when either bridge is present.
-
Method Summary
Modifier and TypeMethodDescriptionstatic DmxTracingof(io.micrometer.tracing.Tracer tracer) Creates an instance bound to the givenTracer.static DmxTracingCreates an instance bound to the givenTracerand exception classifier.traceResult(String name, CheckedSupplier<V> supplier) Executessupplierinside a new span namedname.<V> Try<V> traceTry(String name, CheckedSupplier<V> supplier) Executessupplierinside a new span namedname.
-
Method Details
-
of
Creates an instance bound to the givenTracer.Warning: uses
getClass().getSimpleName()as theexceptiontag — an unsafe default in production where arbitrary exceptions may appear. Preferof(Tracer, Function)with an explicit classifier.- Parameters:
tracer- the tracer to open spans with; must not benull- Returns:
- a new
DmxTracingbound to the given tracer
-
of
public static DmxTracing of(io.micrometer.tracing.Tracer tracer, Function<Throwable, String> exceptionClassifier) Creates an instance bound to the givenTracerand exception classifier.The
exceptionClassifiermaps each failureThrowableto the value written to theexceptionspan tag. It should return a value from a small, bounded set to keep tag cardinality predictable in tracing backends.- Parameters:
tracer- the tracer to open spans with; must not benullexceptionClassifier- maps a failure cause to itsexceptiontag value; must not be null, should return bounded values- Returns:
- a new
DmxTracingbound to the given tracer and classifier
-
traceTry
Executessupplierinside a new span namedname.The span is tagged with
outcome=successon success, oroutcome=failureplusexception=<classifier result>(the value returned byexceptionClassifier.apply(cause)) and marked as error on failure. The span is always ended before this method returns.- Type Parameters:
V- the value type returned on success- Parameters:
name- the span name; must not benullsupplier- the operation to execute; must not benull- Returns:
Success(value)on success,Failure(cause)on any exception
-
traceResult
Executessupplierinside a new span namedname.Equivalent to
traceTry(String, CheckedSupplier)converted to aResult.- Type Parameters:
V- the value type returned on success- Parameters:
name- the span name; must not benullsupplier- the operation to execute; must not benull- Returns:
Ok(value)on success,Err(cause)on any exception
-