1 Counter
counter指标类型只能增加,重启时置零,可以使用计数器来表示所服务的请求数,已完成的任务或错误。
from prometheus_client import start_http_server, Counter
import random
import time
a = Counter('a', 'Description of Counter')
b = Counter('b', 'Description of Counter',['label1','lable2'])
def process_request(t):
"""A dummy function that takes some time."""
a.inc() # Increment by 1
b.labels(label1='k1', lable2='f1').inc(1.6) # Increment by given value
b.labels(label1='k1', lable2='f2').inc(3) # Increment by given value
b.labels(label1='k2', lable2='f2').inc(4) # Increment by given value
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8001)
# Generate some requests.
while True:
process_request(1)
http://127.0.0.1:8001/
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 65.0
python_gc_objects_collected_total{generation="1"} 286.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 37.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="7",patchlevel="4",version="3.7.4"} 1.0
# HELP a_total Description of Counter
# TYPE a_total counter
a_total 9.0
# TYPE a_created gauge
a_created 1.582171645067157e+09
# HELP b_total Description of Counter
# TYPE b_total counter
b_total{label1="k1",lable2="f1"} 14.399999999999999
b_total{label1="k1",lable2="f2"} 27.0
b_total{label1="k2",lable2="f2"} 36.0
# TYPE b_created gauge
b_created{label1="k1",lable2="f1"} 1.582171645245558e+09
b_created{label1="k1",lable2="f2"} 1.5821716452455769e+09
b_created{label1="k2",lable2="f2"} 1.582171645245592e+09
2 Gauge
Gauge指标类型表示单个数值,可以任意地上升和下降的度量。通常用于测量值,如温度或当前内存使用情况。
from prometheus_client import start_http_server, Gauge
import random
import time
a = Gauge('a', 'Description of gauge')
b = Gauge('b', 'Description of gauge',['label1','lable2'])
def process_request(t):
"""A dummy function that takes some time."""
a.set(random.random()) # value自己定义,但是一定要为 整数或者浮点数
b.labels(label1='k1',lable2='f1').set(random.randint(100,200))
b.labels(label1='k1', lable2='f2').set(random.randint(200,300))
b.labels(label1='k2', lable2='f2').set(random.randint(400,500))
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8002)
# Generate some requests.
while True:
process_request(1)
http://127.0.0.1:8002/
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 65.0
python_gc_objects_collected_total{generation="1"} 286.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 37.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="7",patchlevel="4",version="3.7.4"} 1.0
# HELP a Description of gauge
# TYPE a gauge
a 0.5924517654833364
# HELP b Description of gauge
# TYPE b gauge
b{label1="k1",lable2="f1"} 128.0
b{label1="k1",lable2="f2"} 254.0
b{label1="k2",lable2="f2"} 465.0
3 Histogram和Summary
《PrometheUS:聊聊数据类型Histogram和Summary》:https://blog.csdn.net/u014686399/article/details/103068655?utm_source=distribute.pc_relevant.none-task
技术交流
CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!