From 0e044cf0eacb4526bddbb73f550810b551b7e5d9 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Fri, 30 Sep 2022 12:12:24 +0200 Subject: [PATCH] Refactor prober gauges (#972) Move prober metric GaugeOpts into prober.go to reduce copy-n-paste. Signed-off-by: SuperQ superq@gmail.com --- prober/grpc.go | 11 +++-------- prober/http.go | 15 +++------------ prober/prober.go | 17 +++++++++++++++++ prober/tcp.go | 15 +++------------ 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/prober/grpc.go b/prober/grpc.go index 45e2d72..e7a5921 100644 --- a/prober/grpc.go +++ b/prober/grpc.go @@ -97,15 +97,10 @@ func ProbeGRPC(ctx context.Context, target string, module config.Module, registr Help: "Response HealthCheck response", }, []string{"serving_status"}) - probeSSLEarliestCertExpiryGauge = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "probe_ssl_earliest_cert_expiry", - Help: helpSSLEarliestCertExpiry, - }) + probeSSLEarliestCertExpiryGauge = prometheus.NewGauge(sslEarliestCertExpiryGaugeOpts) - probeTLSVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "probe_tls_version_info", - Help: helpProbeTLSInfo, - }, + probeTLSVersion = prometheus.NewGaugeVec( + probeTLSInfoGaugeOpts, []string{"version"}, ) diff --git a/prober/http.go b/prober/http.go index 17d75d7..af12c7e 100644 --- a/prober/http.go +++ b/prober/http.go @@ -264,15 +264,9 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr Help: "Response HTTP status code", }) - probeSSLEarliestCertExpiryGauge = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "probe_ssl_earliest_cert_expiry", - Help: helpSSLEarliestCertExpiry, - }) + probeSSLEarliestCertExpiryGauge = prometheus.NewGauge(sslEarliestCertExpiryGaugeOpts) - probeSSLLastChainExpiryTimestampSeconds = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "probe_ssl_last_chain_expiry_timestamp_seconds", - Help: helpSSLChainExpiryInTimeStamp, - }) + probeSSLLastChainExpiryTimestampSeconds = prometheus.NewGauge(sslChainExpiryInTimeStampGaugeOpts) probeSSLLastInformation = prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -283,10 +277,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr ) probeTLSVersion = prometheus.NewGaugeVec( - prometheus.GaugeOpts{ - Name: "probe_tls_version_info", - Help: helpProbeTLSInfo, - }, + probeTLSInfoGaugeOpts, []string{"version"}, ) diff --git a/prober/prober.go b/prober/prober.go index ec67e56..850ee7c 100644 --- a/prober/prober.go +++ b/prober/prober.go @@ -29,3 +29,20 @@ const ( helpSSLChainExpiryInTimeStamp = "Returns last SSL chain expiry in timestamp" helpProbeTLSInfo = "Returns the TLS version used or NaN when unknown" ) + +var ( + sslEarliestCertExpiryGaugeOpts = prometheus.GaugeOpts{ + Name: "probe_ssl_earliest_cert_expiry", + Help: helpSSLEarliestCertExpiry, + } + + sslChainExpiryInTimeStampGaugeOpts = prometheus.GaugeOpts{ + Name: "probe_ssl_last_chain_expiry_timestamp_seconds", + Help: helpSSLChainExpiryInTimeStamp, + } + + probeTLSInfoGaugeOpts = prometheus.GaugeOpts{ + Name: "probe_tls_version_info", + Help: helpProbeTLSInfo, + } +) diff --git a/prober/tcp.go b/prober/tcp.go index f70e583..dab140c 100644 --- a/prober/tcp.go +++ b/prober/tcp.go @@ -89,14 +89,8 @@ func dialTCP(ctx context.Context, target string, module config.Module, registry } func ProbeTCP(ctx context.Context, target string, module config.Module, registry *prometheus.Registry, logger log.Logger) bool { - probeSSLEarliestCertExpiry := prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "probe_ssl_earliest_cert_expiry", - Help: helpSSLEarliestCertExpiry, - }) - probeSSLLastChainExpiryTimestampSeconds := prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "probe_ssl_last_chain_expiry_timestamp_seconds", - Help: helpSSLChainExpiryInTimeStamp, - }) + probeSSLEarliestCertExpiry := prometheus.NewGauge(sslEarliestCertExpiryGaugeOpts) + probeSSLLastChainExpiryTimestampSeconds := prometheus.NewGauge(sslChainExpiryInTimeStampGaugeOpts) probeSSLLastInformation := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "probe_ssl_last_chain_info", @@ -105,10 +99,7 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry []string{"fingerprint_sha256", "subject", "issuer", "subjectalternative"}, ) probeTLSVersion := prometheus.NewGaugeVec( - prometheus.GaugeOpts{ - Name: "probe_tls_version_info", - Help: helpProbeTLSInfo, - }, + probeTLSInfoGaugeOpts, []string{"version"}, ) probeFailedDueToRegex := prometheus.NewGauge(prometheus.GaugeOpts{ -- 2.25.1