prometheus_client with labels is not working

1,730 views
Skip to first unread message

Rahul Sinha

unread,
Jun 2, 2021, 5:04:04 AM6/2/21
to Prometheus Users
Code ::

from prometheus_client import start_http_server, Gauge
import random
from random import randrange

#Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message')
Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message', labelnames=['partition'])

@Latency_Time.track_inprogress()
def latency_request(latency):
     Latency_Time.labels('partition', '1').set(latency)
  # Latency_Time.set(latency)

if __name__ == '__main__':
start_http_server(17500)
while True:
latency_request(random.randrange(1900, 2150))


Error Message::

/Users/rahulsinha/anaconda3/envs/envpy37/bin/python /Users/rahulsinha/PycharmProjects/pythonProject/prom_pythonclient.py
Traceback (most recent call last):
  File "/Users/rahulsinha/PycharmProjects/pythonProject/prom_pythonclient.py", line 8, in <module>
    @Latency_Time.track_inprogress()
  File "/Users/rahulsinha/anaconda3/envs/envpy37/lib/python3.7/site-packages/prometheus_client/metrics.py", line 377, in track_inprogress
    self._raise_if_not_observable()
  File "/Users/rahulsinha/anaconda3/envs/envpy37/lib/python3.7/site-packages/prometheus_client/metrics.py", line 66, in _raise_if_not_observable
    raise ValueError('%s metric is missing label values' % str(self._type))
ValueError: gauge metric is missing label values

Process finished with exit code 1

Rahul Sinha

unread,
Jun 2, 2021, 6:59:13 PM6/2/21
to Prometheus Users
Code ::

from prometheus_client import start_http_server, Gauge
import random
from random import randrange

#Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message')    # This code works without label

Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message', labelnames=['partition'])

@Latency_Time.track_inprogress()
def latency_request(latency):
     Latency_Time.labels('partition', '1').set(latency). # I know there is something wrong here.. I'm not able to figure out what !
   # Latency_Time.set(latency)                           # This code works without label


if __name__ == '__main__':
     start_http_server(17500)
     while True:
          latency_request(random.randrange(1900, 2150))

Chris Marchbanks

unread,
Jun 4, 2021, 3:33:08 PM6/4/21
to Rahul Sinha, Prometheus Users
Hello,

Which labelname a labelvalue is assigned to is based on the order of
the arguments, so you do not need to re-specify 'partition' when
setting the labels. Just Latency_Time.labels('1').set(latency) should
work for you.

Cheers,

Chris
> --
> You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/2e844b18-8671-4f20-bc4d-721f432d703an%40googlegroups.com.

Rahul Sinha

unread,
Jun 4, 2021, 9:53:03 PM6/4/21
to Prometheus Users
Hi Chris,

Thank you for your reply. Kindly see comments highlighted in yellow. Please advice me the changes required inside the function.

Appreciate your help.

from prometheus_client import start_http_server, Gauge
import random
from random import randrange

#Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message') # This code works without label

Latency_Time = Gauge('consumer_gauge_seconds', 'Time spent in consuming message', labelnames=['partition'])
# Latency_Time.labels(1).set(2300) # Thanks. It works here.


@Latency_Time.track_inprogress()
def latency_request(partition, latency):
    Latency_Time.labels(partition).set(latency) # Same code doesn't works here. Kindly help

#   Latency_Time.labels('partition', '1').set(latency). # I know there is something wrong here.. I'm not able to figure out what !
# # Latency_Time.set(latency) # This code works without label


if __name__ == '__main__':
    start_http_server(17500)
    while True:
        latency_request(random.randrange(1, 12), random.randrange(1900, 2150))


Error ::

/Users/rahulsinha/anaconda3/envs/envpy37/bin/python /Users/rahulsinha/PycharmProjects/pythonProject/venv/prom_py1.py
Traceback (most recent call last):
  File "/Users/rahulsinha/PycharmProjects/pythonProject/venv/prom_py1.py", line 10, in <module>
    @Latency_Time.track_inprogress()
  File "/Users/rahulsinha/anaconda3/envs/envpy37/lib/python3.7/site-packages/prometheus_client/metrics.py", line 377, in track_inprogress
    self._raise_if_not_observable()
  File "/Users/rahulsinha/anaconda3/envs/envpy37/lib/python3.7/site-packages/prometheus_client/metrics.py", line 66, in _raise_if_not_observable
    raise ValueError('%s metric is missing label values' % str(self._type))
ValueError: gauge metric is missing label values




Thank You.

Regards,
Rahul Sinha

Reply all
Reply to author
Forward
0 new messages