Skip to content

Commit

Permalink
Merge pull request #1 from thorfour/choose-protocol-timeout
Browse files Browse the repository at this point in the history
Choose protocol timeout
  • Loading branch information
thorfour authored Apr 10, 2019
2 parents e7e450c + db05728 commit 2735fe8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion prober/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion prober/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion prober/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 37 additions & 22 deletions prober/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package prober

import (
"context"
"net"
"time"

Expand All @@ -24,7 +25,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",
Expand Down Expand Up @@ -60,31 +61,45 @@ func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, r
probeDNSLookupTimeSeconds.Add(lookupTime)
}()

ip, err = net.ResolveIPAddr(IPProtocol, 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)
}

ipC := make(chan *net.IPAddr, 1)
errors := make(chan error, 1)
go func() {
defer close(ipC)
defer close(errors)
ip, err := net.ResolveIPAddr(IPProtocol, target)
if err != nil {
if IPProtocol == "ip6" {
probeIPProtocolGauge.Set(6)
if !fallbackIPProtocol {
level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false):", "err", err)
} else {
probeIPProtocolGauge.Set(4)
level.Warn(logger).Log("msg", "Resolution with IP protocol failed, attempting fallback protocol", "fallback_protocol", fallbackProtocol, "err", err)
ip, err = net.ResolveIPAddr(fallbackProtocol, target)
}

if err != nil {
if IPProtocol == "ip6" {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
}
errors <- err
return
}
return ip, 0.0, err
}
}

if ip.IP.To4() == nil {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
}
if ip.IP.To4() == nil {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
}

level.Info(logger).Log("msg", "Resolved target address", "ip", ip)
return ip, lookupTime, nil
ipC <- ip
}()

select {
case <-ctx.Done():
return nil, lookupTime, ctx.Err()
case ip := <-ipC:
level.Info(logger).Log("msg", "Resolved target address", "ip", ip)
return ip, lookupTime, nil
}
}

0 comments on commit 2735fe8

Please sign in to comment.