Search code examples
javasummaryprometheus

How to add labels to Prometheus Summary metric in Java


Counters and Gauges allow for labels to be added to them. When I try to add labels to a Summary, I get an "incorrect number of labels" error.

This is what I'm trying:

private static final Summary latencySummary = Summary.build()
            .name("all_latencies")
            .help("all latencies.")
            .register();

latencySummary.labels("xyz_api_latency").observe(timer.elapsedSeconds());

I've looked at the Summary github source code, but can't find the answer. How are labels added to a Summary?


Solution

  • You need to provide the labelname in the metric:

    private static final Summary latencySummary = Summary.build()
        .name("latency_seconds")
        .help("All latencies.")
        .labelNames("api")
        .register();