使用 prometheus python 庫編寫自定義指標(biāo)的方法(完整代碼)
雖然 prometheus 已有大量可直接使用的 exporter 可供使用,以滿足收集不同的監(jiān)控指標(biāo)的需要。例如,node exporter 可以收集機器 cpu,內(nèi)存等指標(biāo),cadvisor 可以收集容器指標(biāo)。然而,如果需要收集一些定制化的指標(biāo),還是需要我們編寫自定義的指標(biāo)。
本文講述如何使用 prometheus python 客戶端庫和 flask 編寫 prometheus 自定義指標(biāo)。
安裝依賴庫
我們的程序依賴于flask 和prometheus client 兩個庫,其 requirements.txt
內(nèi)容如下:
flask==1.1.2
prometheus-client==0.8.0
運行 flask
我們先使用 flask web 框架將 /metrics
接口運行起來,再往里面添加指標(biāo)的實現(xiàn)邏輯。
#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask app = 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 字符。
編寫指標(biāo)
Prometheus 提供四種指標(biāo)類型,分別為 Counter,Gauge,Histogram 和 Summary。
Counter
Counter 指標(biāo)只增不減,可以用來代表處理的請求數(shù)量,處理的任務(wù)數(shù)量,等。
可以使用 Counter
定義一個 counter 指標(biāo):
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, Response from prometheus_client import Counter, generate_latest app = 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 counter
my_counter_total 6.0
# HELP my_counter_created an example showed how to use counter
# TYPE my_counter_created gauge
my_counter_created 1.5932468510424378e+09
在定義 counter 指標(biāo)時,可以定義其 label 標(biāo)簽:
counter = Counter('my_counter', 'an example showed how to use counter', ['machine_ip'])
在使用時指定標(biāo)簽的值:
counter.labels('127.0.0.1').inc(1)
這時瀏覽器會將標(biāo)簽輸出:
my_counter_total{machine_ip="127.0.0.1"} 1.0
Gauge
Gauge 指標(biāo)可增可減,例如,并發(fā)請求數(shù)量,cpu 占用率,等。
可以使用 Gauge
定義一個 gauge 指標(biāo):
registry = CollectorRegistry() gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)
為使得 /metrics
接口返回多個指標(biāo),我們引入了 CollectorRegistry
,并設(shè)置 gauge 的 registry 屬性。
使用 set
方法設(shè)置 gauge 指標(biāo)的值:
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 gauge
my_gauge{machine_ip="127.0.0.1"} 2.0
Histogram
Histogram 用于統(tǒng)計樣本數(shù)值落在不同的桶(buckets)里面的數(shù)量。例如,統(tǒng)計應(yīng)用程序的響應(yīng)時間,可以使用 histogram 指標(biāo)類型。
使用 Histogram
定義一個 historgram 指標(biāo):
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)
如果我們不使用默認(rèn)的 buckets
,可以指定一個自定義的 buckets
,如上面的代碼所示。
使用 observe()
方法設(shè)置 histogram 的值:
histogram.labels('127.0.0.1').observe(1001)
訪問 /metrics
接口,輸出:
# HELP my_histogram an example showed how to use histogram
# TYPE my_histogram histogram
my_histogram_bucket{le="100.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="200.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="300.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="500.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="1000.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="3000.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="10000.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="+Inf",machine_ip="127.0.0.1"} 1.0
my_histogram_count{machine_ip="127.0.0.1"} 1.0
my_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 gauge
my_histogram_created{machine_ip="127.0.0.1"} 1.593260699767071e+09
由于我們設(shè)置了 histogram
的樣本值為 1001,可以看到,從 3000 開始,xxx_bucket 的值為 1。由于只設(shè)置一個樣本值,故 my_histogram_count
為 1 ,且樣本總數(shù) my_histogram_sum
為 1001。
讀者可以自行試驗幾次,慢慢體會 histogram 指標(biāo)的使用,遠(yuǎn)比看網(wǎng)上的文章理解得快。
Summary
Summary 和 histogram 類型類似,可用于統(tǒng)計數(shù)據(jù)的分布情況。
定義 summary 指標(biāo):
summary = Summary('my_summary', 'an example showed how to use summary', ['machine_ip'], registry=registry)
設(shè)置 summary 指標(biāo)的值:
summary.labels('127.0.0.1').observe(randint(1, 10))
訪問 /metrics
接口,輸出:
# HELP my_summary an example showed how to use summary
# TYPE my_summary summary
my_summary_count{machine_ip="127.0.0.1"} 4.0
my_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 gauge
my_summary_created{machine_ip="127.0.0.1"} 1.593263241728389e+09
附:完整源代碼
#!/usr/bin/env python # -*- coding:utf-8 -*- from random import randint from flask import Flask, Response from prometheus_client import Counter, Gauge, Histogram, Summary, \ generate_latest, CollectorRegistry app = 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_python
https://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.html
https://prometheus.io/docs/practices/histograms/
總結(jié)
到此這篇關(guān)于使用 prometheus python 庫編寫自定義指標(biāo)的文章就介紹到這了,更多相關(guān)prometheus python 庫編寫自定義指標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Numpy中創(chuàng)建數(shù)組的9種方式小結(jié)
本文主要介紹了Numpy中創(chuàng)建數(shù)組的9種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Python+selenium 獲取瀏覽器窗口坐標(biāo)、句柄的方法
今天小編就為大家分享一篇Python+selenium 獲取瀏覽器窗口坐標(biāo)、句柄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python字符串處理實現(xiàn)單詞反轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Python字符串處理實現(xiàn)單詞反轉(zhuǎn)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06python選取特定列 pandas iloc,loc,icol的使用詳解(列切片及行切片)
今天小編就為大家分享一篇python選取特定列 pandas iloc,loc,icol的使用詳解(列切片及行切片),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python 利用郵件系統(tǒng)完成遠(yuǎn)程控制電腦的實現(xiàn)(關(guān)機、重啟等)
這篇文章主要介紹了Python 利用郵件系統(tǒng)完成遠(yuǎn)程控制電腦(關(guān)機、重啟等),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python 利用for循環(huán) 保存多個圖像或者文件的實例
今天小編就為大家分享一篇python 利用for循環(huán) 保存多個圖像或者文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11