编写一个简单的基于jmespath 的prometheus exporter
目的很简单,因为系统好多监控指标是通过json 暴露的,并不是标准的prometheus metrics 格式,处理方法
实际上很简单,我们可以基于jsonpath 解析json数据,转换为prometheus metrics 但是感觉查询能力一般,个人
还是比较喜欢jmespath,目前已经有一个开源的基于jsonpath 开发的exporter prometheus-jsonpath-exporter,基于python 开发,所以使用jmespath 直接替换jsonpath 实现更加方便的exporter
项目结构
代码很简单,基于jsonpath 的exporter开发, 替换jsonpath library 为 jmespath
- 整体结构
├── Dockerfile
├── LICENSE
├── README.md
├── app
│ ├── exporter.py
│ └── requirements.txt
└── example
└── config.yml
- app/requirements.txt
pytz
prometheus_client
jmespath
pyYAML
- app/exporter.py
#!/usr/bin/python
import json
import time
import urllib2
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY
import argparse
import yaml
import jmespath
import logging
DEFAULT_PORT=9158
DEFAULT_LOG_LEVEL='info'
class JsonPathCollector(object):
def __init__(self, config):
self._config = config
def collect(self):
config = self._config
result = json.loads(urllib2.urlopen(config['json_data_url'], timeout=10).read())
for metric_config in config['metrics']:
metric_name = "{}_{}".format(config['metric_name_prefix'], metric_config['name'])
metric_description = metric_config.get('description', '')
metric_path = metric_config['path']
// jmeshpath metrics 处理
value =jmespath.search(metric_path,result)
logging.debug("metric_name: {}, value for '{}' : {}".format(metric_name, metric_path, value))
metric = GaugeMetricFamily(metric_name, metric_description, value=value)
yield metric
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Expose metrics bu jsonpath for configured url')
parser.add_argument('config_file_path', help='Path of the config file')
args = parser.parse_args()
with open(args.config_file_path) as config_file:
config = yaml.load(config_file)
log_level = config.get('log_level', DEFAULT_LOG_LEVEL)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.getLevelName(log_level.upper()))
exporter_port = config.get('exporter_port', DEFAULT_PORT)
logging.debug("Config %s", config)
logging.info('Starting server on port %s', exporter_port)
start_http_server(exporter_port)
REGISTRY.register(JsonPathCollector(config))
while True: time.sleep(1)
- dockerfile
FROM python:2.7.13-alpine
COPY app /opt/prometheus-jmespath-exporter
RUN pip install -r /opt/prometheus-jmespath-exporter/requirements.txt
EXPOSE 9158
ENTRYPOINT ["python", "/opt/prometheus-jmespath-exporter/exporter.py"]
测试使用
- 构建容器镜像
docker build -t dalongrong/promethues-jmespath-exporter .
- 使用
一个简单的例子,使用了goaccess 进行nginx 日志的处理,包含了geoip 的处理,具体json api 格式参考goaccess-geoip-docker-compose-demo
version: "3"
services:
metrics:
image: dalongrong/promethues-jmespath-exporter
volumes:
- "./conf/config.yaml:/etc/prometheus-jsonpath-exporter/config.yml"
ports:
- "9158:9158"
command: /etc/prometheus-jsonpath-exporter/config.yml
- confi.yaml
exporter_port: 9158 # Port on which prometheus can call this exporter to get metrics
log_level: info
json_data_url: http://ipaddress||docker-compose-servicename/app.json # Url to get json data used for fetching metric values
metric_name_prefix: go_access # All metric names will be prefixed with this value
metrics:
- name: total_request_ip # Final metric name will be go_access_total_request
description: Number of total request ip address
path: length(hosts.data[*].data)
- 效果
说明
主要还是基于别人写好的一个exporter,只是做了简单的修改,使用jmespath进行metrics 指标的转换,主要是jmespath 能力强大,同时docker 镜像
也已经push docker 官方仓库了promethues-jmespath-exporter
参考资料
https://github.com/rongfengliang/goaccess-geoip-docker-compose-demo
https://github.com/project-sunbird/prometheus-jsonpath-exporter
https://github.com/rongfengliang/promethues-jmespath-exporter
https://www.cnblogs.com/rongfengliang/p/10688965.html
编写一个简单的基于jmespath 的prometheus exporter的更多相关文章
- 使用CEF(二)— 基于VS2019编写一个简单CEF样例
使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十一)——一步一步教你如何撸Dapr之自动扩/缩容
上一篇我们讲到了dapr提供的bindings,通过绑定可以让我们的程序轻装上阵,在极端情况下几乎不需要集成任何sdk,仅需要通过httpclient+text.json即可完成对外部组件的调用,这样 ...
- struts1:(Struts重构)构建一个简单的基于MVC模式的JavaWeb
在构建一个简单的基于MVC模式的JavaWeb 中,我们使用了JSP+Servlet+JavaBean构建了一个基于MVC模式的简单登录系统,但在其小结中已经指出,这种模式下的Controller 和 ...
- javascript编写一个简单的编译器(理解抽象语法树AST)
javascript编写一个简单的编译器(理解抽象语法树AST) 编译器 是一种接收一段代码,然后把它转成一些其他一种机制.我们现在来做一个在一张纸上画出一条线,那么我们画出一条线需要定义的条件如下: ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- 手把手教你编写一个简单的PHP模块形态的后门
看到Freebuf 小编发表的用这个隐藏于PHP模块中的rootkit,就能持久接管服务器文章,很感兴趣,苦无作者没留下PoC,自己研究一番,有了此文 0×00. 引言 PHP是一个非常流行的web ...
- 用 C 语言编写一个简单的垃圾回收器
人们似乎觉得编写垃圾回收机制是非常难的,是一种仅仅有少数智者和Hans Boehm(et al)才干理解的高深魔法.我觉得编写垃圾回收最难的地方就是内存分配,这和阅读K&R所写的malloc例 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十)——一步一步教你如何撸Dapr之绑定
如果说Actor是dapr有状态服务的内部体现的话,那绑定应该是dapr对serverless这部分的体现了.我们可以通过绑定极大的扩展应用的能力,甚至未来会成为serverless的基础.最开始接触 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十二)——istio+dapr构建多运行时服务网格
多运行时是一个非常新的概念.在 2020 年,Bilgin Ibryam 提出了 Multi-Runtime(多运行时)的理念,对基于 Sidecar 模式的各种产品形态进行了实践总结和理论升华.那到 ...
随机推荐
- HTML中data-* 属性
使用 data-* 属性来嵌入自定义数据: <ul><li data-animal-type="bird">Owl</li><li dat ...
- Python机器学习(基础篇---监督学习(支持向量机))
支持向量机(分类) 支持向量机分类器根据训练样本的分布,搜索所有可能的线性分类器中最佳的那个.我们会发现决定其直线位置的样本并不是所有训练数据,而是其中的两个空间间隔最小的两个不同类别的数据点,而我们 ...
- python并发_线程
关于进程的复习: # 管道 # 数据的共享 Manager dict list # 进程池 # cpu个数+1 # ret = map(func,iterable) # 异步 自带close和join ...
- java面向对象编程(九)--final
1.final概念 final可以修饰变量或者方法.在某些情况下,程序员可能有以下需求: a.当不希望父类的某个方法被子类覆盖(override)时,可以用final关键字修饰. b.当不希望类的某个 ...
- Charles几个常用测试功能小结
Charles应该是目前最常用的代理软件(之一),使用简单.Charles强大的抓包与协议调试代理功能可以满足我们大部分需求,居然还免费(我可没说有破解版).日常测试中,我吗常用的几个功能主要是抓取网 ...
- CentOS7的网卡重启方法
1.centos6的网卡重启方法:service network restartcentos7的网卡重启方法:systemctl restart network 2.DNS配置文件:cat /etc/ ...
- C# Winform 国际化
1.在Form的language属性选择中文,来制作中文界面 保存后,设置界面标题会变成如下所示,并且会出现一个zh-CN的资源文件,打开resx文件可看到相应内容 2.将Form的language属 ...
- 【python全栈开发】初识python
本人最开始接触python是在2013年接触,写过hello word!在此之前对开发类没有多大兴趣,不知道重要性,属于浑浑噩噩,忙忙乎乎,跌跌撞撞的.随后选择了Linux运维作为就业主攻方向. 经过 ...
- 在word中批量更改Mathtype公式的格式
方法参照下面这个链接 还有这个方法
- Golang微服务:Micro介绍
官方文档地址 https://micro.mu/docs/index.html Tookit API HTTP接入网关.反向代理或将HTTP转为RPC请求调用后端服务 Web 一个web应用程序,默认 ...