Skip to content

Commit

Permalink
Simple implementation of single-resolver-call without fallback
Browse files Browse the repository at this point in the history
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>
roidelapluie committed May 28, 2021
1 parent 54cba1b commit e3b594c
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions prober/utils.go
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion prober/utils_test.go
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit e3b594c

Please sign in to comment.