【轉】prometheus數據上報方式-pushgateway


 

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

-------------------------

 

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
復制代碼

刪除指標:

# 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'  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM