什么是全链路监控
链路追踪监控系统:
- Prometheus »
- Apache SkyWalking »
- 阿里 ARMS »
- 美团 Cat »
- Zabbix »
- OpenZipkin »
- Zipkin + Sleuth
- Pinpoint
- Jaeger
- OpenTelemetry
- OpenFalcon
- Nagios
- EventBridge
- Hubble
- ※ Grafana 可视化 »
Prometheus
- 教程
Prometheus Relabel机制(relabel_config) »
https://blog.csdn.net/weixin_46902396/article/details/118637402
安装
- 二进制方式安装
创建启动脚本
echo 'nohup ./prometheus --web.enable-lifecycle --config.file="/home/prometheus-env/prometheus/prometheus.yml" &' >> start.sh && chmod +x start.sh
- docker方式安装
docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus
浏览器验证
http://172.32.1.91:9090/
PromQL
理解:SQL语句
http_request_total{} # 瞬时向量表达式,选择当前最新的数据
http_request_total{}[5m] # 区间向量表达式,选择以当前时间为基准,5分钟内的数据
http_requests_total offset 5m # 5分钟前的数据
sum(http_requests_total{method="GET"} offset 5m) // 聚合
HTTP API接口调用
- 在 HTTP API 中使用 PromQL
请求:
http://172.32.1.181:9090/api/v1/query?query=node_cpu_seconds_total{cpu="0",mode="idle"}
GET
返回结果:
{
"status": "success",
"data": {
"resultType": "vector",
"result": [
{
"metric": {
"__name__": "node_cpu_seconds_total", // 指标
"cpu": "0",
"instance": "172.32.1.181:9100",
"job": "localnode", // job_name
"mode": "idle"
},
"value": [
1653536219.426, // 时间戳
"5306557.85"
]
}
]
}
}
- 应用管理 API
准备就绪状态:
GET /-/ready
例:
http://172.32.1.181:9093/-/ready
重新加载:
PUT /-/reload
POST /-/reload
其他
http://172.32.1.181:9090/api/v1/rules
http://172.32.1.181:9115/metrics
http://172.32.1.181:9115/probe?module=http_get_2xx&target=baidu.com
http://172.32.1.181:9115/probe?module=http_post_2xx&target=http://172.32.1.181:8282/getToken999
配置文件 prometheus.yml
- job_name: 'yx_blackbox'
metrics_path: /probe
params:
module: [http_post_2xx]
static_configs:
- targets:
- http://prometheus.io # Target to probe with http.
- https://prometheus.io # Target to probe with https.
- http://example.com:8080 # Target to probe with http on port 8080.
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 172.32.1.181:9115
检查prometheus配置是否正常
./promtool check config prometheus.yml
Prometheus服务发现
目前支持的服务发现类型主要有如下几种:
1、基于文件的服务发现
2、基于consul的服务发现
3、基于k8s API的服务发现
4、基于eureka的服务发现
5、基于nacos的服务发现
6、基于DNS的服务发现
资源监控
Prometheus 都可以采集那些指标?-- 常用 Exporter 合集_内容合集_耳东@Erdong_InfoQ写作社区
CPU使用率
通常所说的CPU使用率,就是除了空闲时间外的其他时间占总CPU时间的百分比。
CPU使用率有很多重要指标,具体含义如下:
idle(通常缩写为id),代表空闲时间。注意,它不包括I/O等待时间(iowait)
iowait(通常缩写为wa),代表等待I/O的CPU时间
user(通常缩写为us),代表用户态CPU时间。注意,它包括下面的nice时间,但包括了guest时间。
nice(通常缩写为ni),代表低优先级用户态CPU时间,也就是进程的nice值被调整为1-19之间是的CPU时间。
IPLC是纯内网的专线,稳定性好,延时低,对网络品质有要求的核心产品,可以通过使用IPLC专线服务来提升网络体验和服务。
system(通常缩写为sys),代表内核态CPU时间
irq(通常缩写为hi),代表处理硬中断的CPU时间
softirq(通常缩写为si),代表处理软中断的CPU时间
steal(通常缩写为st),代表当系统运行在虚拟机中的时候,被其他虚拟机占用的CPU时间
guest(通常缩写为guest),代表通过虚拟化运行其他操作系统的时间,也就是运行虚拟机的CPU时间
实战
Prometheus 查询语言 PromQL 的 CPU 使用率计算方法
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
http://172.32.1.181:9090/api/v1/query?query=100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
Nacos
MySQL
https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
Redis
https://github.com/oliver006/redis_exporter/releases
Flink
https://github.com/apache/flink/tree/master/flink-metrics/flink-metrics-prometheus
ClickHouse
https://github.com/ClickHouse/clickhouse_exporter
HBase
Prometheus监控实战篇Nginx、Hbase操作详解 »
告警
告警能力在Prometheus的架构中被划分为两个部分,在Prometheus Server中定义告警规则以及产生告警,Alertmanager组件统一处理这些由Prometheus产生的告警。
Alertmanager提供了多种内置第三方告警通知方式,同时还提供了对Webhook通知的支持,通过Webhook用户可以完成对告警更多个性化的扩展。
1、修改 prometheus.yml
文件
rule_files:
- /home/prometheus-env/prometheus/rules/*.rules
2、创建配置中对应的文件夹 rules/
3、在所创建的文件夹中创建以 .rules
结尾的规则文件
vi hoststats-alert.rules
groups:
- name: hostStatsAlert
rules:
- alert: hostCpuUsageAlert
expr: sum(avg without (cpu)(irate(node_cpu_seconds_total{mode!='idle'}[5m]))) by (instance) > 0.85
for: 1m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} CPU usgae high"
description: "{{ $labels.instance }} CPU usage above 85% (current value: {{ $value }})"
4、重新加载Prometheus
curl -X POST http://172.32.1.181:9090/-/reload
5、浏览器验证
http://172.32.1.181:9090/rules
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oVxQ0u57-1654048722120)(vx_images/280623417239695.png)]
6、安装并配置alertmanager.yml
邮件通知示例:
global:
smtp_smarthost: smtp.gmail.com:587
smtp_from: <smtp mail from>
smtp_auth_username: <usernae>
smtp_auth_identity: <username>
smtp_auth_password: <password>
route:
group_by: ['alertname']
receiver: 'default-receiver'
receivers:
- name: default-receiver
email_configs:
- to: <mail to address>
send_resolved: true
7、配置 prometheus.yml
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- 172.32.1.181:9093
8、测试
cat /dev/zero>/dev/null
http://172.32.1.181:9090/alerts
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2SLHxiht-1654048722121)(vx_images/19673017247026.png)]
http://172.32.1.181:9093/#/alerts
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yhl9SKig-1654048722122)(vx_images/175072817226860.png)]
使用Java自定义Exporter
SkyWalking
https://baijiahao.baidu.com/s?id=1721183541175773764&wfr=spider&for=pc
APM
- 启动
启动脚本在 bin/
目录下,分客户端和服务端:
只启动oap服务:oapService.sh
只启动ui服务:webappService.sh
同时启动OAP和UI :startup.sh
- 设置
UI的设置文件是发行包里的webapp/webapp.yml
server:
port: 8080
浏览器访问
http://172.32.1.91:8080/
Java Agent
- 命令行启动
java -javaagent:/home/srt/sky-walking/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name=yx-gateway -Dskywalking.collector.backend_service=172.32.1.91:11800 -jar gateway-0.0.1-SNAPSHOT.jar
java -javaagent:/home/srt/sky-walking/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name=yx-api -Dskywalking.collector.backend_service=172.32.1.91:11800 -jar api-0.0.1-SNAPSHOT.jar
- IDEA配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dhZNIiwV-1654048722122)(vx_images/381611914226859.png)]
自定义链路追踪
会有代码入侵
阿里云ARMS
ARMS是修改了开源项目pinpoint
整合而来,更是加上了arthas
,不需要使用-javaagent在项目启动的时候进行导入。有一个致命的缺点的,就是太贵了。
[Prometheus告警规则 (aliyun.com)](