siimon / prom-client

Prometheus client for node.js
Apache License 2.0
3.09k stars 372 forks source link

Set default labels on a single metric #598

Open bienzaaron opened 10 months ago

bienzaaron commented 10 months ago

I would like to propose an option to be added to each metric type, which would allow specifying default label values for that metric only. Those labels would then be applied to each metric value (unless the value is overridden when recorded, similar to how default labels already work at the client level in prom-client).

It would look something like this:

const counter = new client.Counter({
  name: 'metric_name',
  help: 'metric_help',
  labelNames: ['my_label'],
  defaultLabels: {
    my_label: 'my_value'
  }
});

counter.inc(); // recorded with my_label: my_value
counter.inc({ my_label: 'my_other_value' }); // recorded with my_label: my_other_value

If this is a valid use case / not already possible, I would love to contribute.

More use case details: My team uses prom-client to expose metrics which are scraped by prometheus and ultimately pushed to newrelic via a prometheus remote write integration.

We have an issue occasionally where if a metric is created that does not follow the naming convention expected by newrelic, newrelic interprets the metric as an inappropriate type (most often, counters are interpreted as gauges). I'd like to use this API to add the newrelic_metric_type label in a universal way to prevent this.

zbjornson commented 9 months ago

It looks like 9e49ee6378e8e1632321ef401db827df24291c2b started to pave the way for this: https://github.com/siimon/prom-client/blob/2ffd316444e08b16517e8172e76e93c0a52c63de/lib/counter.js#L18-L22

Happy to review a PR if you want to wire up incWithoutExemplar() to use defaultLabels.