2

I have a prometheus metric with labels declared like

errors_total = prometheus_client.Counter("errors_total", "Total errors", ["source", "code])
errors_total.labels("source"="initialization", code="100")
errors_total.labels("source"="shutingdown", code="200")  

When I increment the metric in the place in the monitored code where the error happens, can I just use it as:

errors_total.labels(source="initialization").inc()

or

errors_total.labels(code="200").inc()

My question is can I just use one label when incrementing the metric?

user2275693
  • 173
  • 2
  • 9

1 Answers1

3

No, you have to give a value for each of the labels. If you try the code, you'll get an exception:

>>> c.labels(source="shutingdown").inc()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 146, in labels
    raise ValueError('Incorrect label names')
ValueError: Incorrect label names
>>> c.labels(["shutingdown"]).inc()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/anemyte/.local/lib/python3.7/site-packages/prometheus_client/metrics.py", line 150, in labels
    raise ValueError('Incorrect label count')
ValueError: Incorrect label count

If you have an event with no value for a label, you can pass a placeholder value. A dash ("-") is the best option as it only takes just one byte, but you can also do with something like "undefined", "none", or "other", depending on the context.

anemyte
  • 17,618
  • 1
  • 24
  • 45