-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
handler.go
179 lines (157 loc) · 5.56 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package http
import (
"context"
"fmt"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
flowpb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/pkg/hubble/metrics/api"
)
type httpHandler struct {
requests *prometheus.CounterVec
responses *prometheus.CounterVec
duration *prometheus.HistogramVec
context *api.ContextOptions
useV2 bool
exemplars bool
}
func (h *httpHandler) Init(registry *prometheus.Registry, options api.Options) error {
c, err := api.ParseContextOptions(options)
if err != nil {
return err
}
h.context = c
if exemplars, ok := options["exemplars"]; ok && exemplars == "true" {
h.exemplars = true
}
if h.useV2 {
h.requests = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "http_requests_total",
Help: "Count of HTTP requests",
}, append(h.context.GetLabelNames(), "method", "protocol", "status", "reporter"))
h.duration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "http_request_duration_seconds",
Help: "Quantiles of HTTP request duration in seconds",
}, append(h.context.GetLabelNames(), "method", "reporter"))
registry.MustRegister(h.requests)
registry.MustRegister(h.duration)
} else {
h.requests = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "http_requests_total",
Help: "Count of HTTP requests",
}, append(h.context.GetLabelNames(), "method", "protocol", "reporter"))
h.responses = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "http_responses_total",
Help: "Count of HTTP responses",
}, append(h.context.GetLabelNames(), "method", "protocol", "status", "reporter"))
h.duration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: api.DefaultPrometheusNamespace,
Name: "http_request_duration_seconds",
Help: "Quantiles of HTTP request duration in seconds",
}, append(h.context.GetLabelNames(), "method", "reporter"))
registry.MustRegister(h.requests)
registry.MustRegister(h.responses)
registry.MustRegister(h.duration)
}
return nil
}
func (h *httpHandler) Status() string {
if h.context == nil {
return ""
}
return h.context.Status() + fmt.Sprintf(",exemplars=%t", h.exemplars)
}
func (h *httpHandler) ProcessFlow(ctx context.Context, flow *flowpb.Flow) error {
if h.useV2 {
return h.processMetricsV2(flow)
} else {
return h.processMetricsV1(flow)
}
}
func (h *httpHandler) isHTTP(flow *flowpb.Flow) bool {
return flow.GetL7().GetHttp() != nil
}
func (h *httpHandler) reporter(flow *flowpb.Flow) string {
reporter := "unknown"
switch flow.GetTrafficDirection() {
case flowpb.TrafficDirection_EGRESS:
reporter = "client"
case flowpb.TrafficDirection_INGRESS:
reporter = "server"
}
return reporter
}
func (h *httpHandler) traceID(flow *flowpb.Flow) string {
if h.exemplars {
return flow.GetTraceContext().GetParent().GetTraceId()
}
return ""
}
func (h *httpHandler) processMetricsV2(flow *flowpb.Flow) error {
if !h.isHTTP(flow) || flow.GetL7().GetType() != flowpb.L7FlowType_RESPONSE {
return nil
}
reporter := h.reporter(flow)
traceID := h.traceID(flow)
labelValues, err := h.context.GetLabelValuesInvertSourceDestination(flow)
if err != nil {
return err
}
http := flow.GetL7().GetHttp()
status := strconv.Itoa(int(http.GetCode()))
requestsCounter := h.requests.WithLabelValues(append(labelValues, http.GetMethod(), http.GetProtocol(), status, reporter)...)
requestDurationHistogram := h.duration.WithLabelValues(append(labelValues, http.GetMethod(), reporter)...)
incrementCounter(requestsCounter, traceID)
observerObserve(requestDurationHistogram, float64(flow.GetL7().GetLatencyNs())/float64(time.Second), traceID)
return nil
}
func (h *httpHandler) processMetricsV1(flow *flowpb.Flow) error {
if !h.isHTTP(flow) {
return nil
}
flowType := flow.GetL7().GetType()
if flowType != flowpb.L7FlowType_REQUEST && flowType != flowpb.L7FlowType_RESPONSE {
return nil
}
reporter := h.reporter(flow)
traceID := h.traceID(flow)
labelValues, err := h.context.GetLabelValues(flow)
if err != nil {
return err
}
http := flow.GetL7().GetHttp()
var requestsCounter, responsesCounter prometheus.Counter
switch flow.GetL7().GetType() {
case flowpb.L7FlowType_REQUEST:
requestsCounter = h.requests.WithLabelValues(append(labelValues, http.GetMethod(), http.GetProtocol(), reporter)...)
incrementCounter(requestsCounter, traceID)
case flowpb.L7FlowType_RESPONSE:
status := strconv.Itoa(int(http.GetCode()))
responsesCounter = h.responses.WithLabelValues(append(labelValues, http.GetMethod(), http.GetProtocol(), status, reporter)...)
requestDurationHistogram := h.duration.WithLabelValues(append(labelValues, http.GetMethod(), reporter)...)
incrementCounter(responsesCounter, traceID)
observerObserve(requestDurationHistogram, float64(flow.GetL7().GetLatencyNs())/float64(time.Second), traceID)
}
return nil
}
func incrementCounter(c prometheus.Counter, traceID string) {
if adder, ok := c.(prometheus.ExemplarAdder); ok && traceID != "" {
adder.AddWithExemplar(1, prometheus.Labels{"traceID": traceID})
} else {
c.Inc()
}
}
func observerObserve(o prometheus.Observer, value float64, traceID string) {
if adder, ok := o.(prometheus.ExemplarObserver); ok && traceID != "" {
adder.ObserveWithExemplar(value, prometheus.Labels{"traceID": traceID})
} else {
o.Observe(value)
}
}