应用监控(Prometheus + Grafana)

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


相关实践学习
通过可观测可视化Grafana版进行数据可视化展示与分析
使用可观测可视化Grafana版进行数据可视化展示与分析。
目录
相关文章
|
2月前
|
Prometheus 监控 Kubernetes
Prometheus 在微服务架构中的应用
【8月更文第29天】随着微服务架构的普及,监控和跟踪各个服务的状态变得尤为重要。Prometheus 是一个开源的监控系统和时间序列数据库,非常适合用于微服务架构中的监控。本文将详细介绍 Prometheus 如何支持微服务架构下的监控需求,包括服务发现、服务间的监控指标收集以及如何配置 Prometheus 来适应这些需求。
59 0
|
15天前
|
Prometheus 监控 Cloud Native
介绍如何使用Prometheus进行监控
介绍如何使用Prometheus进行监控
53 3
|
20天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。
|
2月前
|
Prometheus 监控 Cloud Native
自定义grafana_table(数据源Prometheus)
综上所述,自定义 Grafana 表格并将 Prometheus 作为数据源的关键是理解 PromQL 的查询机制、熟悉 Grafana 面板的配置选项,并利用 Grafana 强大的转换和自定义功能使数据展示更为直观和有洞见性。随着对这些工具更深入的了解,您将可以创建出更高级的监控仪表盘,以支持复杂的业务监控需求。
83 1
|
2月前
|
Prometheus 监控 Cloud Native
prometheus学习笔记之Grafana安装与配置
prometheus学习笔记之Grafana安装与配置
|
2月前
|
Prometheus 运维 监控
Grafana 在 DevOps 中的应用
【8月更文第29天】Grafana 是一个开源的数据可视化平台,它可以连接到多种数据源,从简单的指标到复杂的查询,都能轻松创建出漂亮的图形化仪表板。在 DevOps 领域,Grafana 被广泛应用于性能监控、故障排查、服务可用性监控等方面。本文将详细介绍 Grafana 如何支持 DevOps 团队的工作,并提供一些具体的使用案例和代码示例。
24 1
|
2月前
|
Prometheus 监控 数据可视化
Grafana 插件生态系统:扩展你的监控能力
【8月更文第29天】Grafana 是一个流行的开源平台,用于创建和共享统计数据的仪表板和可视化。除了内置的支持,Grafana 还有一个强大的插件生态系统,允许用户通过安装插件来扩展其功能。本文将介绍一些 Grafana 社区提供的插件,并探讨它们如何增强仪表盘的功能性。
94 1
|
2月前
|
存储 Prometheus 监控
Grafana 与 Prometheus 集成:打造高效监控系统
【8月更文第29天】在现代软件开发和运维领域,监控系统已成为不可或缺的一部分。Prometheus 和 Grafana 作为两个非常流行且互补的开源工具,可以协同工作来构建强大的实时监控解决方案。Prometheus 负责收集和存储时间序列数据,而 Grafana 则提供直观的数据可视化功能。本文将详细介绍如何集成这两个工具,构建一个高效、灵活的监控系统。
152 1
|
2月前
|
Prometheus 监控 Cloud Native
Grafana 入门指南:快速上手监控仪表盘
【8月更文第29天】Grafana 是一款开源的数据可视化和监控工具,它允许用户轻松地创建美观的仪表盘和图表,以便更好地理解和监控数据。无论您是需要监控系统性能指标、应用程序日志还是业务关键指标,Grafana 都能提供灵活而强大的解决方案。本指南将带领您快速上手 Grafana,包括安装、配置以及创建第一个监控面板。
129 1
|
2月前
|
Prometheus 监控 Cloud Native
Spring Boot 性能护航!Prometheus、Grafana、ELK 组合拳,点燃数字化时代应用稳定之火
【8月更文挑战第29天】在现代软件开发中,保证应用性能与稳定至关重要。Spring Boot 作为流行的 Java 框架,结合 Prometheus、Grafana 和 ELK 可显著提升监控与分析能力。Prometheus 负责收集时间序列数据,Grafana 将数据可视化,而 ELK (Elasticsearch、Logstash、Kibana)则管理并分析应用日志。通过具体实例演示了如何在 Spring Boot 应用中集成这些工具:配置 Prometheus 获取度量信息、Grafana 显示结果及 ELK 分析日志,从而帮助开发者快速定位问题,确保应用稳定高效运行。
48 1
下一篇
无影云桌面