天天看點

Prometheus + Grafana微服務監控<四>:Prometheus自定義埋點一、Prometheus自定義名額類型二、PromQL語句三、gateway自定義埋點四、Grafana自定義圖表五、參考資料

目錄

一、Prometheus自定義名額類型

1.1 Counter(計數器)

1.2 Gauge(儀表盤)

1.3 Histogram(直方圖)

1.4 Summary(摘要)

二、PromQL語句

2.1 查詢結果類型(3種)

2.2 查詢語句

三、gateway自定義埋點

3.1 引入jar包

3.2 項目添加配置

3.3 gateway建立全局過濾器

四、Grafana自定義圖表

五、參考資料

一、Prometheus自定義名額類型

共4種名額類型:Counter、Gauge、Histogram、Summary

1.1 Counter(計數器)

1):值隻增不減

2):調用inc(),計數器+1

3):應用場景:請求總量、錯誤總量,微服務線上時間、CPU使用時間等

1.2 Gauge(儀表盤)

1):值可增可減,反應名額的目前狀态

2):調用inc(),+1;dec(),-1

3):應用場景:目前正在處理的Http請求數量、CPU使用率、記憶體使用率等

1.3 Histogram(直方圖)

指定分布範圍内(buckets)記錄大小或者事件發生的次數(資料分布情況)

1):預設的buckets範圍為:{.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10}

2):自動建立3個名額:

              事件發生總次數:XX_count,如:請求總數

              所有事件産生值的總和: XX_sum,如:請求總數的響應時間總和

              事件産生值分布在bucket中的次數:XX_bucket,如:在1s-2s響應時間的次數

3):應用場景:請求響應時間分布等

1.4 Summary(摘要)

自定義分布範圍的資料分布情況,類似Histogram

1):自定義分布範圍quantile(與Histogram差別,可自定義)

2):自動建立3個名額:

              事件發生總次數:XX_count,如:請求總數

              所有事件産生值的總和: XX_sum,如:請求總數的響應時間總和

              事件産生值分布在quantile中的次數:XX{quantile="XXX"},如:在分位數XXX響應時間的次數

3):應用場景:請求響應時間分布等

二、PromQL語句

參考資料:https://www.bookstack.cn/read/prometheus_practice/promql-summary.md

2.1 查詢結果類型(3種)

瞬時資料(Instant vector):一個時序隻有一個點,例如:http_requests_total

區間資料(Range vector):一個時序有多個點,例如:http_requests_total[5m]

純量資料(Scalar):純量隻有一個數字,沒有時序,例如:count(http_requests_total)

2.2 查詢語句

1):條件查詢:=、!=、=~、!~,如:http_requests_total{code=~"2.."}:表示查詢 code 為 "2xx" 的資料

2):算數運算符:+、-、*、/、%、^, 如: http_requests_total{code=~"2.."} * 2

3):比較運算符:==、!=、>、<、>=、<=, 如:http_requests_total{code=~"5.."} > 50

4):邏輯運算符:and、or、unless, 如:http_requests_total{code=~"5.."} or http_requests_total{code=~"4.."}

5):聚合運算符:sum、min、max、avg、count、topk,如:sum(http_requests_total{code=~"2.."})

6):内置函數:rate、floor......,如:rate(http_requests_total{code=~"2.."}[2m]):表示在2分鐘内狀态為2XX每秒請求數量,即:2min内請求增加量除以120s

三、gateway自定義埋點

3.1 引入jar包

<!-- 監控 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>io.micrometer</groupId>
	<artifactId>micrometer-registry-prometheus</artifactId>
	<version>1.6.5</version>
</dependency>
           

3.2 項目添加配置

management:
  endpoints:
    web:
      exposure:
        include: '*'
           

3.3 gateway建立全局過濾器

package com.common.instance.gateway.filters;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.*;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @description Grafana統計全局過濾器
 * @author tcm
 * @version 1.0.0
 * @date 2021/4/13 16:00
 **/
public class GrafanaStatisticsGlobalFilter implements GlobalFilter, Ordered {

    public static Summary requestLatency;
    public static Counter counter;
    public volatile static Gauge gauge;
    private PrometheusMeterRegistry prometheusMeterRegistry;

    public GrafanaStatisticsGlobalFilter(MeterRegistry meterRegistry){
        this.prometheusMeterRegistry = (PrometheusMeterRegistry)meterRegistry;
        requestLatency = Summary
                .build("demo_gateway_response_time_summary", "微服務響應時間")
                .labelNames("routeId", "uri", "status")
                .register(prometheusMeterRegistry.getPrometheusRegistry());
        counter = Counter
                .build("demo_gateway_request_count", "網關請求次數")
                .labelNames("routeId", "uri", "status")
                .register(prometheusMeterRegistry.getPrometheusRegistry());
        gauge = Gauge
                .build("demo_gateway_process_request_gauge", "網關進行中請求")
                .labelNames("routeId", "uri", "status")
                .register(prometheusMeterRegistry.getPrometheusRegistry());
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 擷取路由
        Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        // 定義标簽s
        Summary.Timer requestTimer = requestLatency.labels(route.getId(), exchange.getRequest().getURI().getPath(), String.valueOf(exchange.getResponse().getStatusCode().value())).startTimer();
        // 請求加1
        gauge.labels(route.getId(), exchange.getRequest().getURI().getPath(), String.valueOf(exchange.getResponse().getStatusCode().value())).inc();

        return chain.filter(exchange)
                .doOnSuccess(aVoid -> endTimer(requestTimer, exchange, "doOnSuccess"))
                .doOnError(throwable -> endTimer(requestTimer, exchange, "doOnError"));
    }

    // 過濾器結束後,處理邏輯
    private void endTimer(Summary.Timer requestTimer, ServerWebExchange exchange, String flag) {
        // 擷取路由
        Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        //
        requestTimer.observeDuration();
        // 響應加1
        counter.labels(route.getId(), exchange.getRequest().getURI().getPath(), String.valueOf(exchange.getResponse().getStatusCode().value())).inc();
        // 響應請求減1
        gauge.labels(route.getId(), exchange.getRequest().getURI().getPath(), String.valueOf(exchange.getResponse().getStatusCode().value())).dec();
    }

    @Override
    public int getOrder() {
        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 50;
    }

}
           

四、Grafana自定義圖表

Step1:點選"Create"按鈕,如下圖所示:

Prometheus + Grafana微服務監控<四>:Prometheus自定義埋點一、Prometheus自定義名額類型二、PromQL語句三、gateway自定義埋點四、Grafana自定義圖表五、參考資料

Step2:點選"Add an empt pannel",編輯圖表:

Prometheus + Grafana微服務監控<四>:Prometheus自定義埋點一、Prometheus自定義名額類型二、PromQL語句三、gateway自定義埋點四、Grafana自定義圖表五、參考資料

Step3:已添加的圖表:

Prometheus + Grafana微服務監控<四>:Prometheus自定義埋點一、Prometheus自定義名額類型二、PromQL語句三、gateway自定義埋點四、Grafana自定義圖表五、參考資料

五、參考資料

https://cloud.tencent.com/developer/article/1553781

http://yunlzheng.github.io/2018/01/24/use-prometheus-monitor-your-spring-boot-application/

https://blog.csdn.net/hxpjava1/article/details/80406222

PromQL用法:https://www.bookstack.cn/read/prometheus_practice/promql-summary.md