Class DmxObserved

java.lang.Object
dmx.fun.observation.DmxObserved

@NullMarked public final class DmxObserved extends Object
Fluent builder for observed dmx-fun operations.

Provides a chainable alternative to DmxObservation when the observation name and registry are configured at different points:

DmxObserved.of("payment.charge")
    .registry(observationRegistry)
    .observeTry(() -> stripe.charge(amount));

The same signals are recorded as with DmxObservation.observeTry(String, CheckedSupplier): the observation is named and tagged with outcome, and on failure the exception key is set and the observation is marked as error.

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

This builder is mutable and not thread-safe; do not share one instance across concurrent per-call reconfigurations.

  • Method Details

    • of

      public static DmxObserved of(String name)
      Creates a builder for the given observation name.
      Parameters:
      name - the observation name; must not be null
      Returns:
      a new DmxObserved builder
    • registry

      public DmxObserved registry(io.micrometer.observation.ObservationRegistry registry)
      Sets the ObservationRegistry to create observations with.
      Parameters:
      registry - the registry to use; must not be null
      Returns:
      this builder
    • exceptionClassifier

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

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

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

      public <V> Try<V> observeTry(CheckedSupplier<V> supplier)
      Executes supplier inside a new observation.
      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(ObservationRegistry) was not set
    • observeResult

      public <V> Result<V, Throwable> observeResult(CheckedSupplier<V> supplier)
      Executes supplier inside a new observation.
      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(ObservationRegistry) was not set