1. counter
Counter 数据类型
描述: 该数据类型在测控中是使用最频繁的数据类型,其用于记录事件的数量或者大小,并通常用来跟踪某个特定代码路径被执行频率和记录数、服务的字节数以及。
2. Gauge 数据类型
描述: 它存放的是一些当前状态的快照其值是可以动态改变的因此可以将负数传给该类型的inc方法, 例如常用于队列中元素的个数、内存以及磁盘的使用率、活跃的线程数或者回调指定的其它函数。
[root@yld-db 0211]# cat flask_counter_gauge.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-from flask import Flask, Response
from prometheus_client import Counter, generate_latest, CollectorRegistry, Gaugeapp = 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)@app.route('/metrics')
def hello():
counter.labels('127.0.0.1').inc(1)
gauge.labels('127.0.0.1').set(444) return Response(generate_latest(registry), mimetype='text/plain')
#return Response(generate_latest(gauge), mimetype='text/plain')if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
[root@yld-db 0211]# curl 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{machine_ip="127.0.0.1"} 1.0
# HELP my_counter_created an example showed how to use counter
# TYPE my_counter_created gauge
my_counter_created{machine_ip="127.0.0.1"} 1.676122056478778e+09
# HELP my_gauge an example showed how to use gauge
# TYPE my_gauge gauge
my_gauge{machine_ip="127.0.0.1"} 444.0
[root@yld-db 0211]#
3. Histogram 数据类型
描述: 该类型主要是用于告诉你低于某一个值得事件个数(分位数
), 例如0.95分位数为300ms代表95%得请求耗时小于300ms。
Histogram 数据类型指标是由_count,_sum,_bukect
组成。
关键说明:桶(Bucket)它是一个counter数据类型的时序集合(必须有序),但是桶的数量影响着性能我们可以采用Prometheus中metric_relable_configs来丢弃一些无关的桶。例如一组桶(1ms, 10ms, 25ms)
用来跟踪落入每个桶中的事件个数。
[root@yld-db 0211]# cat flask_histogram.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
from flask import Flask, Response
from prometheus_client import Counter, generate_latest, CollectorRegistry, Histogram, Gauge
app = Flask(__name__)
registry = CollectorRegistry()
gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)
buckets = (100, 200, 300, 500, 1000, 1500, 5000, float('inf'))
histogram = Histogram('my_histogram', 'an example showed how to use histogram', ['machine_ip'], registry=registry, buckets=buckets)
@app.route('/metrics')
def hello():
gauge.labels('127.0.0.1').set(444)
my_random = random.randint(98,4500)
print my_random
histogram.labels('127.0.0.1').observe(my_random)
return Response(generate_latest(registry), mimetype='text/plain')
#return Response(generate_latest(gauge), mimetype='text/plain')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
[root@yld-db 0211]#
[root@yld-db 0211]# curl 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"} 444.0
# 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"} 1.0
my_histogram_bucket{le="500.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="1000.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="1500.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="5000.0",machine_ip="127.0.0.1"} 4.0
my_histogram_bucket{le="+Inf",machine_ip="127.0.0.1"} 4.0
my_histogram_count{machine_ip="127.0.0.1"} 4.0
my_histogram_sum{machine_ip="127.0.0.1"} 7895.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.676123241790573e+09
[root@yld-db 0211]#
4. Summary 数据类型
描述: 该数据类型主要针对于系统性能的监测(平均延迟数据),例如除了后端的延时你可能也希望跟踪收到的后端响应体的大小,指标名称即指标_count 与 指标_sum
分别代表调用数量和测控值得总和。
[root@yld-db 0211]# cat flask_summary.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
from flask import Flask, Response
from prometheus_client import Counter, generate_latest, CollectorRegistry, Summary, Gauge
app = Flask(__name__)
registry = CollectorRegistry()
gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)
summary = Summary('my_summary', 'an example showed how to use summary', ['machine_ip'], registry=registry)
@app.route('/metrics')
def hello():
gauge.labels('127.0.0.1').set(444)
my_random = random.randint(1, 10)
print my_random
summary.labels('127.0.0.1').observe(my_random)
return Response(generate_latest(registry), mimetype='text/plain')
#return Response(generate_latest(gauge), mimetype='text/plain')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
[root@yld-db 0211]#
[root@yld-db 0211]# curl 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"} 444.0
# HELP my_summary an example showed how to use summary
# TYPE my_summary summary
my_summary_count{machine_ip="127.0.0.1"} 6.0
my_summary_sum{machine_ip="127.0.0.1"} 33.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.676123663974278e+09
[root@yld-db 0211]#
参考:7.Prometheus监控进阶之自定义监控业务应用 (qq.com)
(20条消息) 使用 Prometheus Python 库编写自定义指标_haozlee的博客-CSDN博客_prometheus 自定义指标
完整代码
[root@yld-db 0211]# cat four_kind_metrics.py
#!/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 = (190, 250, 380, 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)
my_random = randint(300,7000)
print my_random
histogram.labels('127.0.0.1').observe(my_random)
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)
[root@yld-db 0211]#
5. Prometheus的配置
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "gauge_exporter"
scrape_interval: 30s # 采集频率为30s抓取一次
static_configs:
- targets: # 配置采集任务
- 127.0.0.1:5004
用一个例子来演示会更加清晰