package io.vertx.micrometer.impl.meters;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.impl.Labels;
import java.util.concurrent.TimeUnit;
public class Timers {
private final String name;
private final String description;
private final Label[] keys;
private final MeterRegistry registry;
public Timers(String name,
String description,
MeterRegistry registry,
Label... keys) {
this.name = name;
this.description = description;
this.registry = registry;
this.keys = keys;
}
public Timer get(String... values) {
return Timer.builder(name)
.description(description)
.tags(Labels.toTags(keys, values))
.register(registry);
}
public EventTiming start() {
return new EventTiming(this);
}
public static class EventTiming {
private final Timers ref;
private final long nanoStart;
private EventTiming(Timers ref) {
this.ref = ref;
this.nanoStart = System.nanoTime();
}
public void end(String... values) {
Timer t = ref.get(values);
t.record(System.nanoTime() - nanoStart, TimeUnit.NANOSECONDS);
}
}
}