简体   繁体   English

如何在普罗米修斯中使用过滤器编写 API 查询

[英]how to write API query with filters in prometheus

what I want: get the metrics from Prometheus with python我想要什么:使用 python 从 Prometheus 获取指标

sample code here:示例代码在这里:

import requests
url='prometheus.cup.com'
response=requests.get("{0}/api/v1/query_range?query=container_cpu_load_average_10s&start=2018-11-05T00:59:00.781Z&end=2018-11-05T01:00:00.781Z&step=15s".format(url))

sample result is:样本结果是:

{u'beta_kubernetes_io_os': u'linux', u'name': u'k8s_POD_nginx-bf8f468d8-gbjvp_openwhisk01_01f85b0c-9f87-11e8-893e-6c92bf025c32_5', u'image': u'openshift/origin-pod:v3.9.0', u'namespace': u'openwhisk01', u'instance': u'openshift-app1.cup.com', u'job': u'kubernetes-cadvisor', u'pod_name': u'nginx-bf8f468d8-gbjvp', u'container_name': u'POD', u'__name__': u'container_cpu_load_average_10s', u'beta_kubernetes_io_arch': u'amd64', u'id': u'/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod01f85b0c_9f87_11e8_893e_6c92bf025c32.slice/docker-d7422ab03a96a07395538e5fa5c9d971bb66855f94a45f4540f423cb1da3422f.scope', u'kubernetes_io_hostname': u'openshift-app1.cup.com'}

problem: i want to get result by some filters, like问题:我想通过一些过滤器得到结果,比如

query=container_cpu_load_average_10s{container_name=POD}, 
(of course, this type is not right, just an example)

so what's the right method to write the API query with filters in python?那么在python中使用过滤器编写API查询的正确方法是什么?

thanks in advance提前致谢

Your code is almost correct, just pass the query as a parameter to the URL.您的代码几乎是正确的,只需将查询作为参数传递给 URL。 See this abbreviated snippet of code taken from here :请参阅取自此处的简短代码片段:

response = requests.get('http://localhost:9090/api/v1/query'),
    params={'query': "query=container_cpu_load_average_10s{container_name=POD}"})
print(response.json()['data']['result'])
Oliver answer have some error, fix it. 奥利弗回答有一些错误,修复它。
 #!/usr/bin/env python import requests prome_sql = "(node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes * 100" response = requests.get('http://192.168.20.249:9090/api/v1/query', params={'query': prome_sql}) print(response.json()["data"]['result'])

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

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