Prometheus 是一个开源的监控和告警系统,主要用于收集、存储和查询时间序列数据,并提供强大的查询语言和告警机制,帮助用户监控系统和应用的性能、健康状况及行为。
搭建HTTP Server
编写一个的 Go HTTP Server,提供 hello world API,使用 prometheus sdk 完成对该接口基本指标的监控(调用次数、调用延迟),并通过 /metrics 接口暴露监控指标
package main
import (
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// 定义监控指标
var (
helloCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "hello_requests_total",
Help: "Total number of /hello requests.",
})
helloDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "hello_request_duration_seconds",
Help: "Histogram of response latency for /hello requests.",
Buckets: prometheus.DefBuckets,
})
)
func init() {
// 注册自定义指标到 Prometheus 默认注册表
prometheus.MustRegister(helloCounter)
prometheus.MustRegister(helloDuration)
}
// /hello 接口的处理函数
func helloHandler(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
// 记录一次调用
helloCounter.Inc()
w.Write([]byte("hello world"))
duration := time.Since(startTime).Seconds()
helloDuration.Observe(duration)
}
func main() {
// 注册 /hello 接口
http.HandleFunc("/hello", helloHandler)
// 注册 /metrics 接口,将 /metrics 接口绑定到 Prometheus 提供的 promhttp.Handler()
http.Handle("/metrics", promhttp.Handler())
log.Println("Starting server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}
http://localhost:8080/metrics:查看暴露的所有 Prometheus 监控指标
http://localhost:8080/hello:访问HTTP服务器,返回hello world
本地部署Prometheus
本地部署 Prometheus,编写基本配置来采集上述 HTTP Server 的 metrics,并通过 PromQL 查询数据
Ubuntu部署Prometheus:
-
下载软件包 https://prometheus.io/download/
-
解压安装
sudo tar -xvf prometheus-2.41.0.linux-amd64.tar.gz -C /opt sudo mv /opt/prometheus-2.41.0.linux-amd64 /opt/prometheus # 重命名
-
配置Prometheus
进入 /opt/prometheus目录,编写配置文件
sudo vim my_prometheus.yml
global: scrape_interval: 15s # 全局抓取间隔 scrape_configs: - job_name: 'http-server' static_configs: - targets: ['localhost:8080'] # HTTP Server 地址及端口,Prometheus 会自动请求 /metrics
-
启动prometheus
./prometheus --config.file=my_prometheus.yml
访问http://localhost:9090,Prometheus的默认端口
配置Prometheus随系统启动,使用systemd(略)
-
采集指标
http://localhost:9090中点击Status->Targets
-
使用PromQL查询数据
PromQL(Prometheus Query Language)是 Prometheus 提供的一种查询语言,用于从时序数据库中检索和聚合监控数据
在“Graph” 页面中输入:
hello_requests_total rate(hello_requests_total[1m]) sum(rate(hello_request_duration_seconds_sum[1m])) / sum(rate(hello_request_duration_seconds_count[1m])) # 平均响应时间 = 延时总和 / 总请求数
本地部署Grafana
本地部署 Grafana,将 Prometheus 作为数据源,创建一个简单的 Dashboard,来可视化监控数据
Grafana 是一个开源的数据可视化和监控平台,它能够连接多种数据源( Prometheus、Graphite、InfluxDB、Elasticsearch 等),实时展示监控数据和日志信息
主要功能:
数据可视化:各种数据可通过折线图、柱状图、饼图、热力图等多种图表形式展示出来
多数据源支持:支持Prometheus、InfluxDB、Graphite、MySQL、PostgreSQL 以及 Elasticsearch 等
实时监控与告警
-
安装Grafana
sudo apt-get install -y adduser libfontconfig1 musl wget https://dl.grafana.com/oss/release/grafana_11.5.1_amd64.deb sudo dpkg -i grafana_11.5.1_amd64.deb
-
设置开机启动
sudo systemctl start grafana-server sudo systemctl enable grafana-server
打开 http://localhost:3000 登录 Grafana(默认账户为 admin/admin)
-
配置 Prometheus 数据源
sudo ./prometheus --config.file=my_prometheus.yml # 启动Prometheus
添加数据源
Data Sources -> Add data source -> Prometheus
填上Prometheus的地址
然后保存,确保 Grafana 能成功连接到 Prometheus 数据源
-
创建Dashboard 并添加 Panel
新建 Dashboard,新建 Panel
自定义PromQL
# 计算QPS(平均每秒请求数)
rate(hello_requests_total[1m])
# 计算P99
histogram_quantile(0.99, sum(rate(hello_request_duration_seconds_bucket[1m])) by (le))