"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
}()
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)
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")