if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
访问 http://127.0.0.1:5000/metrics,浏览器输出:
1 2 3 4 5 6
# 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 标签:
1
counter = Counter('my_counter', 'an example showed how to use counter', ['machine_ip'])
在使用时指定标签的值:
1
counter.labels('127.0.0.1').inc(1)
这时浏览器会将标签输出:
1
my_counter_total{machine_ip="127.0.0.1"} 1.0
Gauge
Gauge 指标可增可减,例如,并发请求数量,cpu 占用率,等。
可以使用 Gauge 定义一个 gauge 指标:
1 2
registry = CollectorRegistry() 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)
如果我们不使用默认的 buckets,可以指定一个自定义的 buckets,如上面的代码所示。
使用 observe() 方法设置 histogram 的值:
1
histogram.labels('127.0.0.1').observe(1001)
访问 /metrics 接口,输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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
# 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
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)