0

So I tried to use Micrometer on Spring Boot 2.X to log metrics to Prometheus. I added the dependency into gradle:

implementation 'io.micrometer:micrometer-registry-prometheus:latest.release'

and then I tried to add any Metrics like:

CompositeMeterRegistry compositeRegistry = new CompositeMeterRegistry();
PrometheusMeterRegistry prometheusMeterRegistry =
    new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
compositeRegistry.add(prometheusMeterRegistry);
long start = System.currentTimeMillis();

doSomething();

long duration = System.currentTimeMillis() - start;

Timer timer =
    Timer.builder("test.times")
        .publishPercentiles(0.5, 0.75, 0.95)
        .maximumExpectedValue(Duration.ofSeconds(15))
        .minimumExpectedValue(Duration.ofSeconds(15))
        .register(compositeRegistry);
timer.record(duration, TimeUnit.MILLISECONDS);
compositeRegistry.timer("test.times.new").record(duration, TimeUnit.MILLISECONDS);
AtomicInteger test = compositeRegistry.gauge("custom.gauge", new AtomicInteger(8));
test.set(30);
Counter counter = compositeRegistry.counter("custom.counter");
counter.increment();

when I go to localhost:port/actuator/prometheus i can see some metrics like:

# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Metadata GC Threshold",} 0.045
jvm_gc_pause_seconds_count{action="end of minor GC",cause="G1 Evacuation Pause",} 2.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="G1 Evacuation Pause",} 0.036

but there is nothing about the Timer, Gauge or Counter I added. I debugged myself through the code and I can see that it is executed and when i look into the compositeRegistry-Object, i can see that something is available even if I dont see any result of any metrics in the values:

enter image description here

Am I missing something I need to configure? Or any implementation problems?

2 Answers 2

1

You need to inject the meter registry so the registry you are using is the same as the one used in the Prometheus endpoint

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide an example?
0

Okay I solved the Problem now, but maybe not the best way. I removed that CompositeMeterRegistry and added the Timer directly to the globalRegistry:

Timer timer = Timer.builder(name)
                   .publishPercentiles(0.5,0.75,0.95)
                   .register(Metrics.globalRegistry);
timer.record(1L, TimeUnit.SECONDS);

this shows the information I want in the Prometheus endpoint aswell as in JConsole.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.