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="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添加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"} 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'  

原文地址:https://www.cnblogs.com/xiaobaozi-95/p/10684524.html