From e3b594c94c6660a1965f087d454e62aa0353c040 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 28 May 2021 23:40:32 +0200 Subject: [PATCH] Simple implementation of single-resolver-call without fallback This approach ensures that we only call a single time the resolver in the dns probes. Otherwise, if we got an error in the first call, we would do a second call. Signed-off-by: Julien Pivotto --- prober/utils.go | 19 +++++++++++++++++++ prober/utils_test.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/prober/utils.go b/prober/utils.go index 98c9152..546c798 100644 --- a/prober/utils.go +++ b/prober/utils.go @@ -26,6 +26,11 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +var protocolToGauge = map[string]float64{ + "ip4": 4, + "ip6": 6, +} + // Returns the IP for the IPProtocol and lookup time. func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol bool, target string, registry *prometheus.Registry, logger log.Logger) (ip *net.IPAddr, lookupTime float64, err error) { var fallbackProtocol string @@ -64,6 +69,20 @@ func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol b }() resolver := &net.Resolver{} + if !fallbackIPProtocol { + ips, err := resolver.LookupIP(ctx, IPProtocol, target) + if err == nil { + for _, ip := range ips { + level.Info(logger).Log("msg", "Resolved target address", "ip", ip.String()) + probeIPProtocolGauge.Set(protocolToGauge[IPProtocol]) + probeIPAddrHash.Set(ipHash(ip)) + return &net.IPAddr{IP: ip}, lookupTime, nil + } + } + level.Error(logger).Log("msg", "Resolution with IP protocol failed", "err", err) + return nil, 0.0, err + } + ips, err := resolver.LookupIPAddr(ctx, target) if err != nil { level.Error(logger).Log("msg", "Resolution with IP protocol failed", "err", err) diff --git a/prober/utils_test.go b/prober/utils_test.go index 95ad8d2..72e1bae 100644 --- a/prober/utils_test.go +++ b/prober/utils_test.go @@ -162,7 +162,7 @@ func TestChooseProtocol(t *testing.T) { registry = prometheus.NewPedanticRegistry() ip, _, err = chooseProtocol(ctx, "ip4", false, "ipv6.google.com", registry, logger) - if err != nil && err.Error() != "unable to find ip; no fallback" { + if err != nil && err.Error() != "address ipv6.google.com: no suitable address found" { t.Error(err) } else if err == nil { t.Error("should set error") -- 2.25.1