使用 prometheus python 庫(kù)編寫自定義指標(biāo)的方法(完整代碼)
雖然 prometheus 已有大量可直接使用的 exporter 可供使用,以滿足收集不同的監(jiān)控指標(biāo)的需要。例如,node exporter 可以收集機(jī)器 cpu,內(nèi)存等指標(biāo),cadvisor 可以收集容器指標(biāo)。然而,如果需要收集一些定制化的指標(biāo),還是需要我們編寫自定義的指標(biāo)。
本文講述如何使用 prometheus python 客戶端庫(kù)和 flask 編寫 prometheus 自定義指標(biāo)。
安裝依賴庫(kù)
我們的程序依賴于flask 和prometheus client 兩個(gè)庫(kù),其 requirements.txt 內(nèi)容如下:
flask==1.1.2
prometheus-client==0.8.0
運(yùn)行 flask
我們先使用 flask web 框架將 /metrics 接口運(yùn)行起來,再往里面添加指標(biāo)的實(shí)現(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)只增不減,可以用來代表處理的請(qǐng)求數(shù)量,處理的任務(wù)數(shù)量,等。
可以使用 Counter 定義一個(gè) counter 指標(biāo):
counter = Counter('my_counter', 'an example showed how to use counter')
其中,my_counter 是 counter 的名稱,an example showed how to use counter 是對(duì)該 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)時(shí),可以定義其 label 標(biāo)簽:
counter = Counter('my_counter', 'an example showed how to use counter', ['machine_ip'])
在使用時(shí)指定標(biāo)簽的值:
counter.labels('127.0.0.1').inc(1)
這時(shí)瀏覽器會(huì)將標(biāo)簽輸出:
my_counter_total{machine_ip="127.0.0.1"} 1.0
Gauge
Gauge 指標(biāo)可增可減,例如,并發(fā)請(qǐng)求數(shù)量,cpu 占用率,等。
可以使用 Gauge 定義一個(gè) gauge 指標(biāo):
registry = CollectorRegistry()
gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)
為使得 /metrics 接口返回多個(gè)指標(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)計(jì)樣本數(shù)值落在不同的桶(buckets)里面的數(shù)量。例如,統(tǒng)計(jì)應(yīng)用程序的響應(yīng)時(shí)間,可以使用 histogram 指標(biāo)類型。
使用 Histogram 定義一個(gè) 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,可以指定一個(gè)自定義的 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
由于我們?cè)O(shè)置了 histogram 的樣本值為 1001,可以看到,從 3000 開始,xxx_bucket 的值為 1。由于只設(shè)置一個(gè)樣本值,故 my_histogram_count 為 1 ,且樣本總數(shù) my_histogram_sum 為 1001。
讀者可以自行試驗(yàn)幾次,慢慢體會(huì) histogram 指標(biāo)的使用,遠(yuǎn)比看網(wǎng)上的文章理解得快。
Summary
Summary 和 histogram 類型類似,可用于統(tǒng)計(jì)數(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 庫(kù)編寫自定義指標(biāo)的文章就介紹到這了,更多相關(guān)prometheus python 庫(kù)編寫自定義指標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Sanic框架實(shí)現(xiàn)文件上傳功能
Sanic是一個(gè)Python 3.5+的異步Web框架,它的設(shè)計(jì)理念與Flask相似,但采用了更高效的異步I/O處理,在處理文件上傳時(shí),Sanic同樣提供了方便、高效的方法,本教程將結(jié)合實(shí)際案例,詳細(xì)介紹如何在Sanic框架中實(shí)現(xiàn)文件上傳的功能,需要的朋友可以參考下2024-08-08
Django讀取Mysql數(shù)據(jù)并顯示在前端的實(shí)例
今天小編就為大家分享一篇Django讀取Mysql數(shù)據(jù)并顯示在前端的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python獲取網(wǎng)絡(luò)時(shí)間戳的兩種方法詳解
在我們進(jìn)行注冊(cè)碼的有效期驗(yàn)證時(shí),通常使用獲取網(wǎng)絡(luò)時(shí)間的方式來進(jìn)行比對(duì)。本文將介紹兩種利用Python獲取網(wǎng)絡(luò)時(shí)間戳的方法,感興趣的可以了解一下2022-01-01

