-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
138 lines (114 loc) · 3.26 KB
/
handler.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package dns
import (
"context"
"fmt"
"strings"
"github.com/prometheus/client_golang/prometheus"
flowpb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/pkg/hubble/metrics/api"
)
type dnsHandler struct {
includeQuery bool
ignoreAAAA bool
context *api.ContextOptions
queries *prometheus.CounterVec
responses *prometheus.CounterVec
responseTypes *prometheus.CounterVec
}
func (d *dnsHandler) Init(registry *prometheus.Registry, options api.Options) error {
c, err := api.ParseContextOptions(options)
if err != nil {
return err
}
d.context = c
for key := range options {
switch strings.ToLower(key) {
case "query":
d.includeQuery = true
case "ignoreaaaa":
d.ignoreAAAA = true
}
}
labels := []string{"rcode", "qtypes", "ips_returned"}
if d.includeQuery {
labels = append(labels, "query")
}
labels = append(labels, d.context.GetLabelNames()...)
d.queries = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "dns_queries_total",
Help: "Number of DNS queries observed",
}, labels)
registry.MustRegister(d.queries)
d.responses = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "dns_responses_total",
Help: "Number of DNS queries observed",
}, labels)
registry.MustRegister(d.responses)
labels = []string{"type", "qtypes"}
if d.includeQuery {
labels = append(labels, "query")
}
labels = append(labels, d.context.GetLabelNames()...)
d.responseTypes = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "dns_response_types_total",
Help: "Number of DNS queries observed",
}, labels)
registry.MustRegister(d.responseTypes)
return nil
}
func (d *dnsHandler) Status() string {
var status []string
if d.includeQuery {
status = append(status, "query")
}
if d.ignoreAAAA {
status = append(status, "ignoreAAAA")
}
return strings.Join(append(status, d.context.Status()), ",")
}
func (d *dnsHandler) ProcessFlow(ctx context.Context, flow *flowpb.Flow) {
if flow.GetL7() == nil {
return
}
dns := flow.GetL7().GetDns()
if dns == nil {
return
}
if d.ignoreAAAA && len(dns.Qtypes) == 1 && dns.Qtypes[0] == "AAAA" {
return
}
labelValues := d.context.GetLabelValues(flow)
labels := []string{"", strings.Join(dns.Qtypes, ","), fmt.Sprintf("%d", len(dns.Ips))}
if d.includeQuery {
labels = append(labels, dns.Query)
}
labels = append(labels, labelValues...)
if flow.GetVerdict() == flowpb.Verdict_DROPPED {
d.queries.WithLabelValues(labels...).Inc()
labels[0] = "Policy denied"
d.responses.WithLabelValues(labels...).Inc()
} else {
if flow.GetIsReply().GetValue() {
labels[0] = rcodeNames[dns.Rcode]
d.responses.WithLabelValues(labels...).Inc()
if len(dns.Rrtypes) > 0 {
labels := []string{"", strings.Join(dns.Qtypes, ",")}
if d.includeQuery {
labels = append(labels, dns.Query)
}
labels = append(labels, d.context.GetLabelValues(flow)...)
for _, t := range dns.Rrtypes {
labels[0] = t
d.responseTypes.WithLabelValues(labels...).Inc()
}
}
} else {
d.queries.WithLabelValues(labels...).Inc()
}
}
}