日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

【转】prometheus数据上报方式pushgateway

發布時間:2023/12/15 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 【转】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'

總結

以上是生活随笔為你收集整理的【转】prometheus数据上报方式pushgateway的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。