使用 prometheus python 庫編寫自定義指標的方法(完整代碼)
雖然 prometheus 已有大量可直接使用的 exporter 可供使用,以滿足收集不同的監控指標的需要。例如,node exporter 可以收集機器 cpu,內存等指標,cadvisor 可以收集容器指標。然而,如果需要收集一些定制化的指標,還是需要我們編寫自定義的指標。
本文講述如何使用 prometheus python 客戶端庫和 flask 編寫 prometheus 自定義指標。
安裝依賴庫
我們的程序依賴于flask 和prometheus client 兩個庫,其 requirements.txt 內容如下:
flask==1.1.2prometheus-client==0.8.0
運行 flask
我們先使用 flask web 框架將 /metrics 接口運行起來,再往里面添加指標的實現邏輯。
#!/usr/bin/env python# -*- coding:utf-8 -*-from flask import Flaskapp = Flask(__name__)@app.route(’/metrics’)def hello(): return ’metrics’if __name__ == ’__main__’: app.run(host=’0.0.0.0’, port=5000)
打開瀏覽器,輸入 http://127.0.0.1:5000/metrics,按下回車后瀏覽器顯示 metrics 字符。
編寫指標
Prometheus 提供四種指標類型,分別為 Counter,Gauge,Histogram 和 Summary。
Counter
Counter 指標只增不減,可以用來代表處理的請求數量,處理的任務數量,等。
可以使用 Counter 定義一個 counter 指標:
counter = Counter(’my_counter’, ’an example showed how to use counter’)
其中,my_counter 是 counter 的名稱,an example showed how to use counter 是對該 counter 的描述。
使用 counter 完整的代碼如下:
#!/usr/bin/env python# -*- coding:utf-8 -*-from flask import Flask, Responsefrom prometheus_client import Counter, generate_latestapp = Flask(__name__)counter = Counter(’my_counter’, ’an example showed how to use counter’)@app.route(’/metrics’)def hello(): counter.inc(1) return Response(generate_latest(counter), mimetype=’text/plain’)if __name__ == ’__main__’: app.run(host=’0.0.0.0’, port=5000)
訪問 http://127.0.0.1:5000/metrics,瀏覽器輸出:
# HELP my_counter_total an example showed how to use counter# TYPE my_counter_total countermy_counter_total 6.0# HELP my_counter_created an example showed how to use counter# TYPE my_counter_created gaugemy_counter_created 1.5932468510424378e+09
在定義 counter 指標時,可以定義其 label 標簽:
counter = Counter(’my_counter’, ’an example showed how to use counter’, [’machine_ip’])
在使用時指定標簽的值:
counter.labels(’127.0.0.1’).inc(1)
這時瀏覽器會將標簽輸出:
my_counter_total{machine_ip='127.0.0.1'} 1.0
Gauge
Gauge 指標可增可減,例如,并發請求數量,cpu 占用率,等。
可以使用 Gauge 定義一個 gauge 指標:
registry = CollectorRegistry()gauge = Gauge(’my_gauge’, ’an example showed how to use gauge’, [’machine_ip’], registry=registry)
為使得 /metrics 接口返回多個指標,我們引入了 CollectorRegistry ,并設置 gauge 的 registry 屬性。
使用 set 方法設置 gauge 指標的值:
gauge.labels(’127.0.0.1’).set(2)
訪問 http://127.0.0.1:5000/metrics,瀏覽器增加輸出:
# HELP my_gauge an example showed how to use gauge# TYPE my_gauge gaugemy_gauge{machine_ip='127.0.0.1'} 2.0
Histogram
Histogram 用于統計樣本數值落在不同的桶(buckets)里面的數量。例如,統計應用程序的響應時間,可以使用 histogram 指標類型。
使用 Histogram 定義一個 historgram 指標:
buckets = (100, 200, 300, 500, 1000, 3000, 10000, float(’inf’))histogram = Histogram(’my_histogram’, ’an example showed how to use histogram’, [’machine_ip’], registry=registry, buckets=buckets)
如果我們不使用默認的 buckets,可以指定一個自定義的 buckets,如上面的代碼所示。
使用 observe() 方法設置 histogram 的值:
histogram.labels(’127.0.0.1’).observe(1001)
訪問 /metrics 接口,輸出:
# HELP my_histogram an example showed how to use histogram# TYPE my_histogram histogrammy_histogram_bucket{le='100.0',machine_ip='127.0.0.1'} 0.0my_histogram_bucket{le='200.0',machine_ip='127.0.0.1'} 0.0my_histogram_bucket{le='300.0',machine_ip='127.0.0.1'} 0.0my_histogram_bucket{le='500.0',machine_ip='127.0.0.1'} 0.0my_histogram_bucket{le='1000.0',machine_ip='127.0.0.1'} 0.0my_histogram_bucket{le='3000.0',machine_ip='127.0.0.1'} 1.0my_histogram_bucket{le='10000.0',machine_ip='127.0.0.1'} 1.0my_histogram_bucket{le='+Inf',machine_ip='127.0.0.1'} 1.0my_histogram_count{machine_ip='127.0.0.1'} 1.0my_histogram_sum{machine_ip='127.0.0.1'} 1001.0# HELP my_histogram_created an example showed how to use histogram# TYPE my_histogram_created gaugemy_histogram_created{machine_ip='127.0.0.1'} 1.593260699767071e+09
由于我們設置了 histogram 的樣本值為 1001,可以看到,從 3000 開始,xxx_bucket 的值為 1。由于只設置一個樣本值,故 my_histogram_count 為 1 ,且樣本總數 my_histogram_sum 為 1001。讀者可以自行試驗幾次,慢慢體會 histogram 指標的使用,遠比看網上的文章理解得快。
Summary
Summary 和 histogram 類型類似,可用于統計數據的分布情況。
定義 summary 指標:
summary = Summary(’my_summary’, ’an example showed how to use summary’, [’machine_ip’], registry=registry)
設置 summary 指標的值:
summary.labels(’127.0.0.1’).observe(randint(1, 10))
訪問 /metrics 接口,輸出:
# HELP my_summary an example showed how to use summary# TYPE my_summary summarymy_summary_count{machine_ip='127.0.0.1'} 4.0my_summary_sum{machine_ip='127.0.0.1'} 16.0# HELP my_summary_created an example showed how to use summary# TYPE my_summary_created gaugemy_summary_created{machine_ip='127.0.0.1'} 1.593263241728389e+09
附:完整源代碼
#!/usr/bin/env python# -*- coding:utf-8 -*-from random import randintfrom flask import Flask, Responsefrom prometheus_client import Counter, Gauge, Histogram, Summary, generate_latest, CollectorRegistryapp = Flask(__name__)registry = CollectorRegistry()counter = Counter(’my_counter’, ’an example showed how to use counter’, [’machine_ip’], registry=registry)gauge = Gauge(’my_gauge’, ’an example showed how to use gauge’, [’machine_ip’], registry=registry)buckets = (100, 200, 300, 500, 1000, 3000, 10000, float(’inf’))histogram = Histogram(’my_histogram’, ’an example showed how to use histogram’, [’machine_ip’], registry=registry, buckets=buckets)summary = Summary(’my_summary’, ’an example showed how to use summary’, [’machine_ip’], registry=registry)@app.route(’/metrics’)def hello(): counter.labels(’127.0.0.1’).inc(1) gauge.labels(’127.0.0.1’).set(2) histogram.labels(’127.0.0.1’).observe(1001) summary.labels(’127.0.0.1’).observe(randint(1, 10)) return Response(generate_latest(registry), mimetype=’text/plain’)if __name__ == ’__main__’: app.run(host=’0.0.0.0’, port=5000)
參考資料
https://github.com/prometheus/client_pythonhttps://prometheus.io/docs/concepts/metric_types/https://prometheus.io/docs/instrumenting/writing_clientlibs/https://prometheus.io/docs/instrumenting/exporters/https://pypi.org/project/prometheus-client/https://prometheus.io/docs/concepts/metric_types/http://www.coderdocument.com/docs/prometheus/v2.14/best_practices/histogram_and_summary.htmlhttps://prometheus.io/docs/practices/histograms/
總結
到此這篇關于使用 prometheus python 庫編寫自定義指標的文章就介紹到這了,更多相關prometheus python 庫編寫自定義指標內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
