tft每日頭條

 > 生活

 > prometheus increase函數注意事項

prometheus increase函數注意事項

生活 更新时间:2025-02-15 08:47:33

prometheus increase函數注意事項?在上篇文章《深入理解Prometheus: PromQL查詢邏輯詳解,我來為大家科普一下關于prometheus increase函數注意事項?以下内容希望對你有幫助!

prometheus increase函數注意事項(深入理解Prometheus:metrictype)1

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即可。

    更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

    查看全部
  • 相关生活资讯推荐

    热门生活资讯推荐

    网友关注

    Copyright 2023-2025 - www.tftnews.com All Rights Reserved