演示服务
为了尽可能详细地给大家演示 PromQL 指标查询,这里我们将 Fork 一个开源的 Prometheus 演示服务来进行查询,这样可以让我们更加灵活地对指标数据进行控制,项目仓库地址:https://github.com/cnych/prometheus_demo_service,这是一个 Go 语言开发的服务,我们可以自己构建应用。
首先准备 golang 环境:
? ? wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
? ? rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.3.linux-amd64.tar.gz
# 配置环境变量,可以将下面命令添加到 /etc/profile 中
? ? export PATH=$PATH:/usr/local/go/bin
# 执行go命令验证
? ? go version
go version go1.16.3 linux/amd64
然后 clone 代码构建:开始准备构建项目
# 首先clone代码(建议使用ssh模式,你懂的~)
? ? git clone https://github.com/cnych/prometheus_demo_service
? ? cd prometheus_demo_service
# 配置 GOPROXY 代理
? ? export GOPROXY=https://goproxy.cn
# 构建
? ? env GOOS=linux GOARCH=amd64 go build -o prometheus_demo_service //进行编译[root@master prometheus_demo_service]# ls
api.go client.go crossbuild.sh Dockerfile go.sum intermittent_metric.go memory.go
batch.go cpu.go disk.go go.mod holiday.go main.go prometheus_demo_service这是编译打包出来的二进制程序,可以直接启动监听的端口是8080
[root@master prometheus_demo_service]# ./prometheus_demo_service [root@master ~]# netstat -tpln | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 20916/./prometheus_
# HELP demo_api_http_requests_in_progress The current number of API HTTP requests in progress.
# TYPE demo_api_http_requests_in_progress gauge
demo_api_http_requests_in_progress 1# HELP demo_api_request_duration_seconds A histogram of the API HTTP request durations in seconds.
# TYPE demo_api_request_duration_seconds histogram
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0001"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.00015000000000000001"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.00022500000000000002"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0003375"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.00050625"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.000759375"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0011390624999999999"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0017085937499999998"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0025628906249999996"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0038443359374999994"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.00576650390625"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.008649755859375"} 0
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.0129746337890625"} 22
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.01946195068359375"} 600
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.029192926025390625"} 612
demo_api_request_duration_seconds_bucket{method="GET",path="/api/bar",status="200",le="0.043789389038085935"} 660
构建完成后启动 3 个服务,分别监听 10000、10001、10002 端口:
[root@master ~]# netstat -tpln | grep 1000
tcp6 0 0 :::10000 :::* LISTEN 26720/./prometheus_
tcp6 0 0 :::10001 :::* LISTEN 27270/./prometheus_
tcp6 0 0 :::10002 :::* LISTEN 27326/./prometheus_ [root@master prometheus_demo_service]# nohup ./prometheus_demo_service --listen-address=:10000 &
[1] 26720
[root@master prometheus_demo_service]# nohup: ignoring input and appending output to ?.ohup.out?[root@master prometheus_demo_service]# nohup ./prometheus_demo_service --listen-address=:10001 &
[2] 27270
[root@master prometheus_demo_service]# nohup: ignoring input and appending output to ?.ohup.out?[root@master prometheus_demo_service]#
[root@master prometheus_demo_service]# nohup ./prometheus_demo_service --listen-address=:10002 &
[3] 27326
上面 3 个服务都在 /metrics
端点暴露了一些指标数据,我们可以把这 3 个服务配置到 Prometheus 抓取任务中,这样后续就可以使用这几个服务来进行 PromQL 查询说明了。
完整的 prometheus.yml
配置文件如下所示:
global:scrape_interval: 5s # 抓取频率scrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]# 配置demo抓取任务- job_name: demoscrape_interval: 15s //会覆盖global的全局配置scrape_timeout: 10sstatic_configs:- targets:- demo-service-0:10000- demo-service-1:10001- demo-service-2:10002