Prometheus - 为平均订单大小、订单处理时间和订单之间的时间设计自定义指标

Prometheus - devise custom metrics for the average order-size, order-processing-time and time between orders

使用 Prometheus 指标需要不同的思维方式。设计指标通常并不难。你能帮我设计订单处理系统的指标吗?

系统处理请求(如订单)。每个请求都包含许多要处理的项目。请求的处理时间可能从几秒到几小时不等。 1 个请求中的项目数在 1 到 100 万之间变化。请求之间可能会有一些时间。

我如何使用 Prometheus 设计指标(计数器、仪表、直方图、摘要?),以便在白天(比如每 10 分钟)提供以下信息:

在每个请求的应用程序中,以下信息可用:开始时间、结束时间、已处理的项目数。

最有用的 Prometheus 指标是它提供的 counter. It is basically a forever-increasing value for the lifetime of the process (that starts again from zero whenever the monitored process restarts). Based on it, you can compute how much the monitored metric has increased over any time range (the previous 5 minutes, yesterday, year-to-date) simply by subtracting its value at the start of the range from its value at the end. (And adjusting for any resets to zero.) Prometheus does this automatically for you via the increase() and rate() 函数。

对于您的特定用例,每个时间单位处理的平均项目数将为例如rate(items_processed_total[5m]),其中rate()由Prometheus提供; items_processed_total 是一个计数器,您每次处理一个项目(或一批)时都必须定义和递增; 5m 是您希望计算平均值的时间范围(您显然可以使用任意值,只要它比您的抓取间隔长几倍)。这将为您提供 QPS 平均值,即每秒处理的项目数,平均超过 5 分钟。

对于第二个要点(请求的平均处理时间),您需要两个计数器,例如 requests_processed_total(每次处理完请求时递增 1)和 request_processing_time_seconds_total(每次处理完一个请求,你都会递增 request_end_time - request_start_time)。您正在寻找的价值将由

产生
rate(request_processing_time_seconds_total[5m]) / rate(requests_processed_total[5m])

即前 5 分钟内请求处理时间增加了多少除以前 5 分钟内处理的请求数。 (或您选择的任意时间范围。)

对于您的最后要点(请求之间的平均等待时间),可以是

1 / rate(requests_processed_total[5m])

(即 [=45= 的倒数],即 "seconds per request")如果您想要请求之间的平均时间。

或者,如果您对请求之间的空闲时间感兴趣:

(1 - rate(request_processing_time_seconds_total[5m]))
  /
rate(requests_processed_total[5m])

如果您只对请求之间的空闲时间感兴趣。解释一下,1 - rate(request_processing_time_seconds_total[5m]) 是您的作业空闲时间的百分比(100% - 处理时间);并将其除以 QPS 等于乘以请求之间的平均间隔(见上文)。

后一个表达式假定这是一个单线程进程,它要么正在处理请求,要么处于空闲状态。如果您实际上是并行处理请求,"idle time" 作为指标没有多大意义。

最后,如果您对平均值(比如中位数或其他百分位数)感兴趣,histograms are great. If they didn't use an order of magnitude more metrics (and storage and CPU, etc.) I'd use them for everything. E.g. a histogram of request processing times would give you the average processing time, but also the estimated median processing time, and whatever other percentile yuu want (estimated). Plus of course, the number of requests and the total processing time (that come built in). And they allow you to do aggregate metrics across multiple instances, as opposed to a summary 指标。