prometheus数据上报方式-pushgateway
pushgateway
客户端使用push的方式上报监控数据到pushgateway,prometheus会定期从pushgateway拉取数据。使用它的原因主要是:
- Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙原因,导致Prometheus 无法直接拉取各个 target数据。
- 在监控业务数据的时候,需要将不同数据汇总, 由 Prometheus 统一收集。
拓扑图

pushgateway安装
# wget https://github.com/prometheus/pushgateway/releases/download/v0.7.0/pushgateway-0.7.0.linux-amd64.tar.gz
# tar xzvf pushgateway-0.7.0.linux-amd64.tar.gz
# mv pushgateway-0.7.0.linux-amd64 /usr/local/pushgateway
运行
# ./pushgateway
# curl localhost:9091/metrics #访问 127.0.0.1:9091/metrics可以看到指标
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile=""} 1.8299e-05
go_gc_duration_seconds{quantile="0.25"} 1.8299e-05
go_gc_duration_seconds{quantile="0.5"} 2.8353e-05
go_gc_duration_seconds{quantile="0.75"} 2.8353e-05
...
prometheus.yml添加target
- job_name: 'push-metrics'
static_configs:
- targets: ['localhost:9091']
honor_labels: true
# 因为prometheus配置pushgateway 的时候,也会指定job和instance,但是它只表示pushgateway实例,不能真正表达收集数据的含义。所以配置pushgateway需要添加honor_labels:true,避免收集数据本身的job和instance被覆盖。
注意:为了防止 pushgateway 重启或意外挂掉,导致数据丢失,可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。
push数据到pushgateway
推送URL :pushgateway默认采用9091端口,路径: /metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}
<JOBNAME>是job标签的值,后面跟任意数量的标签对,instance标签可以有也可以没有。
示例:
# echo "test_metric 2333" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job
推送之后可以看到
# TYPE test_metric untyped
test_metric{instance="",job="test_job"} 2333
推送一个更复杂的
# cat <<EOF | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metric counter
test_metric{label="val1"}
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 123.45
EOF
或者是先把指标数据写入文件
# curl -XPOST --data-binary @data.txt http://127.0.0.1:9091/metrics/job/nginx/instance/172.27.0.3
http_request_total{code="",domain="abc.cn"}
http_request_total{code="",domain="abc.cn"}
http_request_total{code="",domain="abc.cn"}
http_request_total{code="",domain="abc.cn"}
http_request_total{schema="http",domain="abc.cn"}
http_request_total{schema="https",domain="abc.cn"}
http_request_time{code="total",domain="abc.cn"} 76335.809000
http_request_uniqip{domain="abc.cn"}
http_request_maxip{clientip="172.27.0.12",domain="abc.cn"}
# cat data.txt
删除指标:
# curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job
#说明: 删除标识的组中的所有指标{job="some_job"}(请注意,这不包括{job="some_job",instance="some_instance"}中的指标 ,即使这些指标具有相同的 job 标签
# curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job/instance/test_instance
也可以在界面上删除

也可以通过使用官方给的python library,使用push 方式把数据推送到pushgateway。
# cat client.py
#!/usr/bin/python3
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
registry = CollectorRegistry()
g = Gauge('ping', '检测最大响应时间',['dst_ip','city'], registry=registry) #Guage(metric_name,HELP,labels_name,registry=registry)
g.labels('192.168.1.10','shenzhen').set(42.2) #set设定值
g.labels('192.168.1.11','shenzhen').dec(2) #dec递减2
g.labels('192.168.1.12','shenzhen').inc() #inc递增,默认增1
push_to_gateway('localhost:9091', job='ping_status', registry=registry)
查看

需要注意:使用这种方法,如果使用相同的job名 ,后面插入的数据会覆盖掉之前的。
例如,job 都叫 ping_status
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
registry = CollectorRegistry()
g = Gauge('test_metrics', '描述信息',['label_name'], registry=registry)
g.labels('label1').set(2)
push_to_gateway('localhost:9091', job='ping_status', registry=registry)
执行脚本,查看结果,可以看到之前的数据已经不存在了:

要删除这条数据 ,可以直接使用:curl -XDELETE 'http://localhost:9091/metrics/job/ping_status'
prometheus数据上报方式-pushgateway的更多相关文章
- 使用 PushGateway 进行数据上报采集
转载自:https://cloud.tencent.com/developer/article/1531821 1.PushGateway 介绍 Prometheus 是一套开源的系统监控.报警.时间 ...
- html5统计数据上报API:SendBeacon
公司为了精准的了解自己产品的用户使用情况,通常会对用户数据进行统计分析,获取pv.uv.页面留存率.访问设备等信息.与之相关的就是客户端的数据采集,然后上报的服务端.为了保证数据的准确性,就需要保证数 ...
- ADO.NET编程之美----数据访问方式(面向连接与面向无连接)
最近,在学习ADO.NET时,其中提到了数据访问方式:面向连接与面向无连接.于是,百度了一下,发现并没有很好的资料,然而,在学校图书馆中发现一本好书(<ASP.NET MVC5 网站开发之美&g ...
- Kooboo CMS技术文档之三:切换数据存储方式
切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...
- geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探
在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天 ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- 【hive】——Hive四种数据导入方式
Hive的几种常见的数据导入方式这里介绍四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询出相应的数据并导入到Hive表中:(4).在 ...
- iOS 应用数据存储方式(XML属性列表-plist)
iOS 应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) ...
- hive的数据导出方式
hive有三种导出数据的方式 >导出数据到本地 >导出数据到hdfs >导出数据到另一个表 导出数据到本地文件系统 insert overwrite local director ...
随机推荐
- 由java派生出来的证书错误
未安装请求对应接口证书时的异常:> javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: ...
- 跨域详解之jsonp,底层的实现原理
分享一下跨域,不仅是因为现在的工作中遇到的越来越多,而且在面试中也经常被问到. 那么什么是跨域呢,我们来看官方给出的解释:浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的(所谓同源是指,域名 ...
- maven jar 包不在项目中
maven update project maven build
- python3 基础二——基本的数据类型一
一.基本的数据类型 Python3 中有六个标准的数据类型Number(数字). String(字符串). List(列表) .Tuple(元组). Sets(集合) .Dictionary(字典) ...
- Please, commit your changes or stash them before you can merge. Aborting
1.stash 通常遇到这个问题,你可以直接commit你的修改:但我这次不想这样. 看看git stash是如何做的. git stash git pull git stash pop ...
- C语言实现的文件交互
计算机与外部设备的交互依靠文件完成 文件是记录在外部介质上的数据的集合:例如1.c 是源码 1.exe可执行的文件 文件的分类 按组织结构: 记录文件:有一定结构的文件,可以解析成字段值的文件: 流式 ...
- C语言笔记 09_共用体&typedef&输入|输出
共用体 共用体允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定义共用体 为了定义共用体, ...
- python--第六天练习题
#1.正则表达式计算 origin = "1 - 2 * ( ( 60 - 30 + ( -40.0 / 5 ) * ( 9 - 2 * 5 / 3 + 7 / 3 * 99 / 4 * 2 ...
- PHP mysqli_debug() 函数
定义和用法 mysqli_debug() 函数用于执行调试操作. 注释:为了使用该函数,您必须编译 MySQL 客户端库来支持调试.
- 五十五.ansible概述、ansible基础 、ad-hoc、批量配置管理
1.环境准备 (自动化工具,批量操作) 6台 2cpu,1.5G以上内存,20G硬盘,1网卡 1.1 基础环境准备 1)启动6台虚拟机,ansible.sh 2)真机配置yum仓库 ]# tar ...