Search code examples
javaspring-bootmetricsprometheusmicrometer

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?


Solution

  • 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();