-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
utils.go
121 lines (103 loc) · 3.46 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prober
import (
"context"
"fmt"
"hash/fnv"
"net"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
// 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
probeDNSLookupTimeSeconds := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_dns_lookup_time_seconds",
Help: "Returns the time taken for probe dns lookup in seconds",
})
probeIPProtocolGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_ip_protocol",
Help: "Specifies whether probe ip protocol is IP4 or IP6",
})
probeIPAddrHash := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_ip_addr_hash",
Help: "Specifies the hash of IP address. It's useful to detect if the IP address changes.",
})
registry.MustRegister(probeIPProtocolGauge)
registry.MustRegister(probeDNSLookupTimeSeconds)
registry.MustRegister(probeIPAddrHash)
if IPProtocol == "ip6" || IPProtocol == "" {
IPProtocol = "ip6"
fallbackProtocol = "ip4"
} else {
IPProtocol = "ip4"
fallbackProtocol = "ip6"
}
level.Info(logger).Log("msg", "Resolving target address", "ip_protocol", IPProtocol)
resolveStart := time.Now()
defer func() {
lookupTime = time.Since(resolveStart).Seconds()
probeDNSLookupTimeSeconds.Add(lookupTime)
}()
resolver := &net.Resolver{}
ips, err := resolver.LookupIPAddr(ctx, target)
if err != nil {
level.Error(logger).Log("msg", "Resolution with IP protocol failed", "err", err)
return nil, 0.0, err
}
// 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.String())
probeIPProtocolGauge.Set(4)
probeIPAddrHash.Set(ipHash(ip.IP))
return &ip, lookupTime, nil
}
// ip4 as fallback
fallback = &ip
case "ip6":
if ip.IP.To4() == nil {
level.Info(logger).Log("msg", "Resolved target address", "ip", ip.String())
probeIPProtocolGauge.Set(6)
probeIPAddrHash.Set(ipHash(ip.IP))
return &ip, lookupTime, nil
}
// ip6 as fallback
fallback = &ip
}
}
// Unable to find ip and no fallback set.
if fallback == nil || !fallbackIPProtocol {
return nil, 0.0, fmt.Errorf("unable to find ip; no fallback")
}
// Use fallback ip protocol.
if fallbackProtocol == "ip4" {
probeIPProtocolGauge.Set(4)
} else {
probeIPProtocolGauge.Set(6)
}
probeIPAddrHash.Set(ipHash(fallback.IP))
level.Info(logger).Log("msg", "Resolved target address", "ip", fallback.String())
return fallback, lookupTime, nil
}
func ipHash(ip net.IP) float64 {
h := fnv.New32a()
h.Write(ip)
return float64(h.Sum32())
}