Delay init of http phase values (#865)
authorBen Kochie <superq@gmail.com>
Thu, 17 Feb 2022 16:49:37 +0000 (17:49 +0100)
committerGitHub <noreply@github.com>
Thu, 17 Feb 2022 16:49:37 +0000 (17:49 +0100)
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 <superq@gmail.com>
prober/http.go

index 7d5adc368e1510fc058cc685b5001845e310a2a3..cdd0da8a1707c3ab184cf44fb3b11abef8f3d19b 100644 (file)
@@ -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