SpringBoot 应用监控

2019-09-26  本文已影响0人  一个神奇的女码农

对JVM的监控

1.首先添加依赖

        <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_hotspot -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_servlet -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_servlet</artifactId>
            <version>0.6.0</version>
        </dependency>

2.启用Prometheus Metrics
添加注解@EnablePrometheusEndpoint

@SpringBootApplication
@EnablePrometheusEndpoint
public class CoreApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(CoreApplication.class);
        springApplication.run(args);
    }

    @Override
    public void run(String... strings) throws Exception {
        DefaultExports.initialize();
    }
}

向外暴露接口

@Configuration
public class MonitoringConfig {

    @Bean
    ServletRegistrationBean servletRegistrationBean() {

        return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
    }
}

访问 localhost:8080/metrics 可以看到jvm相关的指标.


jvm指标.png

添加拦截器,对所有接口进行拦截

@SpringBootApplication
@EnablePrometheusEndpoint
public class CoreApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PrometheusMetricsInterceptor()).addPathPatterns("/**");
    }
}

自定义指标

@Component
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
    // 创建计数器
    static final Counter requestCounter = Counter.build()
            // 计数器名字 必填
            .name("module_core_http_requests_total")
            // 计数器标签 选填
            .labelNames("path", "method", "code")
            // 设计量度 必填
            .help("Total requests.").register();

    static final Gauge inprogressRequests = Gauge.build()
            .name("module_core_http_inprogress_requests").labelNames("path", "method", "code")
            .help("Inprogress requests.").register();

    static final Gauge requestTime = Gauge.build()
            .name("module_core_http_requests_costTime").labelNames("path", "method", "code")
            .help("requests cost time.").register();

    static final Histogram requestLatencyHistogram = Histogram.build().labelNames("path", "method", "code")
            .name("module_core_http_requests_latency_seconds_histogram").help("Request latency in seconds.")
            .register();

    static final Summary requestLatency = Summary.build()
            .name("module_core_http_requests_latency_seconds_summary")
            // 添加第50百分位数(=中位数),允许误差为5%
            .quantile(0.5, 0.05)
            // 添加第90个百分位数,允许误差为1%
            .quantile(0.9, 0.01)
            .labelNames("path", "method", "code")
            .help("Request latency in seconds.").register();
    private Histogram.Timer histogramRequestTimer;

    private Summary.Timer summaryTimer;

    private Gauge.Timer gaugeTimer;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        int status = response.getStatus();
        inprogressRequests.labels(requestURI, method, String.valueOf(status)).inc();
        histogramRequestTimer = requestLatencyHistogram.labels(requestURI, method, String.valueOf(status)).startTimer();
        summaryTimer = requestLatency.labels(requestURI, method, String.valueOf(status)).startTimer();
        gaugeTimer = requestTime.labels(requestURI, method, String.valueOf(status)).startTimer();
        return super.preHandle(request, response, handler);
    }
}

然后就可以去Prometheus监控指标了,搭建可以参考我上次写的文档

https://www.jianshu.com/p/4adfc5d111b6

我们这边直接开始配置文件
修改配置文件prometheus.yml(需要注意的是格式必须要正确,连个空格都不能多)
添加:

- job_name: 'jvm_test'
        static_configs:
         - targets: ['192.168.1.200:8080']
           labels:
            instance: 'jvm-test

添加完成之后重启服务即可

添加指标

添加完成之后效果图


指标.png
上一篇 下一篇

猜你喜欢

热点阅读