欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 使用Prometheus采集自定義指標數(shù)據(jù)的方案

 更新時間:2022年10月09日 11:11:56   作者:張志翔 ?  
這篇文章主要介紹了SpringBoot 使用Prometheus采集自定義指標數(shù)據(jù),我們在k8s集群成功搭建了Prometheus服務,今天,我們將在springboot2.x中使用prometheus記錄指標,需要的朋友可以參考下

我們在k8s集群成功搭建了Prometheus服務。今天,我們將在springboot2.x中使用prometheus記錄指標。

一、我們需要什么指標

對于DDD、TDD等,大家比較熟悉了,但是對于MDD可能就比較陌生了。MDD是Metrics-Driven Development的縮寫,主張開發(fā)過程由指標驅動,通過實用指標來驅動快速、精確和細粒度的軟件迭代。MDD可使所有可以測量的東西都得到量化和優(yōu)化,進而為整個開發(fā)過程帶來可見性,幫助相關人員快速、準確地作出決策,并在發(fā)生錯誤時立即發(fā)現(xiàn)問題并修復。依照MDD的理念,在需求階段就應該考慮關鍵指標,在應用上線后通過指標了解現(xiàn)狀并持續(xù)優(yōu)化。有一些基于指標的方法論,建議大家了解一下:

  • Google的四大黃金指標:延遲Latency、流量Traffic、錯誤Errors、飽和度Saturation
  • Netflix的USE方法:使用率Utilization、飽和度Saturation、錯誤Error
  • WeaveCloud的RED方法:速率Rate、錯誤Errors、耗時Duration

二、在SrpingBoot中引入prometheus

SpringBoot2.x集成Prometheus非常簡單,首先引入maven依賴:

io.micrometer
micrometer-registry-prometheus
1.7.3
io.github.mweirauch
micrometer-jvm-extras
0.2.2

然后,在application.properties中將prometheus的endpoint放出來。

management:
  endpoints:
    web:
      exposure:
        include: info,health,prometheus

接下來就可以進行指標埋點了,Prometheus的四種指標類型此處不再贅述,請自行學習。一般指標埋點代碼實現(xiàn)上有兩種形式:AOP、侵入式,建議盡量使用AOP記錄指標,對于無法使用aop的場景就只能侵入代碼了。常用的AOP方式有:

  • @Aspect(通用)
  • HandlerInterceptor (SpringMVC的攔截器)
  • ClientHttpRequestInterceptor (RestTemplate的攔截器)
  • DubboFilter (dubbo接口)

我們選擇通用的@Aspect,結合自定義指標注解來實現(xiàn)。首先自定義指標注解:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodMetrics {
    String name() default "";
    String desc() default "";
    String[] tags() default {};
    //是否記錄時間間隔
    boolean withoutDuration() default false;
}

然后是切面實現(xiàn):

@Aspect
public class PrometheusAnnotationAspect {
 
    @Autowired
    private MeterRegistry meterRegistry;
 
    @Pointcut("@annotation(com.smac.prometheus.annotation.MethodMetrics)")
    public void pointcut() {}
 
    @Around(value = "pointcut()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
        Method targetMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Method currentMethod = ClassUtils.getUserClass(joinPoint.getTarget().getClass()).getDeclaredMethod(targetMethod.getName(), targetMethod.getParameterTypes());
        if (currentMethod.isAnnotationPresent(MethodMetrics.class)) {
            MethodMetrics methodMetrics = currentMethod.getAnnotation(MethodMetrics.class);
            return processMetric(joinPoint, currentMethod, methodMetrics);
        } else {
            return joinPoint.proceed();
        }
    }
 
    private Object processMetric(ProceedingJoinPoint joinPoint, Method currentMethod, MethodMetrics methodMetrics) {
        String name = methodMetrics.name();
        if (!StringUtils.hasText(name)) {
            name = currentMethod.getName();
        }
        String desc = methodMetrics.desc();
        if (!StringUtils.hasText(desc)) {
            desc = currentMethod.getName();
        }
        //不需要記錄時間
        if (methodMetrics.withoutDuration()) {
            Counter counter = Counter.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);
            try {
                return joinPoint.proceed();
            } catch (Throwable e) {
                throw new IllegalStateException(e);
            } finally {
                counter.increment();
            }
        }
        //需要記錄時間(默認)
        Timer timer = Timer.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);
        return timer.record(() -> {
            try {
                return joinPoint.proceed();
            } catch (Throwable e) {
                throw new IllegalStateException(e);
            }
        });
    }
}

代碼很容易,沒什么可說明的,接下來就是在需要記監(jiān)控的地方加上這個注解就行,比如:

@MethodMetrics(name="sms_send",tags = {"vendor","aliyun"})
public void send(String mobile, SendMessage message) throws Exception {
    ...
}

至此,aop形式的指標實現(xiàn)方式就完成了。如果是侵入式的話,直接使用meterRegistry就行:

meterRegistry.counter("sms.send","vendor","aliyun").increment();

啟動服務,打開http://localhost:8080/actuator/prometheus查看指標。

三、高級指標之分位數(shù)

分位數(shù)(P50/P90/P95/P99)是我們常用的一個性能指標,Prometheus提供了兩種解決方案:

client側計算方案

summery類型,設置percentiles,在本地計算出Pxx,作為指標的一個tag被直接收集。

Timer timer = Timer.builder("sms.send").publishPercentiles(0.5, 0.9, 0.95,0.99).register(meterRegistry);
timer.record(costTime, TimeUnit.MILLISECONDS);

會出現(xiàn)四個帶quantile的指標,如圖:

server側計算方案

開啟histogram,將所有樣本放入buckets中,在server側通過histogram_quantile函數(shù)對buckets進行實時計算得出。注意:histogram采用了線性插值法,buckets的劃分對誤差的影響比較大,需合理設置。

Timer timer = Timer.builder("sms.send")
                .publishPercentileHistogram(true)
                .serviceLevelObjectives(Duration.ofMillis(10),Duration.ofMillis(20),Duration.ofMillis(50))
                .minimumExpectedValue(Duration.ofMillis(1))
                .maximumExpectedValue(Duration.ofMillis(100))
                .register(meterRegistry);
timer.record(costTime, TimeUnit.MILLISECONDS);

會出現(xiàn)一堆xxxx_bucket的指標,如圖:

然后,使用

histogram_quantile(0.95, rate(sms_send_seconds_bucket[5m]))

就可以看到P95的指標了,如圖:

結論:

方案1適用于單機或只關心本地運行情況的指標,比如gc時間、定時任務執(zhí)行時間、本地緩存更新時間等;

方案2則適用于分布式環(huán)境下的整體運行情況的指標,比如搜索接口的響應時間、第三方接口的響應時間等。

到此這篇關于SpringBoot 使用Prometheus采集自定義指標數(shù)據(jù)的文章就介紹到這了,更多相關SpringBoot采集自定義指標數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot?LiteFlow引擎框架使用原理解析

    SpringBoot?LiteFlow引擎框架使用原理解析

    LiteFlow是一個輕量且強大的國產規(guī)則引擎框架,可用于復雜的組件化業(yè)務的編排領域,本文給大家介紹SpringBoot?LiteFlow引擎框架的相關操作,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • java網(wǎng)上圖書商城(1)User模塊

    java網(wǎng)上圖書商城(1)User模塊

    這篇文章主要介紹了java網(wǎng)上圖書商城,User模塊,實現(xiàn)用戶注冊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Spring Cloud Ribbon客戶端詳細介紹

    Spring Cloud Ribbon客戶端詳細介紹

    Spring Cloud Ribbon 是一套基于 Netflix Ribbon 實現(xiàn)的客戶端負載均衡和服務調用工具。通過Spring Cloud的封裝,可以讓我們輕松地將面向服務的REST模版請求自動轉換成客戶端負載均衡的服務調用
    2022-09-09
  • Spring Boot結合IDEA自帶Maven插件如何快速切換profile

    Spring Boot結合IDEA自帶Maven插件如何快速切換profile

    IDEA是目前 Java 開發(fā)者中使用最多的開發(fā)工具,它有著簡約的設計風格,強大的集成工具,便利的快捷鍵,這篇文章主要介紹了Spring Boot結合IDEA自帶Maven插件快速切換profile,需要的朋友可以參考下
    2023-03-03
  • 解決SpringBoot返回結果如果為null或空值不顯示處理問題

    解決SpringBoot返回結果如果為null或空值不顯示處理問題

    這篇文章主要介紹了解決SpringBoot返回結果如果為null或空值不顯示處理問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java代碼實現(xiàn)Map和Object互轉及Map和Json互轉

    Java代碼實現(xiàn)Map和Object互轉及Map和Json互轉

    這篇文章主要介紹了Java代碼實現(xiàn)map和Object互轉及Map和json互轉的相關資料,需要的朋友可以參考下
    2016-05-05
  • 最新評論