prometheus pythonライブラリを使用してカスタム指標を記述する
20960 ワード
prometheusには、異なるモニタリング指標を収集する必要性を満たすために、直接使用可能なexporterが多数存在します.例えば、node exporterは機械cpu、メモリなどの指標を収集することができ、cadvisorは容器指標を収集することができる.しかし、いくつかのカスタマイズされた指標を収集する必要がある場合は、カスタム指標を作成する必要があります.
この文書では、prometheus pythonクライアントライブラリとflaskを使用してprometheusカスタム指標を記述する方法について説明します.
依存ライブラリのインストール
我々のプログラムはflaskとprometheus clientの2つのライブラリに依存し、その
flaskの実行
まずflask webフレームワークを使用して
ブラウザを開き、
指標の作成
Prometheusは、Counter、Gauge、Histogram、Summaryの4つの指標タイプを提供します.
Counter
Counter指標は増減しないだけで,処理の要求数,処理のタスク数などを表すことができる.
ここで、
counterの完全なコードは次のとおりです.
アクセス
counter指標を定義するときに、labelラベルを定義できます.
使用時にラベルの値を指定します.
ブラウザからラベルが出力されます.
Gauge
Gauge指標は、例えば、同時要求数、cpu占有率など、増減可能である.
Histogram
Histogramは、サンプル値が異なるバケツに落ちた数を統計するために使用されます.たとえば、histogram指標タイプを使用すると、アプリケーションの応答時間を統計できます.
デフォルトの
Summary
Summaryはhistogramタイプと類似しており、統計データの分布状況に使用できます.
summary指標の定義:
summary指標の値を設定します.
添付:完全なソースコード
参考資料 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/ https://blog.csdn.net/wtan825/article/details/94616813
この文書では、prometheus pythonクライアントライブラリとflaskを使用してprometheusカスタム指標を記述する方法について説明します.
依存ライブラリのインストール
我々のプログラムはflaskとprometheus clientの2つのライブラリに依存し、その
requirements.txt
の内容は以下の通りである.flask==1.1.2
prometheus-client==0.8.0
flaskの実行
まずflask webフレームワークを使用して
/metrics
インタフェースを実行し、指標の実装ロジックを追加します.#!/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文字が表示されます.指標の作成
Prometheusは、Counter、Gauge、Histogram、Summaryの4つの指標タイプを提供します.
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, 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指標を定義するときに、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 gauge
my_gauge{machine_ip="127.0.0.1"} 2.0
Histogram
Histogramは、サンプル値が異なるバケツに落ちた数を統計するために使用されます.たとえば、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 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
histogram
のサンプル値が1001に設定されているため、3000からxxx_bucketの値は1です.サンプル値は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 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)
参考資料