-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (98 loc) · 2.57 KB
/
main.go
File metadata and controls
125 lines (98 loc) · 2.57 KB
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
122
123
124
125
package main
import (
"log"
"net"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
Namespace = "dns_exporter"
)
type Config struct {
Hosts []string
}
type DNSCollector struct {
total *prometheus.Desc
totalError *prometheus.Desc
latency *prometheus.Desc
hosts []string
totalCount map[string]int
totalCountMutex sync.Mutex
totalErrorCount map[string]int
totalErrorCountMutex sync.Mutex
}
func NewDNSCollector(config Config) (*DNSCollector, error) {
dnsCollector := &DNSCollector{
total: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "resolution_total"),
"Total number of DNS resolutions.",
[]string{"host"},
nil,
),
totalError: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "resolution_error_total"),
"Total number of DNS resolution errors.",
[]string{"host"},
nil,
),
latency: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", "resolution_seconds"),
"Time taken to resolve DNS.",
[]string{"host"},
nil,
),
hosts: config.Hosts,
totalCount: map[string]int{},
totalErrorCount: map[string]int{},
}
return dnsCollector, nil
}
func (e *DNSCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- e.total
ch <- e.totalError
ch <- e.latency
}
func (e *DNSCollector) Collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
wg.Add(len(e.hosts))
for _, host := range e.hosts {
go func(host string) {
defer wg.Done()
e.resolveHost(ch, host)
}(host)
}
wg.Wait()
}
func (e *DNSCollector) resolveHost(ch chan<- prometheus.Metric, host string) {
start := time.Now()
_, err := net.LookupHost(host)
if err != nil {
e.totalErrorCountMutex.Lock()
e.totalErrorCount[host] += 1
e.totalErrorCountMutex.Unlock()
log.Printf("could not lookup host '%s': %s", host, err)
}
elapsed := time.Since(start)
e.totalCountMutex.Lock()
e.totalCount[host] += 1
e.totalCountMutex.Unlock()
ch <- prometheus.MustNewConstMetric(e.total, prometheus.CounterValue, float64(e.totalCount[host]), host)
ch <- prometheus.MustNewConstMetric(e.totalError, prometheus.CounterValue, float64(e.totalErrorCount[host]), host)
ch <- prometheus.MustNewConstMetric(e.latency, prometheus.GaugeValue, elapsed.Seconds(), host)
}
func main() {
dnsCollector, err := NewDNSCollector(Config{
Hosts: []string{
"example.org",
"google.com",
},
})
if err != nil {
log.Fatalf("could not create dns collector: %s", err)
}
prometheus.MustRegister(dnsCollector)
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe("localhost:8000", nil)
}