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的更多相关文章

  1. 使用 PushGateway 进行数据上报采集

    转载自:https://cloud.tencent.com/developer/article/1531821 1.PushGateway 介绍 Prometheus 是一套开源的系统监控.报警.时间 ...

  2. html5统计数据上报API:SendBeacon

    公司为了精准的了解自己产品的用户使用情况,通常会对用户数据进行统计分析,获取pv.uv.页面留存率.访问设备等信息.与之相关的就是客户端的数据采集,然后上报的服务端.为了保证数据的准确性,就需要保证数 ...

  3. ADO.NET编程之美----数据访问方式(面向连接与面向无连接)

    最近,在学习ADO.NET时,其中提到了数据访问方式:面向连接与面向无连接.于是,百度了一下,发现并没有很好的资料,然而,在学校图书馆中发现一本好书(<ASP.NET MVC5 网站开发之美&g ...

  4. Kooboo CMS技术文档之三:切换数据存储方式

    切换数据存储方式包括以下几种: 将文本内容存储在SqlServer.MySQL.MongoDB等数据库中 将站点配置信息存储在数据库中 将后台用户信息存储在数据库中 将会员信息存储在数据库中 将图片. ...

  5. geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探

    在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天 ...

  6. Android数据存储方式--SharedPreferences

    Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...

  7. 【hive】——Hive四种数据导入方式

    Hive的几种常见的数据导入方式这里介绍四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询出相应的数据并导入到Hive表中:(4).在 ...

  8. iOS 应用数据存储方式(XML属性列表-plist)

    iOS 应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) ...

  9. hive的数据导出方式

    hive有三种导出数据的方式 >导出数据到本地 >导出数据到hdfs >导出数据到另一个表   导出数据到本地文件系统 insert overwrite local director ...

随机推荐

  1. Mha-Atlas-MySQL高可用方案实践

    一,mysql-mha环境准备 1.1 实验环境: 1.1 实验环境: 主机名 IP地址(NAT) 描述 mysql-master eth0:10.1.1.154 系统:CentOS6.5(6.x都可 ...

  2. 基于栈的指令集与基于寄存器的指令集详细比对及JVM执行栈指令集实例剖析

    基于栈的指令集与基于寄存器的指令集详细比对: 这次来学习一些新的概念:关于Java字节码的解释执行的一种方式,当然啦是一些纯理论的东东,但很重要,在之后会有详细的实验来对理论进行巩固滴,下面来了解一下 ...

  3. mysql init password centos

    https://www.cnblogs.com/FlyingPuPu/p/7783735.html

  4. [Python自学] day-22 (1) (Session、CSRF、中间件)

    一.响应函数补充 三种返回响应的方式: return HttpResponse() return render() return redirect() HttpResponse: 除了能够返回字符串, ...

  5. 001_C#我的第一个串口上位机软件

    (一)首先感谢杜洋工作室 < 入门 C#设计 >带来的视频教学 (二)本次设计从会单片机但是又不会上位机又想搞简单上位机的人群角度提供教程 (三)本次教程的目的是制作普通的串口软件,从而实 ...

  6. 001_linuxC++之_类的引入

    (一) C++类的引入,图片的程序比较好看,文中程序不贴出来 (二) 知识点 1. 成员函数的存取权限:公有的(public),保护的(protectd),私有的(private) 2. 第27行th ...

  7. Linq to JSON/Json.NET

    Can I LINQ a JSON? http://stackoverflow.com/questions/18758361/can-i-linq-a-json Json.NET https://js ...

  8. [Luogu] 家族

    https://www.luogu.org/problemnew/show/1767 字符串的读入有点麻烦 #include <cstdio> #include <cstring&g ...

  9. 数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)

    #include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; ...

  10. shell 获取指定ip的丢包率

    shell 获取指定ip的丢包率 丢包率大于10%就重新网络 使用sed 替换字符串 [[ $(ping -c 10 -W 1 baidu.com | awk '$6 ~ /%/{print $6}' ...