Wrap DNS lookup metric time defer
authorBen Kochie <bjk@gitlab.com>
Mon, 30 Oct 2017 15:51:58 +0000 (16:51 +0100)
committerBrian Brazil <brian.brazil@robustperception.io>
Wed, 1 Nov 2017 15:22:34 +0000 (15:22 +0000)
Wrap the DNS lookup time `defer` in an anonymous function to avoid
arguments being evaluated early.

Closes: https://github.com/prometheus/blackbox_exporter/issues/253

prober/utils.go

index 42a93ba4842f73d15c0d9e1faa3ca5ab9ec788bf..524341d9f3a338ec94815b2a69cf23313f01e658 100644 (file)
@@ -43,7 +43,9 @@ func chooseProtocol(preferredIPProtocol string, target string, registry *prometh
        level.Info(logger).Log("msg", "Resolving target address", "preferred_ip_protocol", preferredIPProtocol)
        resolveStart := time.Now()
 
-       defer probeDNSLookupTimeSeconds.Add(time.Since(resolveStart).Seconds())
+       defer func() {
+               probeDNSLookupTimeSeconds.Add(time.Since(resolveStart).Seconds())
+       }()
 
        ip, err := net.ResolveIPAddr(preferredIPProtocol, target)
        if err != nil {