亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁/技術(shù)文章
文章詳情頁

使用 prometheus python 庫編寫自定義指標(biāo)的方法(完整代碼)

瀏覽:64日期:2022-07-19 13:29:11

雖然 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.2prometheus-client==0.8.0

運行 flask

我們先使用 flask web 框架將 /metrics 接口運行起來,再往里面添加指標(biāo)的實現(xiàn)邏輯。

#!/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 字符。

編寫指標(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, 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 指標(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 gaugemy_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 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

由于我們設(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 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/

總結(jié)

到此這篇關(guān)于使用 prometheus python 庫編寫自定義指標(biāo)的文章就介紹到這了,更多相關(guān)prometheus python 庫編寫自定義指標(biāo)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久精品99成人中文字幕880 | 毛片视频免费 | 日本黄色激情片 | 欧美激情综合亚洲一二区 | 国产在线观看精品 | 有一婷婷色 | 狠狠色丁香婷婷久久综合2021 | 中国一级黄色录像片 | 欧美精品在线看 | 另类二区 | 伊人啪啪 | 黄色免费看看 | 久久国产一级毛片一区二区 | 99热在线获取最新地址 | 国语对白清晰好大好白在线 | 在线观看人成网站深夜免费 | 麻豆网址 | 爱爱小视频在线观看 | 国产精品久久久久久久久久免费 | 欧美综合中文字幕久久 | 欧美日韩国产超高清免费看片 | 女人一级片 | 中国护士一级毛片免费版本 | 香蕉视频 在线播放 | 妞干网最新视频 | 国产a∨一区二区三区香蕉小说 | 成年人毛片 | 国产色一区 | 精品日产一区二区三区手机 | 国产精品免费大片一区二区 | 亚洲精品一区亚洲精品 | 欧美成人福利视频 | 欧美精欧美乱码一二三四区 | 欧美成人亚洲高清在线观看 | 免费人成黄页在线观看日本 | 十六一下岁女子毛片免费 | 久久91亚洲人成电影网站 | 国产亚洲精品色一区 | 久久国产精品永久免费网站 | 色综合中文字幕在线亚洲 | 色的视频在线观看免费播放 |