Where does micrometer store data?

1.5k Views Asked by At

As i am in doubt when we use micrometer and prometheus in production as prometheus pull data form micrometer and we just use remote data storage for prometheus but some data are also stored by micrometer.. now my question is if my server is running in a production than the micrometer store data is keep going increases as it is running or it automatically flushes after some time? means how micrometer store data in production?

2

There are 2 best solutions below

0
On

Micrometer itself does not store data persistently. All data is kept in memory. If the application restarts, the counters starts from zero.

It is the task of the timeline database to hanlde that. E.g. Prometheus has functions like rate() and increase() that ignore these resets.

0
On

No matter which environment your application uses micrometer (whether it is locally, in dev, acceptance or production, micrometer will behave the same way:

  1. Collecting and storing in-memory metrics
  2. Waiting for other metrics analysis and visualization tools to collect the data:
    • Publishing when the tools implementation uses a push model
    • Exposing the needed endpoints for tools using the pull model, which is the Prometheus case

micrometers, and any other metrics collecting library by the way, cannot make assumptions about when the data collected should be flushed or cleared since it cannot make assumptions or even know in advance which tools will be collecting which data and when.

Meanwhile, if you have already full picture about your application architecture and you know that you will be only using Prometheus to collect metrics, you can configure your endpoint to clear the MeterRegistry after successful scraping (based on the official documentation sample since you did not afford any snippet about the implementation):

PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
try {
    HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
    server.createContext("/prometheus", httpExchange -> {
        String response = prometheusRegistry.scrape(); (1)
        httpExchange.sendResponseHeaders(200, response.getBytes().length);
        try (OutputStream os = httpExchange.getResponseBody()) {
            os.write(response.getBytes());
            prometheusRegistry.clear(); // clear the registry upon successful response write
        }
    });
    new Thread(server::start).start();
} catch (IOException e) {
    throw new RuntimeException(e);
}