Prometheus监控:rate与irate的区别

对官网文档的解读

irate和rate都会用于计算某个指标在一定时间间隔内的变化速率。但是它们的计算方法有所不同:irate取的是在指定时间范围内的最近两个数据点来算速率,而rate会取指定时间范围内所有数据点,算出一组速率,然后取平均值作为结果。

所以官网文档说:irate适合快速变化的计数器(counter),而rate适合缓慢变化的计数器(counter)。

根据以上算法我们也可以理解,对于快速变化的计数器,如果使用rate,因为使用了平均值,很容易把峰值削平。除非我们把时间间隔设置得足够小,就能够减弱这种效应。

试验

用grafana做了一个试验,创建一个测试的dashboard, 分别用 irate 和 rate 来监控CPU使用率指标,时间间隔分别用10m, 5m, 2m, 1m 。其中间隔为10分钟的表达式如下:

sum(irate(process_cpu_seconds_total[10m])) * 100

sum(rate(process_cpu_seconds_total[10m])) * 100

下图是间隔10分钟的结果,可以看到,irate的曲线比较曲折,而rate的曲线相对平缓:

下图是间隔5分钟的结果:

下图是间隔2分钟的结果,两个曲线重合了:

下图是间隔1分钟的结果,显示没有数据,应该是在这个时间间隔找不到一组数据来计算,所以没有数据:

 

附:官网文档

irate()

irate(v range-vector) calculates the per-second instant rate of increase of the time series in the range vector. This is based on the last two data points. Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for.

The following example expression returns the per-second rate of HTTP requests looking up to 5 minutes back for the two most recent data points, per time series in the range vector:

irate(http_requests_total{job="api-server"}[5m])

irate should only be used when graphing volatile, fast-moving counters. Use rate for alerts and slow-moving counters, as brief changes in the rate can reset the FOR clause and graphs consisting entirely of rare spikes are hard to read.

Note that when combining irate() with an aggregation operator (e.g. sum()) or a function aggregating over time (any function ending in _over_time), always take a irate() first, then aggregate. Otherwise irate() cannot detect counter resets when your target restarts.

rate()

rate(v range-vector) calculates the per-second average rate of increase of the time series in the range vector. Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for. Also, the calculation extrapolates to the ends of the time range, allowing for missed scrapes or imperfect alignment of scrape cycles with the range's time period.

The following example expression returns the per-second rate of HTTP requests as measured over the last 5 minutes, per time series in the range vector:

rate(http_requests_total{job="api-server"}[5m])

rate should only be used with counters. It is best suited for alerting, and for graphing of slow-moving counters.

Note that when combining rate() with an aggregation operator (e.g. sum()) or a function aggregating over time (any function ending in _over_time), always take a rate() first, then aggregate. Otherwise rate() cannot detect counter resets when your target restarts.

Prometheus是一个广泛使用的开源监控系统,用于收集和存储系统指标数据。它具有灵活的查询语言和强大的图形化和报警功能,可用于监控各种类型的应用程序和基础设施。 要使用Prometheus监控系统,通常需要以下步骤: 1. 下载和安装Prometheus服务器:你可以从Prometheus官方网站下载适合你操作系统的二进制文件,并按照说明进行安装。 2. 配置Prometheus服务器:在安装完成后,你需要创建一个配置文件来定义你要监控的目标和其他设置。配置文件使用YAML格式,可以指定要监控的目标的地址、指标的抓取频率、告警规则等。 3. 启动Prometheus服务器:在配置文件完成后,你可以启动Prometheus服务器,并通过浏览器访问其Web界面,默认端口为9090。在Web界面中,你可以查看收集到的指标数据、执行查询、创建面板和设置告警规则等。 4. 配置目标应用程序:为了让Prometheus能够收集目标应用程序的指标数据,你需要在目标应用程序中集成Prometheus的客户端库,并在应用程序代码中暴露指标数据的接口。Prometheus客户端库支持多种编程语言,例如Go、Java、Python等。 5. 可选:使用Prometheus的可视化工具:Prometheus本身提供了一个基本的Web界面,但你也可以使用其他可视化工具来更好地展示和分析指标数据。一些流行的可视化工具包括Grafana和Kibana等。 总结来说,Prometheus提供了一种灵活、可扩展的方式来监控系统指标数据,可以帮助你有效地监控和管理你的应用程序和基础设施。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值