Spring Boot + prometheus + Grafana应用可视化监控

Spring Boot + prometheus + Grafana应用可视化监控

问题

  • 安装 prometheus + Grafana
  • spring boot 集成prometheus
  • prometheus自定义指标监控

Application prometheus Grafana三者关系

spring集成prometheus ,prometheus 进行数据信息采集,Grafana进行可视化监控,以及预警。

《Spring Boot + prometheus + Grafana应用可视化监控》

Prometheus Grafana 使用docker 安装

http://www.spring4all.com/article/265

安装的Prometheus 时候需要注意的是,自定义的prometheus.yml ,由于是使用docker启动的Prometheus ,所以需要将prometheus.yml挂载到docker上。比如我现在放在/etc/prometheus/prometheus.yml,那么我启动的命令则是:

root@iZbp1bh9k37nqyfytklbiyZ:~# docker run -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

prometheus.yml内容

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: spring-boot     scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /prometheus
    scheme: http
    static_configs:
      - targets:         - ip:port 
  • 附上docker的安装教程地址

    https://blog.csdn.net/nimoyaoww/article/details/79155489
  • 常用docker的命令

    • 查看docker运行的镜像
    root@iZbp1bh9k37nqyfytklbiyZ:~# docker ps
    • 关闭运行的镜像
    root@iZbp1bh9k37nqyfytklbiyZ:~# docker kill name

Prometheus自定义监控

  • 启动类加上@EnablePrometheusEndpoint@EnableSpringBootMetricsCollector注解,继承WebMvcConfigurerAdapter

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class MonitorApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(MonitorApplication.class, args);
    }
    @Override
    public void run(String... strings) throws Exception {
        DefaultExports.initialize();
    }
}
  • 覆写addInterceptors方法,添加拦截器
 @Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new PrometheusMetricsInterceptor()).addPathPatterns("/**");
}
  • 编写拦截器,在接口处理前置加上接口统计,使用Counter
package com.zz.prometheus;

import io.prometheus.client.Counter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** * @author wangteng * @desc * @date 2018/6/21 11:46 */
@Slf4j
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
    static final Counter requestCounter = Counter.build()
        .name("io_namespace_http_requests_total").labelNames("path", "method", "code")
        .help("Total requests.").register();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        int status = response.getStatus();
        log.info("request :" + requestURI);
        requestCounter.labels(requestURI, method, String.valueOf(status)).inc();
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }
}
  • 编写TestController
package com.zz.controller;

import com.google.gson.Gson;
import com.zz.config.api.ApiUtcValidator;
import com.zz.pojo.User;
import io.prometheus.client.Counter;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Random;

@RestController
public class TestController {
    private static Random random = new Random();
    @ApiOperation(value = "测试接口")
    @ApiUtcValidator
    @PostMapping("/test")
    public String test( @ApiParam @RequestBody @Valid User user) {
        return new Gson().toJson(user);
    }
}

重新打包,重启Spring项目。

Prometheus查询以及Grafana展示

《Spring Boot + prometheus + Grafana应用可视化监控》

  • Grafana展示

《Spring Boot + prometheus + Grafana应用可视化监控》

这里监控的是所有的接口,如果想单独查看某一个接口,可以后面加上条件{path="url"}

展示效果:

《Spring Boot + prometheus + Grafana应用可视化监控》

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/wagnteng/article/details/80762491
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞