ProbeHTTP 函数用于通过 HTTP 协议对指定的目标地址进行监控和探测。通过使用 Prometheus 指标进行性能度量,函数支持捕获请求的各类数据,如响应时间、状态码、重定向次数、SSL/TLS 信息等。
本文将逐步解析这段代码,帮助您理解它的各个部分以及它是如何工作的。
1. 函数签名与参数说明
func ProbeHTTP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger *slog.Logger) (success bool)
ctx: 上下文对象,通常用于控制请求的取消、超时等。target: 需要探测的目标地址(URL)。module: 配置模块,包含了 HTTP 请求相关的设置。registry: Prometheus 注册表,用于注册并记录监控指标。logger: 日志记录器,用于输出日志信息。success: 函数执行结果标志,表示是否成功探测。
2. 定义 Prometheus 指标
var redirects int
var (
durationGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_http_duration_seconds",
Help: "Duration of http request by phase, summed over all redirects",
}, []string{
"phase"})
contentLengthGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_http_content_length",
Help: "Length of http content response",
})
// 其他指标定义
)
在函数开头,定义了多个 Prometheus 指标,记录 HTTP 请求的各项数据:
durationGaugeVec: 记录请求各阶段的持续时间(例如解析、连接、TLS 握手等)。contentLengthGauge: 记录响应体的大小。redirectsGauge: 记录重定向次数。statusCodeGauge: 记录 HTTP 响应状态码。- 其他一些用于记录 SSL

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



