Class DmxMicrometer

java.lang.Object
dmx.fun.micrometer.DmxMicrometer

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

Instruments Try and Result executions automatically, recording success/failure counts and execution latency without manual metric tracking:

DmxMicrometer dmx = DmxMicrometer.of(registry);

Try<Response> result = dmx.recordTry("http.client.get",
    Tags.of("endpoint", "/users"),
    () -> httpClient.get(url)
);

Metrics recorded per call

  • {name}.count — Counter tagged with outcome=success|failure
  • {name}.duration — Timer tagged with outcome=success|failure
  • {name}.failure — Counter tagged with exception=<label> (on failure only)

Exception tag cardinality

By default the exception tag uses getClass().getSimpleName(), which is unbounded when arbitrary third-party exceptions appear at runtime — a violation of Micrometer's low-cardinality contract. In production, supply an explicit exceptionClassifier that maps every reachable exception type to one of a small, fixed set of labels:

DmxMicrometer dmx = DmxMicrometer.of(registry, cause ->
    switch (cause) {
        case IOException __          -> "io";
        case TimeoutException __     -> "timeout";
        default                      -> "other";
    }
);

For a fluent builder alternative see DmxMetered.

  • Method Details

    • of

      public static DmxMicrometer of(io.micrometer.core.instrument.MeterRegistry registry)
      Creates an instance bound to the given MeterRegistry.

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

      Parameters:
      registry - the registry to record metrics into; must not be null
      Returns:
      a new DmxMicrometer bound to the given registry
    • of

      public static DmxMicrometer of(io.micrometer.core.instrument.MeterRegistry registry, Function<Throwable, String> exceptionClassifier)
      Creates an instance bound to the given MeterRegistry and exception classifier.

      The exceptionClassifier maps each failure Throwable to the value written to the exception tag. It must return a value from a small, bounded set to satisfy Micrometer's low-cardinality requirement.

      Parameters:
      registry - the registry to record metrics into; must not be null
      exceptionClassifier - maps a failure cause to its exception tag value; must not be null, must return bounded values
      Returns:
      a new DmxMicrometer bound to the given registry and classifier
    • recordTry

      public <V> Try<V> recordTry(String name, io.micrometer.core.instrument.Tags tags, CheckedSupplier<V> supplier)
      Executes the supplier and records metrics under name.
      Type Parameters:
      V - the value type returned on success
      Parameters:
      name - the base metric name; must not be null
      tags - additional tags to attach to all metrics; must not be null
      supplier - the operation to execute; must not be null
      Returns:
      Success(value) on success, Failure(cause) on any exception
    • recordResult

      public <V> Result<V, Throwable> recordResult(String name, io.micrometer.core.instrument.Tags tags, CheckedSupplier<V> supplier)
      Executes the supplier and records metrics under name.
      Type Parameters:
      V - the value type returned on success
      Parameters:
      name - the base metric name; must not be null
      tags - additional tags to attach to all metrics; must not be null
      supplier - the operation to execute; must not be null
      Returns:
      Ok(value) on success, Err(cause) on any exception