prometheus数据上报方式-pushgateway

pushgateway

客户端使用push的方式上报监控数据到pushgateway,prometheus会按期从pushgateway拉取数据。使用它的缘由主要是:python

  • Prometheus 采用 pull 模式,可能因为不在一个子网或者防火墙缘由,致使Prometheus 没法直接拉取各个 target数据。
  • 在监控业务数据的时候,须要将不一样数据汇总, 由 Prometheus 统一收集。

拓扑图linux


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能够看到指标nginx

# HELP go_gc_duration_seconds A summary of the GC invocation durations. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 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添加targetgit

- 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 参数将数据持久化下来。github

 

push数据到pushgateway

    推送URL :pushgateway默认采用9091端口,路径: /metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}dom

<JOBNAME>是job标签的值,后面跟任意数量的标签对,instance标签能够有也能够没有。curl

示例:ide

# 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"} 2333url

推送一个更复杂的spa

# 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"} 100 # 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="200",domain="abc.cn"} 276683 http_request_total{code="400",domain="abc.cn"} 0 http_request_total{code="408",domain="abc.cn"} 7 http_request_total{code="401",domain="abc.cn"} 0 http_request_total{schema="http",domain="abc.cn"} 277725 http_request_total{schema="https",domain="abc.cn"} 0 http_request_time{code="total",domain="abc.cn"} 76335.809000 http_request_uniqip{domain="abc.cn"} 216944 http_request_maxip{clientip="172.27.0.12",domain="abc.cn"} 81
# 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'  

相关文章
相关标签/搜索