1. 程式人生 > >prometheus的資料型別介紹

prometheus的資料型別介紹

一、簡介

Prometheus將所有采集到的樣本資料以時間序列(time-series)的方式儲存在記憶體資料庫中,並定時儲存在硬碟上。時間序列中的每一個樣本由以下三部分組成。

  • 指標(metric): metric name和描述當前樣本特徵的labelsets組成,參考格式如 <metric name>{<label name>=<label value>, ...};,其中metric name的命名規則為:應用名稱開頭_監測對像_數值型別_單位
  • 時間截(timestamp):一個精確到毫秒的時間截;
  • 樣本值(value):一個float64的浮點型別資料表示當前的樣本值。

二、Prometheus的四種資料型別

2.1 Counter(計數器型別)
Counter型別的指標的工作方式和計數器一樣,只增不減(除非系統發生了重置)。Counter一般用於累計值,例如記錄請求次數、任務完成數、錯誤發生次數。counter主要有兩個方法:

//將counter值加1.
Inc()
// 將指定值加到counter值上,如果指定值< 0會panic.
Add(float64)

在Prometheus自定義的metrics監控中,Counter的使用可以參考如下:

public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {

    static final Counter requestCounter = Counter.build()
            .name("io_namespace_http_requests_total").labelNames("path", "method", "code") //metric name建議使用_total結尾
            .help("Total requests.").register();

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        int status = response.getStatus();

        requestCounter.labels(requestURI, method, String.valueOf(status)).inc(); //呼叫inc()函式,每次請求發生時計數+1
        super.afterCompletion(request, response, handler, ex);
    }
}

Counter型別資料可以讓使用者方便的瞭解事件產生的速率的變化,在PromQL內建的相關操作函式可以提供相應的分析,比如以HTTP應用請求量來進行說明:

//通過rate()函式獲取HTTP請求量的增長率
rate(http_requests_total[5m])
//查詢當前系統中,訪問量前10的HTTP地址
topk(10, http_requests_total)

2.2 Gauge(儀表盤型別)
Gauge是可增可減的指標類,可以用於反應當前應用的狀態。比如在監控主機時,主機當前的內容大小(node_memory_MemFree),可用記憶體大小(node_memory_MemAvailable)。或者時容器當前的cpu使用率,記憶體使用率。
Gauge指標物件主要包含兩個方法inc()以及dec(),使用者新增或者減少計數。
在Prometheus自定義的metrics監控中,Gauge的使用可以參考如下:

public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {

...省略的程式碼
static final Gauge inprogressRequests = Gauge.build()
        .name("io_namespace_http_inprogress_requests").labelNames("path", "method", "code")
        .help("Inprogress requests.").register();

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    ...省略的程式碼
    inprogressRequests.labels(requestURI, method, String.valueOf(status)).inc();// 計數器+1
    return super.preHandle(request, response, handler);
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    ...省略的程式碼
    inprogressRequests.labels(requestURI, method, String.valueOf(status)).dec();// 計數器-1
    super.afterCompletion(request, response, handler, ex);
}
}

對於Gauge型別的監控指標,通過PromQL內建函式delta()可以獲取樣本在一段時間內的變化情況,比如:

dalta(cpu_temp_celsius{host="zeus"}[2h]) //計算CPU溫度在兩小時內的差異
predict_linear(node_filesystem_free{job="node"}[1h], 4*3600) //預測系統磁碟空間在4小時之後的剩餘情況

2.3 Histogram(直方圖型別)
Histogram 由 < basename>_bucket{le="< upper inclusive bound>"},< basename>_bucket{le="+Inf"}, < basename>_sum,_count 組成,主要用於表示一段時間範圍內對資料進行取樣(通常是請求持續時間或響應大小),並能夠對其指定區間以及總數進行統計,通常它採集的資料展示為直方圖。
在Prometheus自定義的metrics監控中,Histgram的使用可以參考如下:
以請求響應時間requests_latency_seconds為例,比如我們需要記錄http請求響應時間符合在分佈範圍{0.005,0.01,0.025,0.05,0.075,0.1,0.25,0.5,0.75,1,2.5,5,7.5,10}中的次數時

public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {

    static final Histogram requestLatencyHistogram = Histogram.build().labelNames("path", "method", "code")
            .name("io_namespace_http_requests_latency_seconds_histogram").help("Request latency in seconds.")
            .register();

    private Histogram.Timer histogramRequestTimer;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ...省略的程式碼
        histogramRequestTimer = requestLatencyHistogram.labels(requestURI, method, String.valueOf(status)).startTimer();
        ...省略的程式碼
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ...省略的程式碼
        histogramRequestTimer.observeDuration();
        ...省略的程式碼
    }

使用Histogram構造器在建立Histogram監控指標時,預設的buckets範圍為{0.005,0.01,0.025,0.05,0.075,0.1,0.25,0.5,0.75,1,2.5,5,7.5,10},如果要修改預設的buckets,可以使用.buckets(double… bukets)覆蓋。
Histogram會自動建立3個指標,分別為:

  • 事件發生的總次數,basename_count。
# 實際含義: 當前一共發生了2次http請求
io_namespace_http_requests_latency_seconds_histogram_count{path="/",method="GET",code="200",} 2.0
  • 所有事件產生值的大小的總和,basename_sum。
# 實際含義: 發生的2次http請求總的響應時間為13.107670803000001 秒
io_namespace_http_requests_latency_seconds_histogram_sum{path="/",method="GET",code="200",} 13.107670803000001
  • 事件產生的值分佈在bucket中的次數,basename_bucket{le=“上包含”}
# 在總共2次請求當中。http請求響應時間 <=0.005 秒 的請求次數為0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.005",} 0.0
# 在總共2次請求當中。http請求響應時間 <=0.01 秒 的請求次數為0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.01",} 0.0
# 在總共2次請求當中。http請求響應時間 <=0.025 秒 的請求次數為0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.025",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.05",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.075",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.1",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.25",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.5",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="0.75",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="1.0",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="2.5",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="5.0",} 0.0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="7.5",} 2.0
# 在總共2次請求當中。http請求響應時間 <=10 秒 的請求次數為0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="10.0",} 2.0
# 在總共2次請求當中。http請求響應時間 10 秒 的請求次數為0
io_namespace_http_requests_latency_seconds_histogram_bucket{path="/",method="GET",code="200",le="+Inf",} 2.0

2.4 Summary(摘要型別)
Summary型別和Histogram型別相似,由< basename>{quantile="< φ>"},< basename>_sum,< basename>_count組成,主要用於表示一段時間內資料取樣結果(通常時請求持續時間或響應大小),它直接儲存了quantile資料,而不是根據統計區間計算出來的。Summary與Histogram相比,存在如下區別:

  • 都包含 < basename>_sum和< basename>_count;
  • Histogram需要通過< basename>_bucket計算quantile,而Summary直接儲存了quantile的值。
    在Prometheus自定義的metrics監控中,Summary的使用可以參考如下:
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {

    static final Summary requestLatency = Summary.build()
            .name("io_namespace_http_requests_latency_seconds_summary")
            .quantile(0.5, 0.05)
            .quantile(0.9, 0.01)
            .labelNames("path", "method", "code")
            .help("Request latency in seconds.").register();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ...省略的程式碼
        requestTimer = requestLatency.labels(requestURI, method, String.valueOf(status)).startTimer();
        ...省略的程式碼
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ...省略的程式碼
        requestTimer.observeDuration();
        ...省略的程式碼
    }
}

Summary型別指標中包含的資料如下:

  • 事件發生總的次數
# 含義:當前http請求發生總次數為12次
io_namespace_http_requests_latency_seconds_summary_count{path="/",method="GET",code="200",} 12.0
  • 事件產生的值的總和
# 含義:這12次http請求的總響應時間為 51.029495508s
io_namespace_http_requests_latency_seconds_summary_sum{path="/",method="GET",code="200",} 51.029495508
  • 事件產生的值的分佈情況
# 含義:這12次http請求響應時間的中位數是3.052404983s
io_namespace_http_requests_latency_seconds_summary{path="/",method="GET",code="200",quantile="0.5",} 3.052404983
# 含義:這12次http請求響應時間的9分位數是8.003261666s
io_namespace_http_requests_latency_seconds_summary{path="/",method="GET",code="200",quantile="0.9",} 8.003261666

參考

1.Prometheus Metric型別
2.自定義Metrics:讓Prometheus監控你的應用程式(Spring版)
3.自定義Metrics:讓Prometheus監控你的應用程式
4.使用Prometheus+Grafana監控MySQL實踐
5.全面學習Prometheus
6.Prometheus筆記(一)metric type