本篇內(nèi)容介紹了“如何自定義Prometheus監(jiān)控指標(biāo)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
站在用戶的角度思考問題,與客戶深入溝通,找到立山網(wǎng)站設(shè)計(jì)與立山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋立山地區(qū)。
目前大部分使用Spring Boot構(gòu)建微服務(wù)體系的公司,大都在使用Prometheus來構(gòu)建微服務(wù)的度量指標(biāo)(Metrics)類監(jiān)控系統(tǒng)。而一般做法是通過在微服務(wù)應(yīng)用中集成Prometheus指標(biāo)采集SDK,從而使得Spring Boot暴露相關(guān)Metrics采集端點(diǎn)來實(shí)現(xiàn)。
但一般來說,Spring Boot默認(rèn)暴露的Metrics數(shù)量及類型是有限的,如果想要建立針對微服務(wù)應(yīng)用更豐富的監(jiān)控維度(例如TP90/TP99分位值指標(biāo)之類),那么還需要我們在Spring Boot默認(rèn)已經(jīng)打開的Metrics基礎(chǔ)之上,配置Prometheus類庫(micrometer-registry-prometheus)所提供的其他指標(biāo)類型。
但怎么樣才能在Spring Boot框架中以更優(yōu)雅地方式實(shí)現(xiàn)呢?難道需要在業(yè)務(wù)代碼中編寫各種自定義監(jiān)控指標(biāo)代碼的暴露邏輯嗎?接下來的內(nèi)容我們將通過@注解+AOP的方式來演示如何以更加優(yōu)雅的方式來實(shí)現(xiàn)Prometheus監(jiān)控指標(biāo)的自定義!
需要說明的是在Spring Boot應(yīng)用中,對程序運(yùn)行信息的收集(如指標(biāo)、日志),比較常用的方法是通過Spring的AOP代理攔截來實(shí)現(xiàn),但這種攔截程序運(yùn)行過程的邏輯多少會(huì)損耗點(diǎn)系統(tǒng)性能,因此在自定義Prometheus監(jiān)控指標(biāo)的過程中,可以將是否上報(bào)指標(biāo)的選擇權(quán)交給開發(fā)人員,而從易用性角度來說,可以通過注解的方式實(shí)現(xiàn)。例如:
package com.wudimanong.monitor.metrics.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Tp { String description() default ""; }
如上所示代碼,我們定義了一個(gè)用于標(biāo)注上報(bào)計(jì)時(shí)器指標(biāo)類型的注解,如果想統(tǒng)計(jì)接口的想TP90、TP99這樣的分位值指標(biāo),那么就可以通過該注解標(biāo)注。除此之外,還可以定義上報(bào)其他指標(biāo)類型的注解,例如:
package com.wudimanong.monitor.metrics.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Count { String description() default ""; }
如上所示,我們定義了一個(gè)用于上報(bào)計(jì)數(shù)器類型指標(biāo)的注解!如果要統(tǒng)計(jì)接口的平均響應(yīng)時(shí)間、接口的請求量之類的指標(biāo),那么可以通過該注解標(biāo)注!
而如果覺得分別定義不同指標(biāo)類型的注解比較麻煩,對于某些接口上述各種指標(biāo)類型都希望上報(bào)到Prometheus,那么也可以定義一個(gè)通用注解,用于同時(shí)上報(bào)多個(gè)指標(biāo)類型,例如:
package com.wudimanong.monitor.metrics.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Monitor { String description() default ""; }
總之,無論是分開定義特定指標(biāo)注解還是定義一個(gè)通用的指標(biāo)注解,其目標(biāo)都是希望以更靈活的方式來擴(kuò)展Spring Boot微服務(wù)應(yīng)用的監(jiān)控指標(biāo)類型。
上面我們靈活定義了上報(bào)不同指標(biāo)類型的注解,而上述注解的具體實(shí)現(xiàn)邏輯,可以通過定義一個(gè)通用的AOP代理類來實(shí)現(xiàn),具體實(shí)現(xiàn)代碼如下:
package com.wudimanong.monitor.metrics.aop; import com.wudimanong.monitor.metrics.Metrics; import com.wudimanong.monitor.metrics.annotation.Count; import com.wudimanong.monitor.metrics.annotation.Monitor; import com.wudimanong.monitor.metrics.annotation.Tp; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Timer; import java.lang.reflect.Method; import java.util.function.Function; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect @Component public class MetricsAspect { /** * Prometheus指標(biāo)管理 */ private MeterRegistry registry; private Function> tagsBasedOnJoinPoint; public MetricsAspect(MeterRegistry registry) { this.init(registry, pjp -> Tags .of(new String[]{"class", pjp.getStaticPart().getSignature().getDeclaringTypeName(), "method", pjp.getStaticPart().getSignature().getName()})); } public void init(MeterRegistry registry, Function > tagsBasedOnJoinPoint) { this.registry = registry; this.tagsBasedOnJoinPoint = tagsBasedOnJoinPoint; } /** * 針對@Tp指標(biāo)配置注解的邏輯實(shí)現(xiàn) */ @Around("@annotation(com.wudimanong.monitor.metrics.annotation.Tp)") public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable { Method method = ((MethodSignature) pjp.getSignature()).getMethod(); method = pjp.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes()); Tp tp = method.getAnnotation(Tp.class); Timer.Sample sample = Timer.start(this.registry); String exceptionClass = "none"; try { return pjp.proceed(); } catch (Exception ex) { exceptionClass = ex.getClass().getSimpleName(); throw ex; } finally { try { String finalExceptionClass = exceptionClass; //創(chuàng)建定義計(jì)數(shù)器,并設(shè)置指標(biāo)的Tags信息(名稱可以自定義) Timer timer = Metrics.newTimer("tp.method.timed", builder -> builder.tags(new String[]{"exception", finalExceptionClass}) .tags(this.tagsBasedOnJoinPoint.apply(pjp)).tag("description", tp.description()) .publishPercentileHistogram().register(this.registry)); sample.stop(timer); } catch (Exception exception) { } } } /** * 針對@Count指標(biāo)配置注解的邏輯實(shí)現(xiàn) */ @Around("@annotation(com.wudimanong.monitor.metrics.annotation.Count)") public Object countMethod(ProceedingJoinPoint pjp) throws Throwable { Method method = ((MethodSignature) pjp.getSignature()).getMethod(); method = pjp.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes()); Count count = method.getAnnotation(Count.class); String exceptionClass = "none"; try { return pjp.proceed(); } catch (Exception ex) { exceptionClass = ex.getClass().getSimpleName(); throw ex; } finally { try { String finalExceptionClass = exceptionClass; //創(chuàng)建定義計(jì)數(shù)器,并設(shè)置指標(biāo)的Tags信息(名稱可以自定義) Counter counter = Metrics.newCounter("count.method.counted", builder -> builder.tags(new String[]{"exception", finalExceptionClass}) .tags(this.tagsBasedOnJoinPoint.apply(pjp)).tag("description", count.description()) .register(this.registry)); counter.increment(); } catch (Exception exception) { } } } /** * 針對@Monitor通用指標(biāo)配置注解的邏輯實(shí)現(xiàn) */ @Around("@annotation(com.wudimanong.monitor.metrics.annotation.Monitor)") public Object monitorMethod(ProceedingJoinPoint pjp) throws Throwable { Method method = ((MethodSignature) pjp.getSignature()).getMethod(); method = pjp.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes()); Monitor monitor = method.getAnnotation(Monitor.class); String exceptionClass = "none"; try { return pjp.proceed(); } catch (Exception ex) { exceptionClass = ex.getClass().getSimpleName(); throw ex; } finally { try { String finalExceptionClass = exceptionClass; //計(jì)時(shí)器Metric Timer timer = Metrics.newTimer("tp.method.timed", builder -> builder.tags(new String[]{"exception", finalExceptionClass}) .tags(this.tagsBasedOnJoinPoint.apply(pjp)).tag("description", monitor.description()) .publishPercentileHistogram().register(this.registry)); Timer.Sample sample = Timer.start(this.registry); sample.stop(timer); //計(jì)數(shù)器Metric Counter counter = Metrics.newCounter("count.method.counted", builder -> builder.tags(new String[]{"exception", finalExceptionClass}) .tags(this.tagsBasedOnJoinPoint.apply(pjp)).tag("description", monitor.description()) .register(this.registry)); counter.increment(); } catch (Exception exception) { } } } }
上述代碼完整的實(shí)現(xiàn)了前面我們定義的指標(biāo)配置注解的邏輯,其中針對@Monitor注解的邏輯就是@Tp和@Count注解邏輯的整合。如果還需要定義其他指標(biāo)類型,可以在此基礎(chǔ)上繼續(xù)擴(kuò)展!
需要注意,在上述邏輯實(shí)現(xiàn)中對“Timer”及“Counter”等指標(biāo)類型的構(gòu)建這里并沒有直接使用“micrometer-registry-prometheus”依賴包中的構(gòu)建對象,而是通過自定義的Metrics.newTimer()這樣的方式實(shí)現(xiàn),其主要用意是希望以更簡潔、靈活的方式去實(shí)現(xiàn)指標(biāo)的上報(bào),其代碼定義如下:
package com.wudimanong.monitor.metrics; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Counter.Builder; import io.micrometer.core.instrument.DistributionSummary; import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import io.micrometer.core.lang.NonNull; import java.util.function.Consumer; import java.util.function.Supplier; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Metrics implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getContext() { return context; } public static Counter newCounter(String name, Consumerconsumer) { MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); return new CounterBuilder(meterRegistry, name, consumer).build(); } public static Timer newTimer(String name, Consumer consumer) { return new TimerBuilder(context.getBean(MeterRegistry.class), name, consumer).build(); } }
上述代碼通過接入Spring容器上下文,獲取了MeterRegistry實(shí)例,并以此來構(gòu)建像Counter、Timer這樣的指標(biāo)類型對象。而這里之所以將獲取方法定義為靜態(tài)的,主要是便于在業(yè)務(wù)代碼中進(jìn)行引用!
而在上述代碼中涉及的CounterBuilder、TimerBuilder構(gòu)造器代碼定義分別如下:
package com.wudimanong.monitor.metrics; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Counter.Builder; import io.micrometer.core.instrument.MeterRegistry; import java.util.function.Consumer; public class CounterBuilder { private final MeterRegistry meterRegistry; private Counter.Builder builder; private Consumerconsumer; public CounterBuilder(MeterRegistry meterRegistry, String name, Consumer consumer) { this.builder = Counter.builder(name); this.meterRegistry = meterRegistry; this.consumer = consumer; } public Counter build() { consumer.accept(builder); return builder.register(meterRegistry); } }
上述代碼為CounterBuilder構(gòu)造器代碼!TimerBuilder構(gòu)造器代碼如下:
package com.wudimanong.monitor.metrics; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.Timer.Builder; import java.util.function.Consumer; public class TimerBuilder { private final MeterRegistry meterRegistry; private Timer.Builder builder; private Consumerconsumer; public TimerBuilder(MeterRegistry meterRegistry, String name, Consumer consumer) { this.builder = Timer.builder(name); this.meterRegistry = meterRegistry; this.consumer = consumer; } public Timer build() { this.consumer.accept(builder); return builder.register(meterRegistry); } }
之所以還特地將構(gòu)造器代碼單獨(dú)定義,主要是從代碼的優(yōu)雅性考慮!如果涉及其他指標(biāo)類型的構(gòu)造,也可以通過類似的方法進(jìn)行擴(kuò)展!
在上述代碼中我們已經(jīng)定義了幾個(gè)自定義指標(biāo)注解及其實(shí)現(xiàn)邏輯代碼,為了使其在Spring Boot環(huán)境中運(yùn)行,還需要編寫如下配置類,代碼如下:
package com.wudimanong.monitor.metrics.config; import com.wudimanong.monitor.metrics.Metrics; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @Configuration public class CustomMetricsAutoConfiguration { @Bean @ConditionalOnMissingBean public MeterRegistryCustomizermeterRegistryCustomizer(Environment environment) { return registry -> { registry.config() .commonTags("application", environment.getProperty("spring.application.name")); }; } @Bean @ConditionalOnMissingBean public Metrics metrics() { return new Metrics(); } }
上述配置代碼主要是約定了上報(bào)Prometheus指標(biāo)信息中所攜帶的應(yīng)用名稱,并對自定義了Metrics類進(jìn)行了Bean配置!
接下來我們演示在業(yè)務(wù)代碼中如果要上報(bào)Prometheus監(jiān)控指標(biāo)應(yīng)該怎么寫,具體如下:
package com.wudimanong.monitor.controller; import com.wudimanong.monitor.metrics.annotation.Count; import com.wudimanong.monitor.metrics.annotation.Monitor; import com.wudimanong.monitor.metrics.annotation.Tp; import com.wudimanong.monitor.service.MonitorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/monitor") public class MonitorController { @Autowired private MonitorService monitorServiceImpl; //監(jiān)控指標(biāo)注解使用 //@Tp(description = "/monitor/test") //@Count(description = "/monitor/test") @Monitor(description = "/monitor/test") @GetMapping("/test") public String monitorTest(@RequestParam("name") String name) { monitorServiceImpl.monitorTest(name); return "監(jiān)控示范工程測試接口返回->OK!"; } }
如上述代碼所示,在實(shí)際的業(yè)務(wù)編程中就可以比較簡單的通過注解來配置接口所上傳的Prometheus監(jiān)控指標(biāo)了!此時(shí)在本地啟動(dòng)程序,可以通過訪問微服務(wù)應(yīng)用的“/actuator/prometheus”指標(biāo)采集端點(diǎn)來查看相關(guān)指標(biāo),如下圖所示:
有了這些自定義上報(bào)的監(jiān)控指標(biāo),那么Promethues在采集后,我們就可以通過像Grafana這樣的可視化工具,來構(gòu)建起多維度界面友好地監(jiān)控視圖了,例如以TP90/TP99為例:
如上所示,在Grafana中可以同時(shí)定義多個(gè)PromeQL來定于不同的監(jiān)控指標(biāo)信息,這里我們分別通過Prometheus所提供的“histogram_quantile”函數(shù)統(tǒng)計(jì)了接口方法“monitorTest”的TP90及TP95分位值!而所使用的指標(biāo)就是自定義的“tp_method_timed_xx”指標(biāo)類型!
“如何自定義Prometheus監(jiān)控指標(biāo)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!