- 資訊首頁(yè) > 網(wǎng)絡(luò )安全 >
- 如何自定義Prometheus監控指標
本篇內容介紹了“如何自定義Prometheus監控指標”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!
目前大部分使用Spring Boot構建微服務(wù)體系的公司,大都在使用Prometheus來(lái)構建微服務(wù)的度量指標(Metrics)類(lèi)監控系統。而一般做法是通過(guò)在微服務(wù)應用中集成Prometheus指標采集SDK,從而使得Spring Boot暴露相關(guān)Metrics采集端點(diǎn)來(lái)實(shí)現。
但一般來(lái)說(shuō),Spring Boot默認暴露的Metrics數量及類(lèi)型是有限的,如果想要建立針對微服務(wù)應用更豐富的監控維度(例如TP90/TP99分位值指標之類(lèi)),那么還需要我們在Spring Boot默認已經(jīng)打開(kāi)的Metrics基礎之上,配置Prometheus類(lèi)庫(micrometer-registry-prometheus)所提供的其他指標類(lèi)型。
但怎么樣才能在Spring Boot框架中以更優(yōu)雅地方式實(shí)現呢?難道需要在業(yè)務(wù)代碼中編寫(xiě)各種自定義監控指標代碼的暴露邏輯嗎?接下來(lái)的內容我們將通過(guò)@注解+AOP的方式來(lái)演示如何以更加優(yōu)雅的方式來(lái)實(shí)現Prometheus監控指標的自定義!
需要說(shuō)明的是在Spring Boot應用中,對程序運行信息的收集(如指標、日志),比較常用的方法是通過(guò)Spring的AOP代理攔截來(lái)實(shí)現,但這種攔截程序運行過(guò)程的邏輯多少會(huì )損耗點(diǎn)系統性能,因此在自定義Prometheus監控指標的過(guò)程中,可以將是否上報指標的選擇權交給開(kāi)發(fā)人員,而從易用性角度來(lái)說(shuō),可以通過(guò)注解的方式實(shí)現。例如:
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è)用于標注上報計時(shí)器指標類(lèi)型的注解,如果想統計接口的想TP90、TP99這樣的分位值指標,那么就可以通過(guò)該注解標注。除此之外,還可以定義上報其他指標類(lèi)型的注解,例如:
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è)用于上報計數器類(lèi)型指標的注解!如果要統計接口的平均響應時(shí)間、接口的請求量之類(lèi)的指標,那么可以通過(guò)該注解標注!
而如果覺(jué)得分別定義不同指標類(lèi)型的注解比較麻煩,對于某些接口上述各種指標類(lèi)型都希望上報到Prometheus,那么也可以定義一個(gè)通用注解,用于同時(shí)上報多個(gè)指標類(lèi)型,例如:
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 ""; }
總之,無(wú)論是分開(kāi)定義特定指標注解還是定義一個(gè)通用的指標注解,其目標都是希望以更靈活的方式來(lái)擴展Spring Boot微服務(wù)應用的監控指標類(lèi)型。
上面我們靈活定義了上報不同指標類(lèi)型的注解,而上述注解的具體實(shí)現邏輯,可以通過(guò)定義一個(gè)通用的AOP代理類(lèi)來(lái)實(shí)現,具體實(shí)現代碼如下:
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指標管理 */ private MeterRegistry registry; private Function<ProceedingJoinPoint, Iterable<Tag>> 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<ProceedingJoinPoint, Iterable<Tag>> tagsBasedOnJoinPoint) { this.registry = registry; this.tagsBasedOnJoinPoint = tagsBasedOnJoinPoint; } /** * 針對@Tp指標配置注解的邏輯實(shí)現 */ @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 )建定義計數器,并設置指標的Tags信息(名稱(chēng)可以自定義) 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指標配置注解的邏輯實(shí)現 */ @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 )建定義計數器,并設置指標的Tags信息(名稱(chēng)可以自定義) 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通用指標配置注解的邏輯實(shí)現 */ @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; //計時(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); //計數器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í)現了前面我們定義的指標配置注解的邏輯,其中針對@Monitor注解的邏輯就是@Tp和@Count注解邏輯的整合。如果還需要定義其他指標類(lèi)型,可以在此基礎上繼續擴展!
需要注意,在上述邏輯實(shí)現中對“Timer”及“Counter”等指標類(lèi)型的構建這里并沒(méi)有直接使用“micrometer-registry-prometheus”依賴(lài)包中的構建對象,而是通過(guò)自定義的Metrics.newTimer()這樣的方式實(shí)現,其主要用意是希望以更簡(jiǎn)潔、靈活的方式去實(shí)現指標的上報,其代碼定義如下:
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, Consumer<Builder> consumer) { MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); return new CounterBuilder(meterRegistry, name, consumer).build(); } public static Timer newTimer(String name, Consumer<Timer.Builder> consumer) { return new TimerBuilder(context.getBean(MeterRegistry.class), name, consumer).build(); } }
上述代碼通過(guò)接入Spring容器上下文,獲取了MeterRegistry實(shí)例,并以此來(lái)構建像Counter、Timer這樣的指標類(lèi)型對象。而這里之所以將獲取方法定義為靜態(tài)的,主要是便于在業(yè)務(wù)代碼中進(jìn)行引用!
而在上述代碼中涉及的CounterBuilder、TimerBuilder構造器代碼定義分別如下:
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 Consumer<Builder> consumer; public CounterBuilder(MeterRegistry meterRegistry, String name, Consumer<Counter.Builder> consumer) { this.builder = Counter.builder(name); this.meterRegistry = meterRegistry; this.consumer = consumer; } public Counter build() { consumer.accept(builder); return builder.register(meterRegistry); } }
上述代碼為CounterBuilder構造器代碼!TimerBuilder構造器代碼如下:
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 Consumer<Builder> consumer; public TimerBuilder(MeterRegistry meterRegistry, String name, Consumer<Timer.Builder> consumer) { this.builder = Timer.builder(name); this.meterRegistry = meterRegistry; this.consumer = consumer; } public Timer build() { this.consumer.accept(builder); return builder.register(meterRegistry); } }
之所以還特地將構造器代碼單獨定義,主要是從代碼的優(yōu)雅性考慮!如果涉及其他指標類(lèi)型的構造,也可以通過(guò)類(lèi)似的方法進(jìn)行擴展!
在上述代碼中我們已經(jīng)定義了幾個(gè)自定義指標注解及其實(shí)現邏輯代碼,為了使其在Spring Boot環(huán)境中運行,還需要編寫(xiě)如下配置類(lèi),代碼如下:
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 MeterRegistryCustomizer<MeterRegistry> meterRegistryCustomizer(Environment environment) { return registry -> { registry.config() .commonTags("application", environment.getProperty("spring.application.name")); }; } @Bean @ConditionalOnMissingBean public Metrics metrics() { return new Metrics(); } }
上述配置代碼主要是約定了上報Prometheus指標信息中所攜帶的應用名稱(chēng),并對自定義了Metrics類(lèi)進(jìn)行了Bean配置!
接下來(lái)我們演示在業(yè)務(wù)代碼中如果要上報Prometheus監控指標應該怎么寫(xiě),具體如下:
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; //監控指標注解使用 //@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 "監控示范工程測試接口返回->OK!"; } }
如上述代碼所示,在實(shí)際的業(yè)務(wù)編程中就可以比較簡(jiǎn)單的通過(guò)注解來(lái)配置接口所上傳的Prometheus監控指標了!此時(shí)在本地啟動(dòng)程序,可以通過(guò)訪(fǎng)問(wèn)微服務(wù)應用的“/actuator/prometheus”指標采集端點(diǎn)來(lái)查看相關(guān)指標,如下圖所示:
有了這些自定義上報的監控指標,那么Promethues在采集后,我們就可以通過(guò)像Grafana這樣的可視化工具,來(lái)構建起多維度界面友好地監控視圖了,例如以TP90/TP99為例:
如上所示,在Grafana中可以同時(shí)定義多個(gè)PromeQL來(lái)定于不同的監控指標信息,這里我們分別通過(guò)Prometheus所提供的“histogram_quantile”函數統計了接口方法“monitorTest”的TP90及TP95分位值!而所使用的指標就是自定義的“tp_method_timed_xx”指標類(lèi)型!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站