翻译:使用Python写JSON Exporter
来源:互联网 发布:多源异构数据融合 编辑:程序博客网 时间:2024/06/05 18:48
原文:Writing JSON Exporters in Python | Robust Perception
使用Python写JSON Exporter
Brian Brazil August 19, 2015
日常有一个常见的问题:是否有一种方法可以将JSON文本存储到普罗米修斯中。从任意的JSON 文档中提取有用的指标(metrics)
是不可能的,毕竟没有现成的实现。不过,可以使用Python快速实现并产生有意义的指标(metrics)
。
更新: Python客户端有一个新的API,更容易使用。请参考Python编写一个Jenkins导出器
假设有一个名为SVC的服务它通过HTTP产生JSON输出。
{ "requests_handled": 14324, "requests_duration_milliseconds": 1235257, "request_failures": 7, "documents_loaded": { "fast": 7, "slow": 60 }}
首先,安装普罗米修斯Python客户端和请求库。
pip install prometheus_client requests
然后就可以写一个简单的exporter。将以下内容放入一个名为json exporter.py
的文件中:
from prometheus_client import start_http_server, Metric, REGISTRYimport jsonimport requestsimport sysimport timeclass JsonCollector(object): def __init__(self, endpoint): self._endpoint = endpoint def collect(self): # Fetch the JSON response = json.loads(requests.get(self._endpoint).content.decode('UTF-8')) # Convert requests and duration to a summary in seconds metric = Metric('svc_requests_duration_seconds', 'Requests time taken in seconds', 'summary') metric.add_sample('svc_requests_duration_seconds_count', value=response['requests_handled'], labels={}) metric.add_sample('svc_requests_duration_seconds_sum', value=response['requests_duration_milliseconds'] / 1000.0, labels={}) yield metric # Counter for the failures metric = Metric('svc_requests_failed_total', 'Requests failed', 'summary') metric.add_sample('svc_requests_failed_total', value=response['request_failures'], labels={}) yield metric # Metrics with labels for the documents loaded metric = Metric('svc_documents_loaded', 'Requests failed', 'gauge') for k, v in response['documents_loaded'].items(): metric.add_sample('svc_documentes_loaded', value=v, labels={'repository': k}) yield metricif __name__ == '__main__': # Usage: json_exporter.py port endpoint start_http_server(int(sys.argv[1])) REGISTRY.register(JsonCollector(sys.argv[2])) while True: time.sleep(1)
执行代码
python json_exporter.py 1234 http://host/path/to/metrics.json
如果访问http://localhost:1234/metrics,就可以访问到指标(metrics)
!
翻译阅读
单机执行
- reymont/prometheus-python - 码云 - 开源中国
原文中并不能直接执行,这里修改了部分代码支持直接执行。
python json_exporter.pycurl -XGET http://localhost:1234/metrics
yield
- 关于Python中的yield - tqsummer - 博客园
- 十二、Python的yield用法与原理 - alvine008的专栏 - CSDN博客
当for第一次调用函数的时候,生成一个生成器,并且在的函数中运行该循环,直到生成第一个值。然后每次调用都会运行循环并且返回下一个值,直到没有值返回为止。
>>> def g(n):... for i in range(n):... yield i **2...>>> for i in g(5):... print i,":",...0 : 1 : 4 : 9 : 16 :
阅读全文
0 0
- 翻译:使用Python写JSON Exporter
- 使用Python写的翻译工具
- python写的翻译脚本
- 在命令行用使用有道翻译,python写一个命令行有道词典
- 主题:用Python写个翻译工具
- 用python写一个命令行有道翻译
- Python JSON的使用
- python中使用JSON
- python json 的使用
- [python]json的使用
- python 使用json模块
- 使用Prometheus监控Kafka logsize(kafka-logsize-exporter)
- 使用chrome+baidu exporter+aria2下载网盘文件
- Python--使用json.dumps 将 json 格式的数据写到文件里--with open as f
- 使用 python 写 COM
- python写json文件,中文乱码问题
- python json和java json的使用
- python中的json使用误区
- Android之获取联系人
- Java Math的floor、ceil、rint及round方法
- Unity3D研究院之打开照相机与本地相册进行裁剪显示
- Swift
- K8S两周年!中美庆生系列之Google大咖直播!
- 翻译:使用Python写JSON Exporter
- Android 获取手机联系人以及短信内容
- 3ds Max插件开发(五)The Reference System参考系统
- Android—获取手机联系人
- Java设计模式之单例模式
- socket网络编程
- windows 用virtualenv创建不同python版本的虚拟环境
- 单列模式实现
- virt-manager无法启动的问题