Martin Schröder
Martin Schröder

Reputation: 4591

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?

Upvotes: 14

Views: 14865

Answers (2)

Dmytro Bagazhkov
Dmytro Bagazhkov

Reputation: 21

I know that the topic is a lil bit of date but still. I maged to solve it like that with Spring Boot Acuator, by using PrometheusMeterRegistry to fetch CollectorRegistry

@Service
public class SomethingService {

    private final PrometheusMeterRegistry prometheusMeterRegistry;
    private final Counter counter;

    public SomethingService(PrometheusMeterRegistry prometheusMeterRegistry) {
        this.prometheusMeterRegistry = prometheusMeterRegistry;
        counter = Counter.build()
                .name("counter_name")
                .help("counter help")
                .labelNames("your_labels")
                .register(prometheusMeterRegistry.getPrometheusRegistry());
    }

    @PostConstruct
    private void init() {
        inc("your_value");
    }

    private void inc(String value) {
       counter.labels(String.valueOf(value)).inc();
    }
}

Upvotes: 2

Martin Schröder
Martin Schröder

Reputation: 4591

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

Upvotes: 26

Related Questions