簡體   English   中英

Python。 prometheus_client ValueError:儀表指標缺少標簽值

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

我嘗試使用 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 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

我需要 3 個指標。 也許更多。

如果我刪除裝飾器,我的代碼可以工作,但我沒有更新值。

請幫忙。

謝謝你。

解決了!

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