我们将逐步解析一个处理 gRPC 探测的 Go 语言函数 ProbeGRPC
。该函数主要用于探测和监控 gRPC 服务的可用性和性能,并将相关指标推送到 Prometheus 进行实时监控。下面,我们将按照代码块逐步解释函数的实现和作用。
1. 函数签名与变量声明
func ProbeGRPC(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger *slog.Logger) (success bool) {
var (
durationGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_grpc_duration_seconds",
Help: "Duration of gRPC request by phase",
}, []string{
"phase"})
isSSLGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_grpc_ssl",
Help: "Indicates if SSL was used for the connection",
})
statusCodeGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_grpc_status_code",
Help: "Response gRPC status code",
})
healthCheckResponseGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_grpc_healthcheck_response",
Help: "Response HealthCheck response",
}, []string{
"serving_status"})
probeSSLEarliestCertExpiryGauge = prometheus.NewGauge(sslEarliestCertExpiryGaugeOpts)
probeTLSVersion = prometheus.NewGaugeVec(
probeTLSInfoGaugeOpts,
[]string{
"version"},
)
probeSSLLastInformation = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "probe_ssl_last_chain_info",
Help: "Contains SSL leaf certificate information",
},
[]string{
"fingerprint_sha256"