【问题标题】:Python. prometheus_client ValueError: gauge metric is missing label valuesPython。 prometheus_client ValueError:仪表度量缺少标签值
【发布时间】:2021-02-11 06:34:19
【问题描述】:

我尝试使用 prometheus_client 导出 RabbitMQ 指标。我对装饰器函数有疑问。

#!/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()

我有一条消息:

andrey@xps:~/prj/python3/rmq$ ./prj2.py 回溯(最近一次通话最后): 文件“./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() _raise_if_not_observable 中的文件“/usr/local/lib/python3.8/dist-packages/prometheus_client/metrics.py”,第 66 行 raise ValueError('%s 度量缺少标签值' % str(self._type)) ValueError:仪表度量缺少标签值

我需要 3 个指标。也许更多。

如果我删除装饰器,我的代码可以工作,但我没有更新值。

请帮忙。

谢谢。

【问题讨论】:

    标签: python python-3.x prometheus


    【解决方案1】:

    解决了!

    #!/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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多