python - 如何在 prometheus 中使用过滤器编写 API 查询

标签 python api prometheus

我想要的:使用 python 从 Prometheus 获取指标

此处示例代码:

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))

样本结果是:

{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'}

问题:我想通过一些过滤器得到结果,比如

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

那么在 python 中使用过滤器编写 API 查询的正确方法是什么?

提前致谢

最佳答案

您的代码几乎是正确的,只需将查询作为参数传递给 URL。看到这个简短的代码片段 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'])

关于python - 如何在 prometheus 中使用过滤器编写 API 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165331/

相关文章:

python - f.readline() 不捕获文件的最后一行

python - 读取所有目录下的所有文件

python - django api 支持使用 Many=True 更新序列化器,仅创建

javascript - Angular2/RXJS - 处理潜在的长查询

r - 为什么在 R 中使用 plivo 发送短信失败?

prometheus - 为什么我的 Prometheus Operator ServiceMonitor 需要 `release` 标签才能正常运行?

java - Grafana 查询时间增量

python - 对两个文本文件求和

python - 如何在给定的时间间隔内随机调用一个函数? - Python

amazon-ecs - 使用 ECS 进行 Prometheus DNS 服务发现