From f763e92bcbc929991750bb116e231bdfc0fe97d2 Mon Sep 17 00:00:00 2001 From: Thor <8681572+thorfour@users.noreply.github.com> Date: Wed, 24 Apr 2019 08:23:22 -0500 Subject: [PATCH] Use timeout for chooseProtocol (#458) * travisci skip ipv6 dns tests Signed-off-by: Thor --- prober/dns.go | 2 +- prober/dns_test.go | 13 ++++++++++ prober/http.go | 2 +- prober/icmp.go | 2 +- prober/tcp.go | 2 +- prober/tcp_test.go | 4 +++ prober/utils.go | 65 ++++++++++++++++++++++++++++------------------ 7 files changed, 61 insertions(+), 29 deletions(-) diff --git a/prober/dns.go b/prober/dns.go index 471331c..00e6467 100644 --- a/prober/dns.go +++ b/prober/dns.go @@ -129,7 +129,7 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry port = "53" targetAddr = target } - ip, _, err = chooseProtocol(module.DNS.IPProtocol, module.DNS.IPProtocolFallback, targetAddr, registry, logger) + ip, _, err = chooseProtocol(ctx, module.DNS.IPProtocol, module.DNS.IPProtocolFallback, targetAddr, registry, logger) if err != nil { level.Error(logger).Log("msg", "Error resolving address", "err", err) return false diff --git a/prober/dns_test.go b/prober/dns_test.go index 525d44e..a463483 100644 --- a/prober/dns_test.go +++ b/prober/dns_test.go @@ -16,6 +16,7 @@ package prober import ( "context" "net" + "os" "runtime" "testing" "time" @@ -85,6 +86,10 @@ func recursiveDNSHandler(w dns.ResponseWriter, r *dns.Msg) { } func TestRecursiveDNSResponse(t *testing.T) { + if os.Getenv("TRAVIS") == "true" { + t.Skip("skipping; travisci is failing on ipv6 dns requests") + } + tests := []struct { Probe config.DNSProbe ShouldSucceed bool @@ -211,6 +216,10 @@ func authoritativeDNSHandler(w dns.ResponseWriter, r *dns.Msg) { } func TestAuthoritativeDNSResponse(t *testing.T) { + if os.Getenv("TRAVIS") == "true" { + t.Skip("skipping; travisci is failing on ipv6 dns requests") + } + tests := []struct { Probe config.DNSProbe ShouldSucceed bool @@ -315,6 +324,10 @@ func TestAuthoritativeDNSResponse(t *testing.T) { } func TestServfailDNSResponse(t *testing.T) { + if os.Getenv("TRAVIS") == "true" { + t.Skip("skipping; travisci is failing on ipv6 dns requests") + } + tests := []struct { Probe config.DNSProbe ShouldSucceed bool diff --git a/prober/http.go b/prober/http.go index 1c5ed23..3d649b5 100644 --- a/prober/http.go +++ b/prober/http.go @@ -271,7 +271,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr targetHost = targetURL.Host } - ip, lookupTime, err := chooseProtocol(module.HTTP.IPProtocol, module.HTTP.IPProtocolFallback, targetHost, registry, logger) + ip, lookupTime, err := chooseProtocol(ctx, module.HTTP.IPProtocol, module.HTTP.IPProtocolFallback, targetHost, registry, logger) if err != nil { level.Error(logger).Log("msg", "Error resolving address", "err", err) return false diff --git a/prober/icmp.go b/prober/icmp.go index 0f1c819..83c8736 100644 --- a/prober/icmp.go +++ b/prober/icmp.go @@ -79,7 +79,7 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr registry.MustRegister(durationGaugeVec) - ip, lookupTime, err := chooseProtocol(module.ICMP.IPProtocol, module.ICMP.IPProtocolFallback, target, registry, logger) + ip, lookupTime, err := chooseProtocol(ctx, module.ICMP.IPProtocol, module.ICMP.IPProtocolFallback, target, registry, logger) if err != nil { level.Warn(logger).Log("msg", "Error resolving address", "err", err) return false diff --git a/prober/tcp.go b/prober/tcp.go index f0ad879..e497625 100644 --- a/prober/tcp.go +++ b/prober/tcp.go @@ -38,7 +38,7 @@ func dialTCP(ctx context.Context, target string, module config.Module, registry return nil, err } - ip, _, err := chooseProtocol(module.TCP.IPProtocol, module.TCP.IPProtocolFallback, targetAddress, registry, logger) + ip, _, err := chooseProtocol(ctx, module.TCP.IPProtocol, module.TCP.IPProtocolFallback, targetAddress, registry, logger) if err != nil { level.Error(logger).Log("msg", "Error resolving address", "err", err) return nil, err diff --git a/prober/tcp_test.go b/prober/tcp_test.go index 480d1f5..9bcc714 100644 --- a/prober/tcp_test.go +++ b/prober/tcp_test.go @@ -68,6 +68,10 @@ func TestTCPConnectionFails(t *testing.T) { } func TestTCPConnectionWithTLS(t *testing.T) { + if os.Getenv("TRAVIS") == "true" { + t.Skip("skipping; travisci is failing on ipv6 dns requests") + } + ln, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Error listening on socket: %s", err) diff --git a/prober/utils.go b/prober/utils.go index 848f5ee..238f65c 100644 --- a/prober/utils.go +++ b/prober/utils.go @@ -14,6 +14,8 @@ package prober import ( + "context" + "fmt" "net" "time" @@ -24,7 +26,7 @@ import ( ) // Returns the IP for the IPProtocol and lookup time. -func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, registry *prometheus.Registry, logger log.Logger) (ip *net.IPAddr, lookupTime float64, err error) { +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 probeDNSLookupTimeSeconds := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "probe_dns_lookup_time_seconds", @@ -46,12 +48,6 @@ func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, r fallbackProtocol = "ip6" } - if IPProtocol == "ip6" { - fallbackProtocol = "ip4" - } else { - fallbackProtocol = "ip6" - } - level.Info(logger).Log("msg", "Resolving target address", "ip_protocol", IPProtocol) resolveStart := time.Now() @@ -60,31 +56,50 @@ func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, r probeDNSLookupTimeSeconds.Add(lookupTime) }() - ip, err = net.ResolveIPAddr(IPProtocol, target) + resolver := &net.Resolver{} + ips, err := resolver.LookupIPAddr(ctx, target) if err != nil { - if !fallbackIPProtocol { - level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false):", "err", err) - } else { - level.Warn(logger).Log("msg", "Resolution with IP protocol failed, attempting fallback protocol", "fallback_protocol", fallbackProtocol, "err", err) - ip, err = net.ResolveIPAddr(fallbackProtocol, target) - } + level.Error(logger).Log("msg", "Resolution with IP protocol failed", "err", err) + return nil, 0.0, err + } - if err != nil { - if IPProtocol == "ip6" { - probeIPProtocolGauge.Set(6) - } else { + // Return the IP in the requested protocol. + var fallback *net.IPAddr + for _, ip := range ips { + switch IPProtocol { + case "ip4": + if ip.IP.To4() != nil { + level.Info(logger).Log("msg", "Resolved target address", "ip", ip) probeIPProtocolGauge.Set(4) + return &ip, lookupTime, nil } - return ip, 0.0, err + + // ip4 as fallback + fallback = &ip + + case "ip6": + + if ip.IP.To4() == nil { + level.Info(logger).Log("msg", "Resolved target address", "ip", ip) + probeIPProtocolGauge.Set(6) + return &ip, lookupTime, nil + } + + // ip6 as fallback + fallback = &ip } } - if ip.IP.To4() == nil { - probeIPProtocolGauge.Set(6) - } else { - probeIPProtocolGauge.Set(4) + // Unable to find ip and no fallback set. + if fallback == nil { + return nil, 0.0, fmt.Errorf("unable to find ip; no fallback") } - level.Info(logger).Log("msg", "Resolved target address", "ip", ip) - return ip, lookupTime, nil + // Use fallback ip protocol. + if fallbackProtocol == "ip4" { + probeIPProtocolGauge.Set(4) + } else { + probeIPProtocolGauge.Set(6) + } + return fallback, lookupTime, nil } -- 2.25.1