Class DmxMicrometer
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 withoutcome=success|failure{name}.duration— Timer tagged withoutcome=success|failure{name}.failure— Counter tagged withexception=<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 Summary
Modifier and TypeMethodDescriptionstatic DmxMicrometerof(io.micrometer.core.instrument.MeterRegistry registry) Creates an instance bound to the givenMeterRegistry.static DmxMicrometerof(io.micrometer.core.instrument.MeterRegistry registry, Function<Throwable, String> exceptionClassifier) Creates an instance bound to the givenMeterRegistryand exception classifier.recordResult(String name, io.micrometer.core.instrument.Tags tags, CheckedSupplier<V> supplier) Executes the supplier and records metrics undername.<V> Try<V> recordTry(String name, io.micrometer.core.instrument.Tags tags, CheckedSupplier<V> supplier) Executes the supplier and records metrics undername.
-
Method Details
-
of
Creates an instance bound to the givenMeterRegistry.Warning: uses
getClass().getSimpleName()as theexceptiontag — an unsafe default in production where arbitrary exceptions may appear. Preferof(MeterRegistry, Function)with an explicit classifier.- Parameters:
registry- the registry to record metrics into; must not be null- Returns:
- a new
DmxMicrometerbound 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 givenMeterRegistryand exception classifier.The
exceptionClassifiermaps each failureThrowableto the value written to theexceptiontag. 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 nullexceptionClassifier- maps a failure cause to itsexceptiontag value; must not be null, must return bounded values- Returns:
- a new
DmxMicrometerbound 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 undername.- Type Parameters:
V- the value type returned on success- Parameters:
name- the base metric name; must not be nulltags- additional tags to attach to all metrics; must not be nullsupplier- 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 undername.- Type Parameters:
V- the value type returned on success- Parameters:
name- the base metric name; must not be nulltags- additional tags to attach to all metrics; must not be nullsupplier- the operation to execute; must not be null- Returns:
Ok(value)on success,Err(cause)on any exception
-