简体   繁体   English

Python。 prometheus_client ValueError:仪表指标缺少标签值

[英]Python. prometheus_client ValueError: gauge metric is missing label values

I try to use prometheus_client for export RabbitMQ metrics.我尝试使用 prometheus_client 导出 RabbitMQ 指标。 I have a problem with decorator functions.我有装饰器功能的问题。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#from prometheus_client import start_http_server, Summary
import prometheus_client as prom
import random
import time

import pika
queue_name = [
 "capt",
 "dev-capt",
 "myBeautifullTest"
]

def get_metric(qname):
  queue_descriptor = channel.queue_declare(qname, durable=True)
  queue_len = queue_descriptor.method.message_count
  return float(queue_len)


params = pika.ConnectionParameters(
    host='rabbitmq1.local',
    port=5672,
    credentials=pika.credentials.PlainCredentials('guest11', 'guest22'),
)

connection = pika.BlockingConnection(parameters=params)
channel = connection.channel()

i = prom.Info("RMQPE", "RabbitMQ Prometheus Exporter")
i.info({'version': '0.0.1'})

# Create a metric to track time spent and requests made.
REQUEST_TIME = prom.Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request():
    """A dummy function that takes some time."""
    time.sleep(1)

RABBIT_QUEUE = prom.Gauge('rabbitmq_test_exporter', 'queue_length' , ['queue_name'], multiprocess_mode = 'all')
for qname in queue_name:
    queue_descriptor = channel.queue_declare(qname, durable=True)
    queue_len = queue_descriptor.method.message_count
    RABBIT_QUEUE.labels(qname).set(queue_len)

@RABBIT_QUEUE.track_inprogress()
def f():
  pass

with RABBIT_QUEUE.track_inprogress():
  pass



if __name__ == '__main__':
    # Start up the server to expose the metrics.
    prom.start_http_server(27015)  # Yes, CS port :)
    # Generate some requests.
    while True:
        process_request()
        f()

I have a message:我有一条消息:

andrey@xps:~/prj/python3/rmq$ ./prj2.py Traceback (most recent call last): File "./prj2.py", line 56, in @RABBIT_QUEUE.track_inprogress() File "/usr/local/lib/python3.8/dist-packages/prometheus_client/metrics.py", line 372, in track_inprogress self._raise_if_not_observable() File "/usr/local/lib/python3.8/dist-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 andrey@xps:~/prj/python3/rmq$ ./prj2.py Traceback(最近一次调用):文件“./prj2.py”,第 56 行,@RABBIT_QUEUE.track_inprogress() 文件“/usr/local /lib/python3.8/dist-packages/prometheus_client/metrics.py”,第 372 行,在 track_inprogress self._raise_if_not_observable() 文件“/usr/local/lib/python3.8/dist-packages/prometheus_client/metrics.py ",第 66 行,在 _raise_if_not_observable 中引发 ValueError('%s metric is missing label values' % str(self._type)) ValueError: Gauge metric is missing label values

I need 3 metrics.我需要 3 个指标。 Maybe more.也许更多。

If I remove the decorator, my code is working, but I haven't updated values.如果我删除装饰器,我的代码可以工作,但我没有更新值。

Please help.请帮忙。

Thank you.谢谢你。

SOLVED!解决了!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#from prometheus_client import start_http_server, Summary
import prometheus_client as prom
import random
import time

import pika
queue_name = [
 "capt",
 "dev-capt",
 "myBeautifullTest"
]


params = pika.ConnectionParameters(
    host='rabbitmq1.local',
    port=5672,
    credentials=pika.credentials.PlainCredentials('guest11', 'guest22'),
)

connection = pika.BlockingConnection(parameters=params)
channel = connection.channel()

i = prom.Info("RMQPE", "RabbitMQ Prometheus Exporter")
i.info({'version': '0.0.1'})

# Create a metric to track time spent and requests made.
REQUEST_TIME = prom.Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request():
    time.sleep(1)


if __name__ == '__main__':
    #                          name                        documentation    labelnames
    RABBIT_QUEUE = prom.Gauge('rabbitmq_test_exporter', 'queue_length', labelnames=['queue_name'])

    prom.start_http_server(27015)
    while True:
        process_request()

        for qname in queue_name:
            queue_descriptor = channel.queue_declare(qname, durable=True)
            queue_len = queue_descriptor.method.message_count
            RABBIT_QUEUE.labels(qname).set(queue_len)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM