Javaer

Prometheus+Grafana可视化监控SpringBoo

2019-02-13  本文已影响357人  AnLingYi

原文地址:https://xeblog.cn/articles/7

Prometheus简介

简史

Prometheus受启发于Google的Brogmon监控系统(相似的Kubernetes是从Google的Brog系统演变而来),从2012年开始由前Google工程师在Soundcloud以开源软件的形式进行研发,并且于2015年早期对外发布早期版本。2016年5月继Kubernetes之后成为第二个正式加入CNCF基金会的项目,同年6月正式发布1.0版本。2017年底发布了基于全新存储层的2.0版本,能更好地与容器平台、云平台配合。

架构

image

特点

相关概念

数据模型

Prometheus 存储的是时序数据, 即按照相同时序(相同的名字和标签),以时间维度存储连续的数据的集合。

监控样本

# HELP system_cpu_usage The "recent cpu usage" for the whole system
# TYPE system_cpu_usage gauge
system_cpu_usage 0.23587264544090683
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="error",} 0.0
logback_events_total{level=“info”,} 557.0

时序类型

Prometheus 时序数据分为 Counter, Gauge, Histogram, Summary 四种类型。

Prometheus的使用

安装

官方下载地址

配置

Pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

项目开启监控

management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

添加Job

进入Prometheus安装根目录 vim prometheus.yml

新增节点

- job_name: xeblog-api
    metrics_path: /actuator/prometheus
    static_configs:
    - targets: ['127.0.0.1:8080’]

job_name:任务名称
metrics_path: 指标路径
targets:实例地址/项目地址,可配置多个

运行Prometheus

进入Prometheus安装根目录 ./prometheus
运行成功日志


image

访问地址:localhost:9090

PromQL

匹配过滤

操作符:= != =~ !~

匹配监控任务为xeblog-api的非GET请求方法的请求数

http_server_requests_seconds_count{job="xeblog-api", method!="GET"}

匹配监控任务为xeblog-api的非GET请求方法的请求数,且请求路径不为/api/message和/api/version

http_server_requests_seconds_count{job="xeblog-api", method!="GET", uri !~ "/api/message|/api/version"}

它门的区别主要是,“=~ !~”支持正则匹配

范围查询

查询最近5分钟内的所有样本数据:
http_request_total{}[5m]
可选单位:

时间位移操作(offset)

查询5分钟前的样本数据:
http_request_total{} offset 5m

查询昨天1天内的样本数据:
http_request_total{}[1d] offset 1d

聚合操作

内置函数

Grafana可视化

安装

官方下载地址

Mac下安装启动示例

// 安装
brew install grafana
// 启动
brew services start grafana

启动后访问地址:localhost:3000

登陆:
初始用户名和密码都是admin

添加Prometheus数据源

image

新增Dashboard

可以选择自己手动添加或者导入一个已配置好的Json文件
Dashboard分享社区:https://grafana.com/dashboards
这里可以下载别人分享的Dashboard Json配置文件

image
推荐下载:Spring Boot Statistics
下载完后 导入文件就可以看见类似一个这样的监控界面
image
最上面这一行是模板变量,可以动态选择
image
点击设置按钮,选择Variables,点击New可以新增变量
image
点击title选择Edit可以进行编辑
image
这里编写PromQL语句
image

监控报警

配置发件邮箱

vim grafana.ini
我的文件路径是/usr/local/etc/grafana/grafana.ini
配置如下:

[smtp]
enabled = true
host = smtp.qq.com:25
user = 你的QQ@qq.com
# If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;"""
password = 邮箱口令(不是QQ密码)
;cert_file =
;key_file =
;skip_verify = false
from_address = 你的QQ@qq.com
from_name = Grafana
# EHLO identity in SMTP dialog (defaults to instance_name)
;ehlo_identity = dashboard.example.com

这里配置的是QQ邮箱,其他邮箱同理

配置完后,重启Grafana

brew services restart grafana

配置收件人

image

可以添加多个收件人,以“;”分隔


image

最后点击Send Test 可以收到一封测试邮件,配置成功


image

监控指标

需要注意的是,Prometheus不支持带有模版变量的监控设置报警,否则会提示“Template variables are not supported in alert queries”

监控示例:
监控CPU使用率
PromQL:

system_cpu_usage{instance="实例地址", job="任务名称"}
image

创建报警


image

这里配置的是当CPU使用率超过1%则发出报警


image
添加报警通知邮箱以及报警的描述信息
image
保存之后,没过多久就收到了报警通知
image

感谢阅读,如有错误,望指正!

参考资料

上一篇 下一篇

猜你喜欢

热点阅读