Class DmxMetered

java.lang.Object
dmx.fun.micrometer.DmxMetered

@NullMarked public final class DmxMetered extends Object
Fluent builder for instrumented dmx-fun operations.

Provides a chainable alternative to DmxMicrometer when the metric name, tags, and registry are configured at different points:

DmxMetered.of("payment.charge")
    .tags(Tags.of("provider", "stripe"))
    .registry(registry)
    .recordTry(() -> stripe.charge(amount));

The same metrics are recorded as with DmxMicrometer.recordTry(String, Tags, CheckedSupplier): {name}.count, {name}.duration, and {name}.failure. This builder is mutable and not thread-safe; avoid sharing one instance for concurrent per-call reconfiguration.

Use exceptionClassifier to control the exception tag value and satisfy Micrometer's low-cardinality contract in production. See DmxMicrometer.of(MeterRegistry, java.util.function.Function) for details.

  • Method Details

    • of

      public static DmxMetered of(String name)
      Creates a builder for the given metric name.
      Parameters:
      name - the base metric name; must not be null
      Returns:
      a new DmxMetered builder
    • tags

      public DmxMetered tags(io.micrometer.core.instrument.Tags tags)
      Sets the tags to attach to all metrics for this operation.
      Parameters:
      tags - the tags to apply; must not be null
      Returns:
      this builder
    • registry

      public DmxMetered registry(io.micrometer.core.instrument.MeterRegistry registry)
      Sets the MeterRegistry to register metrics with.
      Parameters:
      registry - the registry to use; must not be null
      Returns:
      this builder
    • exceptionClassifier

      public DmxMetered exceptionClassifier(Function<Throwable, String> classifier)
      Sets the function that maps each failure cause to its exception tag value.

      The classifier must return a value from a small, bounded set to satisfy Micrometer's low-cardinality requirement. When not set, defaults to getClass().getSimpleName() — an unsafe default in production.

      Parameters:
      classifier - maps a failure cause to its exception tag value; must not be null, must return bounded values
      Returns:
      this builder
    • recordTry

      public <V> Try<V> recordTry(CheckedSupplier<V> supplier)
      Executes the supplier and records metrics.
      Type Parameters:
      V - the value type returned on success
      Parameters:
      supplier - the operation to execute; must not be null
      Returns:
      Success(value) on success, Failure(cause) on any exception
      Throws:
      IllegalStateException - if registry(MeterRegistry) was not set
    • recordResult

      public <V> Result<V, Throwable> recordResult(CheckedSupplier<V> supplier)
      Executes the supplier and records metrics.
      Type Parameters:
      V - the value type returned on success
      Parameters:
      supplier - the operation to execute; must not be null
      Returns:
      Ok(value) on success, Err(cause) on any exception
      Throws:
      IllegalStateException - if registry(MeterRegistry) was not set