prometheus increase函数注意事项(深入理解Prometheus:metrictype)

在上篇文章《深入理解Prometheus: PromQL查询逻辑详解,我来为大家科普一下关于prometheus increase函数注意事项?以下内容希望对你有帮助!

prometheus increase函数注意事项(深入理解Prometheus:metrictype)

prometheus increase函数注意事项

在上篇文章《深入理解Prometheus: PromQL查询逻辑详解

》中,介绍了PromQL查询逻辑,并同时提到了两种类型:

metric type:counter, gauge, histogram, summary, untyped

promql expr type:string, scalar, instant vector, range vector

本篇文章将介绍metric type每种类型的含义、如果使用其度量app的运行、如果暴露给prometheus server,以及在PromQL中使用的注意事项。

类型详解

类型

说明

gauge

用于跟踪当前的请求数或可以自然上升或下降的事物,例如内存使用情况、队列长度、正在进行的请求数或当前 CPU 使用情况。

counter

用于跟踪多个事件或数量的累积总数,例如 HTTP 请求的总数或处理请求所花费的总秒数。重新启动时,计数器的值会重置为0。

histogram

用于跟踪一组观察值(如请求延迟)在一组存储桶中的分布。它还跟踪观察值的总数,以及观察值的累积和。

summary

用于跟踪一组观察值(如请求延迟)的分布,作为一组分位数/百分位数。与histogram一样,还跟踪观察值的总数,以及观察值的累积和。

使用Metric Type

metric type在两个地方会体现其价值:

  1. 度量app并生成metric,这是最重要的体现

  2. 使用PromQL计算或者汇总metric数据

首先,我们先看如何用这些metric来度量app。每种类型都有各自特定的方法和特性,通常我们使用Prometheus Client Library来更方便的度量我们的app,下面将具体示例。

Gauge metrics

queueLength := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "queue_length", Help: "The number of items in the queue.", }) // Use Set() when you know the absolute value from some other source. queueLength.Set(0) // Use these methods when your code directly observes the increase or decrease of something, such as adding an item to a queue. queueLength.Inc() // Increment by 1. queueLength.Dec() // Decrement by 1. queueLength.Add(23) queueLength.Sub(42)

gauge metric经常用于暴露当前Unix时间戳,所以特意加了一个便捷函数

demoTimestamp.SetToCurrentTime()

Counter metrics

totalRequests := prometheus.NewCounter(prometheus.CounterOpts{ Name: "http_requests_total", Help: "The total number of handled HTTP requests.", }) totalRequests.Inc() totalRequests.Add(23)

counter metric随时间只加不减,且重启后自动设置为0,计算式相关的函数如rate可以识别到重置的问题,并正确计算

Histogram metrics

histogram metric稍微复杂一点,需要设置观察bucket的数量,以及每个bucket的上限。Prometheus histogram中的bucket计数是累积的,每个后续桶都包含前一个桶的计数,换句话说,所有桶的下边界都从零开始。因此,您无需显式配置每个存储桶的下限,只需配置上限:

requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "A histogram of the HTTP request durations in seconds.", // Bucket configuration: the first bucket includes all requests finishing in 0.05 seconds, the last one includes all requests finishing in 10 seconds. Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, })

由于枚举所有存储桶可能很常见,因此有一些辅助函数prometheus.LinearBuckets()和prometheus.ExponentialBuckets()可以帮助生成线性或指数存储桶方案。

直方图会自动分类和计算值的分布,因此只需要一个Observe()方法,当在代码中处理要跟踪的内容时调用该方法。例如,如果您刚刚处理了一个耗时 0.42 秒的 HTTP 请求,将执行以下操作:

requestDurations.Observe(0.42)

Go Client Library还为计时持续时间提供了帮助函数,然后自动将它们观察到histogram中:

timer := prometheus.NewTimer(requestDurations) // [...Handle the request...] timer.ObserveDuration()

Summary metrics

创建和使用摘要类似于Histogram,不同之处在于需要指定要跟踪的分位数,而不是bucket。例如,如果您想跟踪 HTTP 请求延迟的第 50、90 和 99 个百分位数,可以创建如下summary:

requestDurations := prometheus.NewSummary(prometheus.SummaryOpts{ Name: "http_request_duration_seconds", Help: "A summary of the HTTP request durations in seconds.", Objectives: map[float64]float64{ 0.5: 0.05, // 50th percentile with a max. absolute error of 0.05. 0.9: 0.01, // 90th percentile with a max. absolute error of 0.01. 0.99: 0.001, // 99th percentile with a max. absolute error of 0.001. }, }, )

创建后,跟踪持续时间的工作方式与Histogram完全相同:

requestDurations.Observe(0.42)

histograms vs. summaries

这个我们在文章《prometheus基本概念》中详细对比过,结论是:

  • 如果需要聚合或者观测值的范围和分布,选择直方图。

  • 如果需要精确的分位数,选择Summary。

    导出格式

    目前只支持基于text的导出格式(曾经支持过grpc格式,后放弃了),具体每种类型的格式如下:

    Gauge

    # HELP queue_length The number of items in the queue. # TYPE queue_length gauge queue_length 42

    Counter

    # HELP http_requests_total The total number of handled HTTP requests. # TYPE http_requests_total counter http_requests_total 7734

    Histogram

    # HELP http_request_duration_seconds A histogram of the HTTP request durations in seconds. # TYPE http_request_duration_seconds histogram http_request_duration_seconds_bucket{le="0.05"} 4599 http_request_duration_seconds_bucket{le="0.1"} 24128 http_request_duration_seconds_bucket{le="0.25"} 45311 http_request_duration_seconds_bucket{le="0.5"} 59983 http_request_duration_seconds_bucket{le="1"} 60345 http_request_duration_seconds_bucket{le="2.5"} 114003 http_request_duration_seconds_bucket{le="5"} 201325 http_request_duration_seconds_bucket{le=" Inf"} 227420 http_request_duration_seconds_sum 88364.234 http_request_duration_seconds_count 227420

    Histogram相对复杂一些,需要多条series才能表达清楚每个bucket的统计数据,其中有几个注意的点:

  • {le=" Inf"}用于表示超过最高上限的bucket的累积值

  • _sum表示观察值得和

  • _count表示观察值的数量

  • _sum和_count可以用于计算平均值

    Summary

    # HELP http_request_duration_seconds A summary of the HTTP request durations in seconds. # TYPE http_request_duration_seconds summary http_request_duration_seconds{quantile="0.5"} 0.052 http_request_duration_seconds{quantile="0.90"} 0.564 http_request_duration_seconds{quantile="0.99"} 2.372 http_request_duration_seconds_sum 88364.234 http_request_duration_seconds_count 227420

    Prometheus如何使用Metric Type

    在Prometheus中,虽然抓取时也同时抓取了type信息,但并没有使用和存储type信息,也就是说在prometheus中,不知道也不识别具体metric是什么type,很多人提这个问题,未来有可能会保留type信息

    在PromQL中也不区分metric type,只要符合PromQL expr type即可。

  • 免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

      分享
      投诉
      首页