首页 > 其他分享 >构建企业级监控大屏 prometheus + grafana

构建企业级监控大屏 prometheus + grafana

时间:2024-08-14 19:58:37浏览次数:8  
标签:exporter request grafana application prometheus import 企业级

1 prometheus下载安装

1.1 虚机部署

https://prometheus.io/download/

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar -xvf prometheus-2.53.0.linux-amd64.tar.gz
cd /home/prometheus-2.53.0.linux-amd64
./prometheus 

1.2 容器部署

# 1下载
docker pull prom/prometheus
# 2创建prometheus配置文件
mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml
# 3启动
docker run -d --name prometheus --restart=always -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

1.3 prometheus.yml配置

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"
    static_configs:
      - targets: ["9.135.95.39:9090"]

访问地址:http://127.0.0.1:9090/targets?search=

2 grafana下载安装

2.1 虚机部署

Download Grafana | Grafana Labs

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.3.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-11.1.0.linux-amd64.tar.gz
cd /home/grafana-11.1.0
bin/grafana-server web

2.2 容器部署

docker run -d --name=grafana -p 3000:3000 grafana/grafana-enterprise-dev:11.1.0-70874

2.3 数据源配置

http://127.0.0.1:3000

默认账号密码:admin/admin

3 node监控

3.1 node_exporter下载

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
tar -xvf node_exporter-1.8.1.linux-amd64.tar.gz
cd /home/ode_exporter-1.8.1.linux-amd64
./node_exporter

3.2 prometheus-job配置

scrape_configs:
  - job_name: "node"
    static_configs:
      - targets: ["9.135.95.39:9100"]

3.3 grafana监控面板

https://grafana.com/grafana/dashboards/12633-linux/

4 mysql监控

4.1 mysqld_exporter下载

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
tar -xvf mysqld_exporter-0.15.1.linux-amd64.tar.gz
cd /home/mysqld_exporter-0.15.1.linux-amd64
# 2修改配置
# cat mysql.cnf
[client]
user=root
password=admin123456
host=192.168.1.139
port=13306
# 3启动
nohup ./mysqld_exporter --web.listen-address=192.168.1.199:9104 --config.my-cnf=mysql.cnf &

4.2 prometheus-job配置

scrape_configs:
  - job_name: "mysql"
    static_configs:
      - targets: ["9.135.95.39:9104"]

4.3 grafana监控面板

https://grafana.com/grafana/dashboards/15211-mysql/

5 springboot应用监控

5.1 springboot应用配置

5.1.1 prometheus pom依赖配置

<!-- Spring Boot Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.7.15</version>
</dependency>
<!-- Micrometer Prometheus Registry -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.6.4</version>
</dependency>

5.1.2 application.yaml配置

spring:
  application:
    name: metrics-demo
server:
  port: 8084    

#Prometheus springboot监控配置
management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    export:
      prometheus:
        enabled: true
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application l

5.1.3 MeterRegistryCustomizer bean

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
        @Value("${spring.application.name}") String applicationName) {
    return (registry) -> registry.config().commonTags("application", applicationName);
}

访问 http://9.139.95.39:8084/actuator/prometheus

5.2 prometheus-job配置

scrape_configs:
  - job_name: "metrics-demo"
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ["9.135.95.39:8084"]

5.3 grafana监控面板

https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

5.4 PromQL 内置函数

# QPS统计
sum(rate(http_server_requests_seconds_count{application="metrics-demo"}[10s]))
# 每个接口QPS统计
rate(http_server_requests_seconds_count{application="metrics-demo"}[10s])

# 耗时统计
sum(rate(http_server_requests_seconds_sum{application="metrics-demo"}[10s])) / sum(rate(http_server_requests_seconds_count{application="metrics-demo"}[10s]))
# 每个接口的耗时
rate(http_server_requests_seconds_sum{application="metrics-demo"}[10s])

5.5 自定义指标

通过拦截器实现 MetricInterceptor.java

package com.demo.interceptor;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;

/**
 * 自定义指标上报
 * @author xfgeng
 * @date 2024-08-14 15:16
 */
public class MetricInterceptor extends HandlerInterceptorAdapter {

    private MeterRegistry meterRegistry;
    private ThreadLocal<Timer.Sample> threadLocal = new ThreadLocal<>();

    public MetricInterceptor(MeterRegistry meterRegistry){
        this.meterRegistry = meterRegistry;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 总计数 + 1
        meterRegistry.counter("api_request_count", Tags.of("url", request.getRequestURI(), "method", request.getMethod())).increment();
        // 处理中计数 +1
        meterRegistry.gauge("api_process_count", Tags.of("url", request.getRequestURI(), "method", request.getMethod()), 1);

        Timer.Sample sample = Timer.start();
        threadLocal.set(sample);

        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        try {
            super.postHandle(request, response, handler, modelAndView);
        } finally {
            meterRegistry.gauge("api_process_count", Tags.of("url", request.getRequestURI(), "method", request.getMethod()), -1);
            //  Timer timer = meterRegistry.timer("micro_req_histogram", Tags.of("url", request.getRequestURI(), "method", request.getMethod(), "code", String.valueOf(response.getStatus())));
            Timer timer = Timer.builder("api_request_histogram").minimumExpectedValue(Duration.ofMillis(1)).maximumExpectedValue(Duration.ofMinutes(3))
                    .sla(Duration.ofMillis(10), Duration.ofMillis(50), Duration.ofMillis(100), Duration.ofMillis(300), Duration.ofMillis(1000))
                    .tags(Tags.of("url", request.getRequestURI(), "method", request.getMethod(), "code", String.valueOf(response.getStatus())))
                    .register(meterRegistry);
            threadLocal.get().stop(timer);
            threadLocal.remove();
        }
    }

}
WebMvcConfig.java
package com.demo.config;

import com.demo.interceptor.MetricInterceptor;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private MeterRegistry meterRegistry;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MetricInterceptor(meterRegistry))
                .addPathPatterns("/api/**") //拦截路径
                .excludePathPatterns("/*.png","/*.gif"); //排除路径
    }

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
            @Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
}

标签:exporter,request,grafana,application,prometheus,import,企业级
From: https://blog.csdn.net/drea_mer/article/details/141197587

相关文章

  • OpenCloudOS 8.10 发布:全面兼容企业级 Linux 生态 ,引入更多新 module
    2024年8月,OpenCloudOS社区年度新版本OpenCloudOS8.10正式发布。全面兼容RHEL 8.10用户态包,引入Python3.12软件包,更新GCCToolset13、LLVMToolset17,RustToolset1.75.0,GoToolset1.21.0等编译器和开发工具。OpenCloudOS8.10采用Kernel5.4LTS版本(内核小......
  • Grafana的仪表盘URL参数设置
    转载请注明出处:在调试grafana时,需要对grafana进行share的url图表进行一些自定义设置,总结下常用的参数参考网站:https://grafana.com/docs/grafana/latest/dashboards/share-dashboards-panels/#dashboard-share-url-parameters1.参数列表orgId:指定组织的ID,用于加载特......
  • grom接入Prometheus,grafana
    在同级目录下分别创建docker-compose.yml,与prometheus.yml配置文件version:'3.8'services:prometheus:image:prom/prometheuscontainer_name:prometheusports:-"9090:9090"#PrometheusWebUI端口volumes:-./promethe......
  • 搭建基于Grafana+JMeter+InfluxDB的性能监控与分析平台(Linux版)
    搭建基于Grafana+JMeter+InfluxDB的性能监控与分析平台(Linux版)在软件开发和运维领域,性能监控与分析是确保应用稳定性和用户体验的关键环节。随着应用规模的不断扩大和复杂度的增加,传统的性能测试工具和方法已难以满足需求。本文将详细介绍如何搭建一个基于Grafana、JMeter和Influ......
  • prometheus监控mysql数据库
    监控需要安装一个工具mysqld_exportermysqld_exporter-0.14.0.linux-386.tar.gz上传到服务器后,解压压缩包tar-zxvf mysqld_exporter-0.14.0.linux-386.tar.gzcd mysqld_exporter-0.14.0.linux-386touchmy.cnfvimy.cnf加入有权限的用户名,密码,数据库地址等信息。[cli......
  • prometheus监控springboot项目配置
    项目运行状况,如果想要了解,那么项目需要增加如下配置,才能被prometheus发现。1、pom.xml增加maven依赖我这项目是maven项目,所以增加的是maven依赖。我这做了统一的版本管理,所以依赖信息中的version去掉了。<!--监控prometheus--><dependency><gr......
  • 监控工具prometheus配置-docker版
    1、安装直接创建一个docker-compose.yml内容增加networks:为了网络在同一个网段,方便通信,增加一个网段。services:prometheus:安装docker镜像的信息配置。定义了镜像来源image,容器名称container_name,重点是吧prometheus.yml放入到容器内的卷映射,对外访问端口portsgrafana:用于U......
  • 打造高效存储与访问体验:NFS共享携手Nginx负载均衡,赋能企业级数据流通与性能优化
     作者简介:我是团团儿,是一名专注于云计算领域的专业创作者,感谢大家的关注 座右铭:   云端筑梦,数据为翼,探索无限可能,引领云计算新纪元 个人主页:团团-CSDN博客前言:随着业务的增长,公司需要更多的服务器来支持用户访问和应用程序的运行。NFS共享可以解决文件存储的问题,而n......
  • Prometheus系列二进制部署
    Prometheus二进制部署官网下载prometheusDownload|Prometheus解压压缩包tar-zxvfprometheus-2.54.0.linux-amd64.tar.gz移动到安装路径下mv./prometheus-2.54.0.linux-amd64/usr/local/bin/prometheus创建启动用户(可选)sudouseradd-rs/bin/falseprometheu......
  • Golang语言之Prometheus的日志模块使用案例
    作者:尹正杰版权声明:原创作品,谢绝转载!否则将追究法律责任。目录一.源代码编写二.编译三.测试一.源代码编写packagemainimport( "fmt" "os" "path/filepath" "time" "g......