简体   繁体   English

使用 python 读取 Prometheus 指标

[英]Reading Prometheus metric using python

I am trying to read Prometheus metrics (the cpu and memory values) of a POD in kubernetes.我正在尝试读取 Kubernetes 中 POD 的 Prometheus 指标(cpu 和内存值)。 I have Prometheus install and everything is up using local host ' http://localhost:9090/ .我安装了 Prometheus,一切都在使用本地主机' http://localhost:9090/ I used the following code to read the CPU and memory of a pod but I have an error results = response.json()['data']['result'] , No JSON object could be decoded .我使用以下代码读取 Pod 的 CPU 和内存,但出现错误 results = response.json()['data']['result'] , No JSON object could be decoded 。 IS anyone can help please ?有人可以帮忙吗?

import datetime
import time
import requests  

PROMETHEUS = 'http://localhost:9090/'

end_of_month = datetime.datetime.today().replace(day=1).date()

last_day = end_of_month - datetime.timedelta(days=1)
duration = '[' + str(last_day.day) + 'd]'

response = requests.get(PROMETHEUS + '/metrics',
  params={
    'query': 'sum by (job)(increase(process_cpu_seconds_total' + duration + '))',
    'time': time.mktime(end_of_month.timetuple())})
results = response.json()['data']['result']

print('{:%B %Y}:'.format(last_day))
for result in results:
  print(' {metric}: {value[1]}'.format(**result))

The code looks true, However,the query in your response command is wrong .代码看起来是正确的,但是,您的响应命令中的查询是错误的。 the true formate is :真正的甲酸盐是:

response =requests.get(PROMETHEUS + '/api/v1/query', params={'query': 'container_cpu_user_seconds_total'}) 

you can change "container_cpu_user_seconds_total" to any query that you want to read.您可以将“container_cpu_user_seconds_total”更改为您想要阅读的任何查询。 .. good luck .. 祝你好运

Performing a GET request at <prom-server-ip>:9090/metrics returns the Prometheus metrics (not in JSON format) of the Prometheus server itself.<prom-server-ip>:9090/metrics处执行GET请求会返回 Prometheus 服务器本身的 Prometheus 指标(非 JSON 格式)。

Since you're trying to perform query, you need to use the HTTP API endpoints like /api/v1/query or /api/v1/query_range instead of using /metrics .由于您正在尝试执行查询,因此您需要使用 HTTP API 端点,如/api/v1/query/api/v1/query_range而不是使用/metrics

$ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
{
   "status" : "success",
   "data" : {
      "resultType" : "vector",
      "result" : [
         {
            "metric" : {
               "__name__" : "up",
               "job" : "prometheus",
               "instance" : "localhost:9090"
            },
            "value": [ 1435781451.781, "1" ]
         },
         {
            "metric" : {
               "__name__" : "up",
               "job" : "node",
               "instance" : "localhost:9100"
            },
            "value" : [ 1435781451.781, "0" ]
         }
      ]
   }
}

For more detailed information visit Prometheus official docs .有关更多详细信息,请访问Prometheus 官方文档

You are using the wrong endpoint.您正在使用错误的端点。 The correct endpoint as suggested by docs should be query interface and that goes like文档建议的正确端点应该是查询接口,就像

http://pushgateway.example.org:9091/api/v1/status http://pushgateway.example.org:9091/api/v1/status

To do this in python simple make use of json package.要在 python 中简单地做到这一点,请使用 json 包。

import json
txt = requests.get(PROMETHEUS+'api/v1/metrics').text
j = json.loads(txt)
print(j['data'])

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

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