zoukankan      html  css  js  c++  java
  • Prometheus+Grafana+SpringBoot业务埋点可视化监控

    引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    

    application.yml文件

    server:
      port: 8010
    spring:
      application:
        name: spring-boot-prometheus-grafana
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      endpoint:
        health:
          show-details: always
    

    controller中埋点

    @RestController
    @RequestMapping("/api")
    public class MainController {
    
        @Autowired
        MeterRegistry registry;
    
        private Counter counter;
        private Counter failCounter;
    
        @PostConstruct
        private void init(){
            counter = registry.counter("requests_add_total","save","carson");
            failCounter=  registry.counter("requests_add_fail_total","save","carson");
        }
    
        @GetMapping("/add")
        public String add(String name,Integer age) {
            counter.increment();
            if(RandomUtils.nextInt(0,1000)<185){
                failCounter.increment();
                return "error";
            }else{
                return "name="+name+"  age="+age;
            }
        }
    
    }
    

    在prometheus.yml添加

      - job_name: 'spring_boot_8010'
        metrics_path: /actuator/prometheus
        static_configs:
        - targets: ['127.0.0.1:8010']
    

    在grafana中添加展示图表



  • 相关阅读:
    博客园代码
    前端
    1338. Reduce Array Size to The Half
    1220. Count Vowels Permutation
    363. Max Sum of Rectangle No Larger Than K
    366. Find Leaves of Binary Tree
    443. String Compression
    8 · Rotate String
    886. Possible Bipartition
    LT 183 wood cut
  • 原文地址:https://www.cnblogs.com/daikainan/p/14443998.html
Copyright © 2011-2022 走看看