应用监控(Prometheus + Grafana)

本文涉及的产品
EMR Serverless StarRocks,5000CU*H 48000GB*H
可观测监控 Prometheus 版,每月50GB免费额度
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 应用监控(Prometheus + Grafana)

可用于应用监控的系统有很多,有的需要埋点(切面)、有的需要配置Agent(字节码增强)。现在使用另外一个监控系统 —— Grafana。

Grafana 监控面板

这套监控主要用到了 SpringBoot Actuator + Prometheus + Grafana 三个模块组合的起来使用的监控。非常轻量好扩展使用。

  1. Actuator - 数据上报、Prometheus - 数据采集、Grafana - 数据展示
  2. 本章节的内容主要为代码中的配置和监控的配置。

环境配置

本节所需的监控配置,已经放到了 chatgpt-data 的 dev-ops 包下了。你只需要确保本地或者云服务器已经安装了Docker,那么就可以执行安装了。

1. Grafana.ini

这一部分是小傅哥通过第一次默认安装后,再通过 docker 脚本 docker container cp grafana:/etc/grafana/ ./docs/dev-ops/ 从容器中拷贝下来的配置。因为我们需要做一些默认的配置处理。

端口修改

# The http port to use

http_port = 4000

  1. Grafana 默认配置的是 3000 端口,但这个端口很多时候都被占用了。所以如果你的也占用了,那么可以在这里修改下。

连接配置

[database]

# You can configure the database connection by specifying type, host, name, user and password

# as separate properties or as on string using the url properties.

# Either "mysql", "postgres" or "sqlite3", it's your choice

type = mysql

host = host.docker.internal:3306

name = grafana

user = root

# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""

password = 123456

  1. 为了让 Grafana 的配置具有迁移性,也不至于删除在安装就丢失配置,那么这里可以选择配置数据库进行使用。
  2. 注意;你需要先在本地安装MySQL以及创建出一个grafana数据库。—— 连接后,会自动建表。

注意:host那里设置localhost或者ipv4地址都连接失败,设置host.docker.internal成功,目前不知道原因

2. datasource.yml

apiVersion: 1

datasources:

- name: Prometheus

type: prometheus

access: proxy

url: http://prometheus:9090

isDefault: true

  1. 注意;因为 Grafana 使用的是 Prometheus 数据源,所以你需要在这里配置上。当然也可以不配置,在启动的 Grafana 线上进行配置。

3. prometheus.yml

global:

scrape_interval: 15s

scrape_configs:

- job_name: 'x-api-app'

metrics_path: '/actuator/prometheus'

static_configs:

- targets: [ '192.168.158.77:8080' ]

  1. 这里配置的是 prometheus.yml 对需要采集的 SpringBoot 应用访问地址。注意你需要替换为你的服务器IP和服务端口。

应用配置

POM 配置

chatgpt-data-app 模块下

<!-- 监控;actuator-上报、prometheus-采集、grafana-展示 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
  1. actuator、prometheus 是监控所需的内容,aspectjweaver 是本节需要使用 prometheus 添加自定义的埋点,而这个会用到切面。

chatgpt-data-trigger 模块下

<!-- 监控;actuator-上报、prometheus-采集、grafana-展示 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

代码配置

1. 启动监听服务

@EnableAspectJAutoProxy
@Configuration
public class PrometheusConfiguration {
 
    @Bean
    public CollectorRegistry collectorRegistry() {
        return new CollectorRegistry();
    }
 
    @Bean
    public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig config, CollectorRegistry collectorRegistry) {
        return new PrometheusMeterRegistry(config, collectorRegistry, Clock.SYSTEM);
    }
 
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }
 
    @Bean
    public CountedAspect countedAspect(MeterRegistry registry) {
        return new CountedAspect(registry);
    }
 
}

2. 自定义监控埋点

@Timed(value = "no_pay_notify_order_job", description = "定时任务,订单支付状态更新")
    @Scheduled(cron = "0/3 * * * * ?")
    public void exec() {
    // ...
    }
  1. 你可以使用监控提供的注解,来对需要监控的方法进行埋点。@Timed 这样就可以采集到数据,在监控中配置了。

3. YML 配置

# 监控
management:
  endpoints:
    web:
      exposure:
        include: "*" # 暴露所有端点,包括自定义端点
  endpoint:
    metrics:
      enabled: true
    health:
      show-details: always # 显示详细的健康检查信息
  metrics:
    export:
      prometheus:
        enabled: true # 启用Prometheus
  prometheus:
    enabled: true # 启用Prometheus端点
  jmx:
    enabled: true # 启用JMX监控
  system:
    cpu:
      enabled: true # 启用CPU监控
    memory:
      enabled: true # 启用内存监控

可以访问 http://127.0.0.1:9090/service-discovery?search= - 查看采集数据。

监控配置

地址:http://127.0.0.1:4000/dashboards

介绍:Grafana 的监控,需要新建监控仪表。也可以使用导入功能。导入功能可以导入 Grafana 官网提供的各项模板,非常好用。

**常用代码**

4.1 CPU

sum(system_cpu_usage{job="x-api-app"}) / sum(system_cpu_count{job="x-api-app"}) * 100

4.2 磁盘

disk_free_bytes{job="x-api-app"}

4.3 连接池

hikaricp_connections{pool="HikariPool-1", job="x-api-app"}

4.4 请求量

sum by(instance, uri, exception) (increase(no_pay_notify_order_job_seconds_count{method="exec", job="x-api-app"}[5m]))

4.5 响应时间

sum by (uri) (increase(http_server_requests_seconds_sum{uri=~"/api/v1/chatgpt/chat/completions"}[1m]))

/sum by (uri) (increase(http_server_requests_seconds_count{uri=~"/api/v1/chatgpt/chat/completions"}[1m]))


目录
相关文章
|
25天前
|
Prometheus 监控 Cloud Native
基于Prometheus和Grafana的监控平台 - 环境搭建
基于Prometheus和Grafana的监控平台 - 环境搭建
|
2天前
|
JSON Prometheus 监控
Prometheus+Grafana 部署
Prometheus 和 Grafana 组成监控解决方案。Prometheus 是开源系统监控工具,Grafana 则用于数据可视化。要连接 Prometheus 数据源,登录 Grafana,点击设置,选择“连接”,添加新数据源,选择 Prometheus 类型,并填入 Prometheus 服务器的 HTTP 地址,如 `http://192.168.1.1:9090`,验证连接。之后,从 Grafana 官方仪表板库导入监控面板,如主机监控模板,以可视化系统状态。完成这些步骤后,便建立了有效的监控系统。
12 1
|
6天前
|
Prometheus 监控 Cloud Native
使用Spring Boot和Prometheus进行监控
使用Spring Boot和Prometheus进行监控
|
18天前
|
Prometheus 监控 Cloud Native
性能监控神器Prometheus、Grafana、ELK 在springboot中的运用
【6月更文挑战第27天】在 Spring Boot 应用中,监控和日志管理是确保系统稳定性和性能的重要手段。
37 4
|
18天前
|
Prometheus 监控 Kubernetes
深入理解Prometheus: Kubernetes环境中的监控实践
Kubernetes简介 在深入Prometheus与Kubernetes的集成之前,首先简要回顾一下Kubernetes的核心概念。Kubernetes是一个开源的容器编排平台,用于自动化容器的部署、扩展和管理。它提供了高度的可扩展性和灵活性,使得它成为微服务和云原生应用的理想选择。 核心组件 • 控制平面(Control Plane):集群管理相关的组件,如API服务器、调度器等。 • 工作节点(Nodes):运行应用容器的机器。 • Pods:Kubernetes的基本运行单位,可以容纳一个或多个容器。
|
1天前
|
JSON Prometheus 监控
Prometheus+Grafana主机运行数据
Prometheus 是一款开源监控工具,Node Exporter 是其官方插件,用于收集服务器硬件和系统指标。要安装 Node Exporter,下载最新二进制文件,解压并移动到目标位置。启动二进制文件或设置为服务。在 Prometheus 配置文件 `prometheus.yml` 中添加 Node Exporter 目标配置
8 0
|
1月前
|
Prometheus 监控 Cloud Native
SpringBoot2.x整合Prometheus+Grafana【附源码】
SpringBoot2.x整合Prometheus+Grafana【附源码】
27 1
|
1月前
|
存储 Prometheus 监控
【监控】grafana图表使用快速上手
【监控】grafana图表使用快速上手
24 0
|
2月前
|
编解码 Prometheus 运维
Prometheus 的监控方法论
【1月更文挑战第24天】
|
2月前
|
存储 Prometheus 监控
Prometheus vs. ELK Stack:容器监控与日志管理工具的较量
随着容器化技术的广泛应用,容器监控与日志管理成为了关键任务。本文将对两种常用工具进行比较与选择,分别是Prometheus和ELK Stack。Prometheus是一款开源的监控系统,专注于时序数据的收集和告警。而ELK Stack则是一套完整的日志管理解决方案,由Elasticsearch、Logstash和Kibana三个组件组成。通过比较它们的特点、优势和适用场景,读者可以更好地了解如何选择适合自己需求的工具。