prix
prix

Reputation: 101

Spring Boot Micrometer/Prometheus not logging any Metrics I added

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?

Upvotes: 0

Views: 2537

Answers (2)

prix
prix

Reputation: 101

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.

Upvotes: 0

checketts
checketts

Reputation: 14963

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

Upvotes: 1

Related Questions