promethenus的四种指标

在监控和性能管理中,四种常用的 Prometheus 指标类型包括计数器(Counter)、仪表盘(Gauge)、直方图(Histogram)和摘要(Summary)。下面举例说明每种类型的典型用途:

1. 计数器(Counter)

计数器是一种只能增加的指标,常用来记录事件的发生次数。

示例用途

  • 记录接收到的 HTTP 请求总数。
  • 统计数据库查询的次数。
from prometheus_client import Counter
http_requests_total = Counter('http_requests_total', 'Total HTTP requests.')

counter返回值:

# HELP request_count_total App Request Count
# TYPE request_count_total counter
request_count_total 0.0
# HELP request_count_created App Request Count
# TYPE request_count_created gauge
request_count_created 1.716084841347778e+09

2. 仪表盘(Gauge)

仪表盘是可以增加或减少的指标,用于记录可以随时变化的数值。

示例用途

  • 监控当前正在处理的并发请求数量。
  • 记录系统内存或磁盘空间的使用情况。
from prometheus_client import Gauge
in_progress_requests = Gauge('in_progress_requests', 'HTTP requests currently in progress.')

3. 直方图(Histogram)

直方图用于记录数值的分布,这些数值被分到预定义的桶中,非常适合于记录请求的响应时间或数据处理时间。

示例用途

  • 测量请求的响应时间,并将这些时间分布到不同的时间桶中。
  • 统计文件大小的分布。
from prometheus_client import Histogram
request_latency = Histogram('request_latency_seconds', 'Request latency in seconds.', buckets=(0.1, 0.5, 1, 5, 10))

在 Prometheus 的直方图(Histogram)类型指标中,buckets 是一个关键的概念,用于定义数据分布的区间。每个 bucket 表示一个特定的数值范围,直方图会记录落入每个区间的事件数量。

Buckets 的定义和作用
buckets 通常是一个有序的数值数组,定义了你想要监测的值的分布区间。例如,在监测HTTP请求的响应时间时,你可以设置 buckets 为 [0.1, 0.5, 1, 5, 10],这表示:

第一个 bucket 记录响应时间小于或等于 0.1 秒的请求。
第二个 bucket 记录响应时间在 0.1 秒到 0.5 秒之间的请求。
第三个 bucket 记录响应时间在 0.5 秒到 1 秒之间的请求。
依此类推。

这是一个使用直方图和定义 buckets 的例子:

from prometheus_client import Histogram
import time

# 创建直方图实例,定义buckets
response_times = Histogram('response_time_seconds', 'Histogram of response time in seconds', buckets=[0.1, 0.5, 1, 5, 10])

@app.route('/some_endpoint')
def some_view_function():
    start_time = time.time()
    # 模拟一些处理操作
    time.sleep(0.4)  # 假设操作耗时0.4秒
    response_times.observe(time.time() - start_time)  # 观察并记录耗时
    return "Response"

返回的metrics中会包含如下信息:

# HELP response_time_seconds Histogram of response time in seconds
# TYPE response_time_seconds histogram
response_time_seconds_bucket{le="0.1"} 0.0
response_time_seconds_bucket{le="0.5"} 15.0
response_time_seconds_bucket{le="1.0"} 15.0
response_time_seconds_bucket{le="5.0"} 15.0
response_time_seconds_bucket{le="10.0"} 15.0
response_time_seconds_bucket{le="+Inf"} 15.0
response_time_seconds_count 15.0
response_time_seconds_sum 6.0434651374816895
# HELP response_time_seconds_created Histogram of response time in seconds
# TYPE response_time_seconds_created gauge
response_time_seconds_created 1.716082213744798e+09

4. 摘要(Summary)

摘要类似于直方图,但它计算和存储可配置百分位数的样本观察值,通常用于捕获请求时间的详细百分位统计。

示例用途

  • 动态计算并记录关键操作的处理时间的中位数和95%百分位数。
  • 监控关键任务的执行时间,提供实时的性能数据。
from prometheus_client import Summary

# Create a Summary to track request durations
request_duration = Summary('request_duration_seconds', 'Duration of HTTP requests in seconds')

@app.route('/request')
def handle_request():
    start_time = time.time()
    # Perform the operations for the HTTP request
    # Let's assume some operations happen here
    time.sleep(0.3)  # Simulate a delay

    # Observe the duration and record it in the summary
    request_duration.observe(time.time() - start_time)
    return "Request handled"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值