Simple implementation of single-resolver-call without fallback
authorJulien Pivotto <roidelapluie@inuits.eu>
Fri, 28 May 2021 21:40:32 +0000 (23:40 +0200)
committerJulien Pivotto <roidelapluie@inuits.eu>
Fri, 28 May 2021 21:42:22 +0000 (23:42 +0200)
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 <roidelapluie@inuits.eu>
prober/utils.go
prober/utils_test.go

index 98c9152ea36efa0d643467c103353e06a40b7ae0..546c798194b64650ac40f72e1c11b97acffe9fbe 100644 (file)
@@ -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)
index 95ad8d2537ca39a45c060c4d640dd691220a6f44..72e1bae7c0953bdcbb6a6a5ff7cfd56d2b0388a1 100644 (file)
@@ -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")