josh
josh

Reputation: 14463

How to retrieve metrics from spring-boot micrometer

I was experimenting with micrometer using below code. How do i configure rate aggregation and get rate-aggregated data from a given counter within the application. By rate aggregated i mean data for previous publishing interval ?

final MeterRegistry myRegistry = new SimpleMeterRegistry();
final Random random = new Random();
for (iteration = 0; iteration < 1000; iteration++)
{
    final int randomNum = random.nextInt(10);
    final Counter myCounter = myRegistry.counter("myTestCounter", "random", Integer.toString(randomNum));
    myCounter.increment();
    Thread.sleep(100);
}
System.out.println("measure = [" + myRegistry.counter("myTestCounter", "random", "0").measure());
System.out.println("count = [" + myRegistry.counter("myTestCounter", "random", "0").count());

Note: I dont want to publish metrics to any monitoring system. I would like to track number of requests given tags per interval (e.g., successful/failure requests for a given interval) in application itself and use it for resilience purpose.

Upvotes: 5

Views: 4681

Answers (1)

Johnny Lim
Johnny Lim

Reputation: 5843

Based on the comment from the author of the project, Micrometer intends to export metrics to an external product-ready monitoring system like Prometheus, so your approach is kind of unusual.

Micrometer supports rate-aggregation for monitoring systems which don't support rate-aggregation like InfluxDB and its support comes from StepMeterRegistry. But SimpleMeterRegistry is not one of its subclasses, so you need to create your own one if you want to stick to the approach.

UPDATED:

I created a sample for a custom StepMeterRegistry.

Upvotes: 2

Related Questions