Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Micrometer's equivalent of Prometheus' labels

I'm transforming a Spring Boot application from Spring Boot 1 (with the Prometheus Simpleclient) to Spring Boot 2 (which uses Micrometer).

I'm stumped at transforming the labels we have with Spring Boot 1 and Prometheus to concepts in Micrometer. For example (with Prometheus):

private static Counter requestCounter =
  Counter.build()
      .name("sent_requests_total")
      .labelNames("method", "path")
      .help("Total number of rest requests sent")
      .register();
...
requestCounter.labels(request.getMethod().name(), path).inc();

The tags of Micrometer seem to be something different than the labels of Prometheus: All values have to be predeclared, not only the keys.

Can one use Prometheus' labels with Spring (Boot) and Micrometer?

like image 308
Martin Schröder Avatar asked Mar 08 '18 09:03

Martin Schröder


People also ask

What is MeterRegistry?

MeterRegistry In Micrometer, a MeterRegistry is the core component used for registering meters. We can iterate over the registry and further each meter's metrics to generate a time series in the backend with combinations of metrics and their dimension values. The simplest form of the registry is SimpleMeterRegistry.

How do you expose Prometheus metrics in spring boot?

Start your application, and send an HTTP get request to http://localhost:8080/actuator/prometheus . You will see all of the metrics being exposed. If you don't see your custom metric, make sure that you have triggered the code to be executed. The metric won't appear until the timer has recorded at least once.

What is Micrometer registry Prometheus?

Micrometer is lets your code be agnostic to the monitoring hosting solution you use. You could code your metrics in a single solution that could publish it to Prometheus or Azure monitor or Influx or etc. In your case you are using Prometheus as your monitoring solution, hence you might as well use Prometheus client.


1 Answers

Further digging showed that only the keys of micrometer tags have to be predeclared - but the constructor really takes pairs of key/values; the values don't matter. And the keys have to be specified when using the metric.

This works:

private static final String COUNTER_BATCHMANAGER_SENT_REQUESTS = "batchmanager.sent.requests";
private static final String METHOD_TAG = "method";
private static final String PATH_TAG = "path";
private final Counter requestCounter;
...
requestCounter = Counter.builder(COUNTER_BATCHMANAGER_SENT_REQUESTS)
    .description("Total number of rest requests sent")
    .tags(METHOD_TAG, "", PATH_TAG, "")
    .register(meterRegistry);
...
 Metrics.counter(COUNTER_BATCHMANAGER_SENT_REQUESTS, METHOD_TAG, methodName, PATH_TAG, path)
    .increment();
like image 95
Martin Schröder Avatar answered Oct 22 '22 07:10

Martin Schröder