使用 Prometheus 指标监控 Caddy
无论您是在云端运行数千个 Caddy 实例,还是在嵌入式设备上运行单个 Caddy 服务器,您都很可能在某个时候希望对 Caddy 的运行状况以及运行时间有一个高级概览。换句话说,您将需要能够监控 Caddy。
启用指标
您需要启用指标。
如果使用 Caddyfile,请在全局选项中启用指标
{
metrics
}
如果使用 JSON,请将 "metrics": {}
添加到您的 apps > http > servers
配置中。
要添加每个主机的指标,您可以插入 per_host
选项。现在,主机特定的指标将具有 Host 标签。
{
metrics {
per_host
}
}
Prometheus
Prometheus 是一个监控平台,它通过抓取监控目标上的指标 HTTP 端点来收集指标。除了帮助您使用像 Grafana 这样的仪表板工具显示指标外,Prometheus 也用于警报。
与 Caddy 一样,Prometheus 也是用 Go 编写的,并以单个二进制文件形式分发。要安装它,请参阅 Prometheus 安装文档,或者在 MacOS 上只需运行 brew install prometheus
。
如果您是 Prometheus 的新手,请阅读 Prometheus 文档,否则请继续阅读!
要配置 Prometheus 从 Caddy 抓取指标,您需要一个类似于这样的 YAML 配置文件
# prometheus.yaml
global:
scrape_interval: 15s # default is 1 minute
scrape_configs:
- job_name: caddy
static_configs:
- targets: ['localhost:2019']
然后您可以像这样启动 Prometheus
$ prometheus --config.file=prometheus.yaml
Caddy 的指标
与任何使用 Prometheus 监控的进程一样,Caddy 公开一个 HTTP 端点,该端点以 Prometheus 导出格式响应。Caddy 的 Prometheus 客户端也被配置为在协商后以 OpenMetrics 导出格式响应(即,如果 Accept
标头设置为 application/openmetrics-text; version=0.0.1
)。
默认情况下,在管理 API(即 http://localhost:2019/metrics)上有一个 /metrics
端点可用。但是,如果管理 API 被禁用,或者您希望在不同的端口或路径上监听,则可以使用 metrics
处理程序来配置它。
您可以使用任何浏览器或 HTTP 客户端(如 curl
)查看指标
$ curl http://localhost:2019/metrics
# HELP caddy_admin_http_requests_total Counter of requests made to the Admin API's HTTP endpoints.
# TYPE caddy_admin_http_requests_total counter
caddy_admin_http_requests_total{code="200",handler="metrics",method="GET",path="/metrics"} 2
# HELP caddy_http_request_duration_seconds Histogram of round-trip request durations.
# TYPE caddy_http_request_duration_seconds histogram
caddy_http_request_duration_seconds_bucket{code="308",handler="static_response",method="GET",server="remaining_auto_https_redirects",le="0.005"} 1
caddy_http_request_duration_seconds_bucket{code="308",handler="static_response",method="GET",server="remaining_auto_https_redirects",le="0.01"} 1
caddy_http_request_duration_seconds_bucket{code="308",handler="static_response",method="GET",server="remaining_auto_https_redirects",le="0.025"} 1
...
您将看到许多指标,这些指标大致分为 4 类
- 运行时指标
- 管理 API 指标
- HTTP 中间件指标
- 反向代理指标
运行时指标
这些指标涵盖了 Caddy 进程的内部,并由 Prometheus Go 客户端自动提供。它们以 go_*
和 process_*
为前缀。
请注意,process_*
指标仅在 Linux 和 Windows 上收集。
请参阅 Go Collector、Process Collector 和 BuildInfo Collector 的文档。
管理 API 指标
这些指标有助于监控 Caddy 管理 API。每个管理端点都已进行检测,以跟踪请求计数和错误。
这些指标以 caddy_admin_*
为前缀。
例如
$ curl -s http://localhost:2019/metrics | grep ^caddy_admin
caddy_admin_http_requests_total{code="200",handler="admin",method="GET",path="/config/"} 1
caddy_admin_http_requests_total{code="200",handler="admin",method="GET",path="/debug/pprof/"} 2
caddy_admin_http_requests_total{code="200",handler="admin",method="GET",path="/debug/pprof/cmdline"} 1
caddy_admin_http_requests_total{code="200",handler="load",method="POST",path="/load"} 1
caddy_admin_http_requests_total{code="200",handler="metrics",method="GET",path="/metrics"} 3
caddy_admin_http_requests_total
管理端点处理的请求总数计数器,包括 admin.api.*
命名空间中的模块。
标签 | 描述 |
---|---|
code |
HTTP 状态码 |
handler |
处理程序或模块名称 |
method |
HTTP 方法 |
path |
管理端点挂载到的 URL 路径 |
caddy_admin_http_request_errors_total
管理端点中遇到的错误数计数器,包括 admin.api.*
命名空间中的模块。
标签 | 描述 |
---|---|
handler |
处理程序或模块名称 |
method |
HTTP 方法 |
path |
管理端点挂载到的 URL 路径 |
HTTP 中间件指标
所有 Caddy HTTP 中间件处理程序都已自动检测,用于确定请求延迟、首字节时间、错误以及请求/响应主体大小。
对于以下直方图指标,存储桶当前不可配置。对于持续时间,使用默认的存储桶集(5ms、10ms、25ms、50ms、100ms、250ms、500ms、1s、2.5s、5s 和 10s)。对于大小,存储桶为 256b、1kiB、4kiB、16kiB、64kiB、256kiB、1MiB 和 4MiB。
caddy_http_requests_in_flight
当前正在由此服务器处理的请求数的仪表。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
caddy_http_request_errors_total
处理请求时遇到的中间件错误数计数器。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
caddy_http_requests_total
发出的 HTTP(S) 请求数计数器。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
caddy_http_request_duration_seconds
往返请求持续时间的直方图。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
code |
HTTP 状态码 |
method |
HTTP 方法 |
caddy_http_request_size_bytes
请求总大小(估计值)的直方图。包括主体。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
code |
HTTP 状态码 |
method |
HTTP 方法 |
caddy_http_response_size_bytes
返回的响应主体大小的直方图。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
code |
HTTP 状态码 |
method |
HTTP 方法 |
caddy_http_response_duration_seconds
响应的首字节时间的直方图。
标签 | 描述 |
---|---|
server |
服务器名称 |
handler |
处理程序或模块名称 |
code |
HTTP 状态码 |
method |
HTTP 方法 |
反向代理指标
caddy_reverse_proxy_upstreams_healthy
反向代理上游健康状况的仪表。
值 0
表示上游不健康,而 1
表示上游健康。
标签 | 描述 |
---|---|
upstream |
上游地址 |
示例查询
一旦您让 Prometheus 抓取 Caddy 的指标,您就可以开始看到一些关于 Caddy 性能的有趣指标。
例如,要查看每秒请求速率,以 5 分钟为平均值
rate(caddy_http_requests_total{handler="file_server"}[5m])
要查看超过 100 毫秒延迟阈值的速率
sum(rate(caddy_http_request_duration_seconds_count{server="srv0"}[5m])) by (handler)
-
sum(rate(caddy_http_request_duration_seconds_bucket{le="0.100", server="srv0"}[5m])) by (handler)
要在 file_server
处理程序上查找第 95 百分位请求持续时间,您可以使用这样的查询
histogram_quantile(0.95, sum(caddy_http_request_duration_seconds_bucket{handler="file_server"}) by (le))
或者要查看 file_server
处理程序上成功 GET
请求的响应大小(以字节为单位)的中位数
histogram_quantile(0.5, caddy_http_response_size_bytes_bucket{method="GET", handler="file_server", code="200"})