From 1b883bf96297b4a621a1e14e1d92e115dd7ec17e Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Thu, 17 Feb 2022 17:49:37 +0100 Subject: [PATCH] Delay init of http phase values (#865) Don't init http probe phase labels until we're ready to send the request. Avoids exposing 0 value samples for things we haven't started to measure. Fix http resolve timing to report lookup time even if it's an error (ie not found). Fixes: https://github.com/prometheus/blackbox_exporter/issues/579 Signed-off-by: SuperQ --- prober/http.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/prober/http.go b/prober/http.go index 7d5adc3..cdd0da8 100644 --- a/prober/http.go +++ b/prober/http.go @@ -307,10 +307,6 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr }) ) - for _, lv := range []string{"resolve", "connect", "tls", "processing", "transfer"} { - durationGaugeVec.WithLabelValues(lv) - } - registry.MustRegister(durationGaugeVec) registry.MustRegister(contentLengthGauge) registry.MustRegister(bodyUncompressedLengthGauge) @@ -336,11 +332,11 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr targetPort := targetURL.Port() ip, lookupTime, err := chooseProtocol(ctx, module.HTTP.IPProtocol, module.HTTP.IPProtocolFallback, targetHost, registry, logger) + durationGaugeVec.WithLabelValues("resolve").Add(lookupTime) if err != nil { level.Error(logger).Log("msg", "Error resolving address", "err", err) return false } - durationGaugeVec.WithLabelValues("resolve").Add(lookupTime) httpClientConfig := module.HTTP.HTTPClientConfig if len(httpClientConfig.TLSConfig.ServerName) == 0 { @@ -450,6 +446,10 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr } request = request.WithContext(httptrace.WithClientTrace(request.Context(), trace)) + for _, lv := range []string{"connect", "tls", "processing", "transfer"} { + durationGaugeVec.WithLabelValues(lv) + } + resp, err := client.Do(request) // This is different from the usual err != nil you'd expect here because err won't be nil if redirects were // turned off. See https://github.com/golang/go/issues/3795 -- 2.25.1