prometheus / client_java

Prometheus instrumentation library for JVM applications
http://prometheus.github.io/client_java/
Apache License 2.0
2.18k stars 798 forks source link

Cannot set labels when exporting Dropwisard metrics #858

Open tashoyan opened 1 year ago

tashoyan commented 1 year ago

I use DropwizardExports to publish Dropwisard metrics to Prometheus PushGateway. I need to set custom labels for the exported metrics (e.g. environment details). The API does not allow to configure labels. When creating samples for Prometheus metrics, labels are always set to empty lists:

MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "", new ArrayList<String>(), new ArrayList<String>(),
                new Long(counter.getCount()).doubleValue());

It would be good to add a new constructor argument (or setter method), taking Map<String, String> - a map of label names and values. These labels will be applied to all Prometheus metrics.

fstab commented 1 year ago

Hi @tashoyan, thanks for reaching out.

We are currently working on the 1.0.0 release of client_java, which will include a complete rewrite of the low level data model (replacing things like the current MetricFamilySamples).

Good news: I agree in some cases it's useful to have common labels for a set of Prometheus metrics, and for that reason the new implementation has a constLabels field in the Metric base class (it's on the 1.0.x feature branch).

However, it will take a while until we port the Dropwizard adapter to the new model. The current focus is on HTTPServer (for jmx_exporter), built-in JVM metrics, and a backwards-compatibility layer for the existing data model.

Once we got release 1.0.0 out (plan is end of September) there will be time to look at other modules like Dropwizard support.