Springboot使用 prometheus监控

来源:互联网 发布:微软办公软件 win10 编辑:程序博客网 时间:2024/06/06 03:39
添加prometheus的Maven坐标
  1. <dependency>
  2. <groupId>io.prometheus</groupId>
  3. <artifactId>simpleclient_spring_boot</artifactId>
  4. <version>0.0.26</version>
  5. </dependency>
启动类增加prometheus注解
  1. package com.newcapec;
  2. import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
  3. import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.boot.SpringApplication;
  8. import org.springframework.boot.autoconfigure.SpringBootApplication;
  9. /**
  10. * @version V1.0
  11. * @Title: 应用主类
  12. * @ClassName: com.newcapec.SpringbootdemoApplication.java
  13. * @Description:
  14. * @Copyright 2016-2017 - Powered By 研发中心
  15. * @author: fly
  16. * @date:2017-03-15 8:01
  17. */
  18. @SpringBootApplication
  19. @EnablePrometheusEndpoint
  20. @EnableSpringBootMetricsCollector
  21. @MapperScan("com.newcapec.dao.mapper")
  22. public class SpringbootdemoApplication {
  23. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  24. public static void main(String[] args) {
  25. SpringApplication.run(SpringbootdemoApplication.class, args);
  26. /* SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringbootdemoApplication.class);
  27. //修改Banner的模式为OFF
  28. builder.bannerMode(Banner.Mode.OFF).run(args);*/
  29. }
  30. }
配置文件设置
  1. application.xml里设置属性:spring.metrics.servo.enabled=false
  2. 去掉重复的metrics,不然在prometheus的控制台的targets页签里,会一直显示此endpointdown状态。
  1. #应用可视化监控
  2. management.security.enabled=false
  3. spring.metrics.servo.enabled=false
配置prometheus.yml,指定SpringBoot应用的ip和端口
  • 可参考:http://blog.csdn.net/fly910905/article/details/78618534
  1. static_configs:
  2. - targets: ['IP:端口号']
自定义prometheus注解
  1. package com.newcapec.config.annotation;
  2. import java.lang.annotation.*;
  3. @Target(ElementType.METHOD)
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. public @interface PrometheusMetrics {
  7. /**
  8. * 默认为空,程序使用method signature作为Metric name
  9. * 如果name有设置值,使用name作为Metric name
  10. * @return
  11. */
  12. String name() default "";
  13. }
自定义prometheus切面
  1. package com.newcapec.config.aspect;
  2. import com.newcapec.config.annotation.PrometheusMetrics;
  3. import groovy.util.logging.Slf4j;
  4. import io.prometheus.client.Counter;
  5. import io.prometheus.client.Histogram;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.aspectj.lang.ProceedingJoinPoint;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.context.request.RequestContextHolder;
  14. import org.springframework.web.context.request.ServletRequestAttributes;
  15. import javax.servlet.http.HttpServletRequest;
  16. @Aspect
  17. @Component
  18. @Slf4j
  19. public class PrometheusMetricsAspect {
  20. private static final Counter requestTotal = Counter.build().name("couter_all").labelNames("api").help
  21. ("total request couter of api").register();
  22. private static final Counter requestError = Counter.build().name("couter_error").labelNames("api").help
  23. ("response Error couter of api").register();
  24. private static final Histogram histogram = Histogram.build().name("histogram_consuming").labelNames("api").help
  25. ("response consuming of api").register();
  26. // 自定义Prometheus注解的全路径
  27. @Pointcut("@annotation(com.newcapec.config.annotation.PrometheusMetrics)")
  28. public void pcMethod() {
  29. }
  30. @Around(value="pcMethod() && @annotation(annotation)")
  31. public Object MetricsCollector(ProceedingJoinPoint joinPoint, PrometheusMetrics annotation) throws Throwable {
  32. MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
  33. PrometheusMetrics prometheusMetrics = methodSignature.getMethod().getAnnotation(PrometheusMetrics.class);
  34. if (prometheusMetrics != null) {
  35. String name;
  36. if (StringUtils.isNotEmpty(prometheusMetrics.name()) ){
  37. name = prometheusMetrics.name();
  38. }else{
  39. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
  40. .getRequest();
  41. name = request.getRequestURI();
  42. }
  43. requestTotal.labels(name).inc();
  44. Histogram.Timer requestTimer = histogram.labels(name).startTimer();
  45. Object object;
  46. try {
  47. object = joinPoint.proceed();
  48. } catch (Exception e) {
  49. requestError.labels(name).inc();
  50. throw e;
  51. } finally {
  52. requestTimer.observeDuration();
  53. }
  54. return object;
  55. } else {
  56. return joinPoint.proceed();
  57. }
  58. }
  59. }
被监控的方法上添加--自定义prometheus注解
  1. @RequestMapping(value ="/hello", method = RequestMethod.GET)
  2. @ResponseBody
  3. @PrometheusMetrics
  4. public String index() {
  5. return "Hello World";
  6. }
prometheus的监控页面
  1. http://localhost:9090/targets


  1. 多次访问 http://localhost:8888/web项目名/hello,然后在prometheus控制台查看相关metrics信息,couter_all,2个页签:Graph,Console

参考来源:http://blog.csdn.net/zl1zl2zl3/article/details/75045005?locationNum=9&fps=1
参考来源:http://blog.csdn.net/ericprotectearth/article/details/78581312

原创粉丝点击