Class DmxTracing

java.lang.Object
dmx.fun.tracing.DmxTracing

@NullMarked public final class DmxTracing extends Object
dmx-fun adapter for Micrometer Tracing.

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 name argument.
  • outcome tag — "success" or "failure".
  • exception tag — 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 Details

    • of

      public static DmxTracing of(io.micrometer.tracing.Tracer tracer)
      Creates an instance bound to the given Tracer.

      Warning: uses getClass().getSimpleName() as the exception tag — an unsafe default in production where arbitrary exceptions may appear. Prefer of(Tracer, Function) with an explicit classifier.

      Parameters:
      tracer - the tracer to open spans with; must not be null
      Returns:
      a new DmxTracing bound to the given tracer
    • of

      public static DmxTracing of(io.micrometer.tracing.Tracer tracer, Function<Throwable, String> exceptionClassifier)
      Creates an instance bound to the given Tracer and exception classifier.

      The exceptionClassifier maps each failure Throwable to the value written to the exception span 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 be null
      exceptionClassifier - maps a failure cause to its exception tag value; must not be null, should return bounded values
      Returns:
      a new DmxTracing bound to the given tracer and classifier
    • traceTry

      public <V> Try<V> traceTry(String name, CheckedSupplier<V> supplier)
      Executes supplier inside a new span named name.

      The span is tagged with outcome=success on success, or outcome=failure plus exception=<classifier result> (the value returned by exceptionClassifier.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 be null
      supplier - the operation to execute; must not be null
      Returns:
      Success(value) on success, Failure(cause) on any exception
    • traceResult

      public <V> Result<V, Throwable> traceResult(String name, CheckedSupplier<V> supplier)
      Executes supplier inside a new span named name.

      Equivalent to traceTry(String, CheckedSupplier) converted to a Result.

      Type Parameters:
      V - the value type returned on success
      Parameters:
      name - the span name; must not be null
      supplier - the operation to execute; must not be null
      Returns:
      Ok(value) on success, Err(cause) on any exception