diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java index 8e6c90caa7..dac5fc0b1b 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java @@ -27,11 +27,7 @@ import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; -import java.util.function.BooleanSupplier; -import java.util.function.DoubleSupplier; -import java.util.function.IntSupplier; -import java.util.function.LongSupplier; -import java.util.function.Supplier; +import java.util.function.*; /** * Timer intended to track of a large number of short running events. Example would be @@ -430,6 +426,10 @@ public Builder description(String description) { return super.description(description); } + public Function with(MeterRegistry registry) { + return extraTags -> register(registry, tags.and(extraTags)); + } + /** * Add the timer to a single registry, or return an existing timer in that * registry. The returned timer will be unique for each registry, but each @@ -439,6 +439,10 @@ public Builder description(String description) { * @return A new or existing timer. */ public Timer register(MeterRegistry registry) { + return register(registry, tags); + } + + private Timer register(MeterRegistry registry, Tags tags) { // the base unit for a timer will be determined by the monitoring system // implementation return registry.timer(new Meter.Id(name, tags, null, description, Type.TIMER), diff --git a/samples/micrometer-samples-core/src/main/java/io/micrometer/core/samples/Demo.java b/samples/micrometer-samples-core/src/main/java/io/micrometer/core/samples/Demo.java new file mode 100644 index 0000000000..5e7f141b8e --- /dev/null +++ b/samples/micrometer-samples-core/src/main/java/io/micrometer/core/samples/Demo.java @@ -0,0 +1,33 @@ +package io.micrometer.core.samples; + +import io.micrometer.core.instrument.Tags; +import io.micrometer.core.instrument.Timer; +import io.micrometer.prometheus.PrometheusConfig; +import io.micrometer.prometheus.PrometheusMeterRegistry; + +import java.time.Duration; +import java.util.function.Function; + +public class Demo { + + private static final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); + + private static final Function timerFactory = Timer.builder("cache.initialization.latency") + .description("Time initialize cache from database") + .tag("static", "abc") + .publishPercentiles(0.99, 0.999) + .publishPercentileHistogram() + .minimumExpectedValue(Duration.ofSeconds(10)) + .maximumExpectedValue(Duration.ofSeconds(600)) + .with(registry); + + public static void main(String[] args) { + doSomething("test"); + } + + private static void doSomething(String cacheName) { + timerFactory.apply(Tags.of("cacheName", cacheName)).record(Duration.ofMillis(100)); + System.out.println(registry.scrape()); + } + +}