Debian Codegraph @

cmd/atomdns/handlers/metrics/metrics.go

raw
1 package metrics
2
3 import (
4 "bytes"
5 "context"
6 "io"
7 "strconv"
8 "sync"
9 "time"
10
11 "codeberg.org/miekg/dns"
12 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsmetrics"
13 "codeberg.org/miekg/dns/dnstest"
14 "codeberg.org/miekg/dns/dnsutil"
15 "github.com/prometheus/client_golang/prometheus"
16 "github.com/prometheus/client_golang/prometheus/promauto"
17 )
18
19 type Metrics struct {
20 disable bool
21
22 i uint64
23 N uint64
24 }
25
26 func (m *Metrics) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc {
27 return dns.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) {
28 if m.disable || m.N == 0 {
29 next.ServeDNS(ctx, w, r)
30 return
31 }
32 if !dnsmetrics.Should(&m.i, m.N) {
33 next.ServeDNS(ctx, w, r)
34 return
35 }
36
37 net := dnsutil.Network(w)
38 fam := strconv.Itoa(dnsutil.Family(w))
39 flags := bufPool.Get().(*bytes.Buffer)
40 if r.Security {
41 flags.WriteString(" do")
42 }
43 if r.CompactAnswers {
44 flags.WriteString(" co")
45 }
46 if r.Delegation {
47 flags.WriteString(" de")
48 }
49 if r.AuthenticatedData {
50 flags.WriteString(" ad")
51 }
52 if r.CheckingDisabled {
53 flags.WriteString(" cd")
54 }
55 if flags.Len() > 1 {
56 Requests.WithLabelValues(dns.Zone(ctx), net, fam, string(flags.Bytes()[1:])).Inc()
57 } else {
58 Requests.WithLabelValues(dns.Zone(ctx), net, fam, "").Inc()
59 }
60 flags.Reset()
61 bufPool.Put(flags)
62
63 RequestSize.WithLabelValues(dns.Zone(ctx), net, fam).Observe(float64(len(r.Data)))
64
65 rw := dnstest.NewRecorder(w)
66 next.ServeDNS(ctx, rw, r)
67
68 // if being hijacked, we don't have anything here, we check for that by checking if we actually have a
69 // message written to us.
70 if rw.Msg == nil {
71 return
72 }
73
74 rw.Msg.Options = dns.MsgOptionUnpackHeader // we only need to rcode
75 rw.Msg.Unpack()
76 RequestDuration.WithLabelValues(dns.Zone(ctx), net, fam).Observe(time.Since(rw.Start).Seconds())
77 ResponseSize.WithLabelValues(dns.Zone(ctx), net, fam).Observe(float64(len(rw.Msg.Data)))
78 Responses.WithLabelValues(dns.Zone(ctx), net, fam, dnsutil.RcodeToString(rw.Msg.Rcode)).Inc()
79
80 io.Copy(w, rw.Msg)
81 })
82 }
83
84 var bufPool = &sync.Pool{
85 New: func() any {
86 return new(bytes.Buffer)
87 },
88 }
89
90 var (
91 Dropped = promauto.NewCounter(prometheus.CounterOpts{
92 Namespace: Namespace, Subsystem: subsystem,
93 Name: "dropped_total",
94 Help: "Counter of dropped requests. These are reported via the server's MsgInvalidFunc.",
95 })
96
97 Requests = promauto.NewCounterVec(prometheus.CounterOpts{
98 Namespace: Namespace, Subsystem: subsystem,
99 Name: "requests_total",
100 Help: "Counter of requests made per zone, network and family.",
101 }, []string{"zone", "network", "family", "flags"})
102
103 Responses = promauto.NewCounterVec(prometheus.CounterOpts{
104 Namespace: Namespace, Subsystem: subsystem,
105 Name: "responses_total",
106 Help: "Counter of responses and response codes.",
107 }, []string{"zone", "network", "family", "rcode"})
108
109 RequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
110 Namespace: Namespace, Subsystem: subsystem,
111 Name: "request_duration_seconds",
112 Buckets: prometheus.ExponentialBuckets(0.00025, 2, 16), // from 0.25ms to 8 seconds
113 Help: "Histogram of the time (in seconds) each request took per zone.",
114 }, []string{"zone", "network", "family"})
115
116 RequestSize = promauto.NewHistogramVec(prometheus.HistogramOpts{
117 Namespace: Namespace, Subsystem: subsystem,
118 Name: "request_size_bytes",
119 Help: "Size of the requests.",
120 Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
121 }, []string{"zone", "network", "family"})
122
123 ResponseSize = promauto.NewHistogramVec(prometheus.HistogramOpts{
124 Namespace: Namespace, Subsystem: subsystem,
125 Name: "response_size_bytes",
126 Help: "Size of the returned response in bytes.",
127 Buckets: []float64{0, 100, 200, 300, 400, 511, 1023, 2047, 4095, 8291, 16e3, 32e3, 48e3, 64e3},
128 }, []string{"zone", "network", "family"})
129 )
130
131 const (
132 subsystem = "dns"
133 Namespace = "atomdns"
134 )