-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathhandler.go
292 lines (247 loc) · 8.46 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package receive
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
"unsafe"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/promql"
)
const forwardTimeout = 5 * time.Second
// DefaultRequestLimit is the size limit of a request body coming in
const DefaultRequestLimit = 128 * 1024
// ClusterAuthorizer authorizes a cluster by its token and id, returning a subject or error
type ClusterAuthorizer interface {
AuthorizeCluster(token, cluster string) (subject string, err error)
}
// Handler knows the forwardURL for all requests
type Handler struct {
ForwardURL string
tenantID string
client *http.Client
logger log.Logger
elideLabelSet map[string]struct{}
matcherSets [][]*labels.Matcher
// Metrics.
forwardRequestsTotal *prometheus.CounterVec
}
// NewHandler returns a new Handler with a http client
func NewHandler(logger log.Logger, forwardURL string, client *http.Client, reg prometheus.Registerer, tenantID string, whitelistRules []string, elideLabels []string) (*Handler, error) {
h := &Handler{
ForwardURL: forwardURL,
tenantID: tenantID,
client: client,
logger: log.With(logger, "component", "receive/handler"),
forwardRequestsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "telemeter_forward_requests_total",
Help: "The number of forwarded remote-write requests.",
}, []string{"result"},
),
}
var ms [][]*labels.Matcher
for _, rule := range whitelistRules {
matchers, err := promql.ParseMetricSelector(rule)
if err != nil {
return nil, err
}
ms = append(ms, matchers)
}
h.matcherSets = ms
h.elideLabelSet = make(map[string]struct{})
for _, l := range elideLabels {
h.elideLabelSet[l] = struct{}{}
}
if reg != nil {
reg.MustRegister(h.forwardRequestsTotal)
}
return h, nil
}
// Receive a remote-write request after it has been authenticated and forward it to Thanos
func (h *Handler) Receive(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
ctx, cancel := context.WithTimeout(r.Context(), forwardTimeout)
defer cancel()
req, err := http.NewRequest(http.MethodPost, h.ForwardURL, r.Body)
if err != nil {
level.Error(h.logger).Log("msg", "failed to create forward request", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// No need for adding a tenant header, as this is done by downstream Observatorium API.
req = req.WithContext(ctx)
resp, err := h.client.Do(req)
if err != nil {
h.forwardRequestsTotal.WithLabelValues("error").Inc()
level.Error(h.logger).Log("msg", "failed to forward request", "err", err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
if resp.StatusCode/100 != 2 {
// Return upstream error as well.
body, err := ioutil.ReadAll(resp.Body)
msg := fmt.Sprintf("upstream response status is not 200 OK: %s", body)
if err != nil {
msg = fmt.Sprintf("upstream response status is not 200 OK: couldn't read body %v", err)
}
h.forwardRequestsTotal.WithLabelValues("error").Inc()
level.Error(h.logger).Log("msg", msg, "statuscode", resp.Status)
http.Error(w, msg, resp.StatusCode)
return
}
h.forwardRequestsTotal.WithLabelValues("success").Inc()
w.WriteHeader(resp.StatusCode)
}
// LimitBodySize is a middleware that check that the request body is not bigger than the limit
func LimitBodySize(logger log.Logger, limit int64, next http.Handler) http.HandlerFunc {
logger = log.With(logger, "component", "receive", "middleware", "LimitBodySize")
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
level.Error(logger).Log("msg", "failed to read body", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set body to this buffer for other handlers to read
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if len(body) >= int(limit) {
level.Debug(logger).Log("msg", "request is too big", "req_size", len(body))
http.Error(w, "request too big", http.StatusRequestEntityTooLarge)
return
}
next.ServeHTTP(w, r)
}
}
var ErrRequiredLabelMissing = fmt.Errorf("a required label is missing from the metric")
// ValidateLabels by checking each enforced label to be present in every time series
func ValidateLabels(logger log.Logger, next http.Handler, labels ...string) http.HandlerFunc {
logger = log.With(logger, "component", "receive", "middleware", "validateLabels")
labelmap := make(map[string]struct{})
for _, label := range labels {
labelmap[label] = struct{}{}
}
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
level.Error(logger).Log("msg", "failed to read body", "err", err)
http.Error(w, "failed to read body", http.StatusInternalServerError)
return
}
// Set body to this buffer for other handlers to read
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
content, err := snappy.Decode(nil, body)
if err != nil {
level.Warn(logger).Log("msg", "failed to decode request body", "err", err)
http.Error(w, "failed to decode request body", http.StatusBadRequest)
return
}
var wreq prompb.WriteRequest
if err := proto.Unmarshal(content, &wreq); err != nil {
level.Warn(logger).Log("msg", "failed to decode protobuf from body", "err", err)
http.Error(w, "failed to decode protobuf from body", http.StatusBadRequest)
return
}
for _, ts := range wreq.GetTimeseries() {
// exit early if not enough labels anyway
if len(ts.GetLabels()) < len(labels) {
level.Warn(logger).Log("msg", "request is missing required labels", "err", ErrRequiredLabelMissing)
http.Error(w, ErrRequiredLabelMissing.Error(), http.StatusBadRequest)
return
}
found := 0
for _, l := range ts.GetLabels() {
if _, ok := labelmap[l.GetName()]; ok {
found++
}
}
if len(labels) != found {
level.Warn(logger).Log("msg", "request is missing required labels", "err", ErrRequiredLabelMissing)
http.Error(w, ErrRequiredLabelMissing.Error(), http.StatusBadRequest)
return
}
}
next.ServeHTTP(w, r)
}
}
func (h *Handler) TransformWriteRequest(logger log.Logger, next http.Handler) http.HandlerFunc {
logger = log.With(h.logger, "middleware", "transformWriteRequest")
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
level.Error(logger).Log("msg", "failed to read body", "err", err)
http.Error(w, "failed to read body", http.StatusInternalServerError)
return
}
content, err := snappy.Decode(nil, body)
if err != nil {
level.Warn(logger).Log("msg", "failed to decode request body", "err", err)
http.Error(w, "failed to decode request body", http.StatusBadRequest)
return
}
var wreq prompb.WriteRequest
if err := proto.Unmarshal(content, &wreq); err != nil {
level.Warn(logger).Log("msg", "failed to decode protobuf from body", "err", err)
http.Error(w, "failed to decode protobuf from body", http.StatusBadRequest)
return
}
// Only allow whitelisted metrics.
n := 0
for _, ts := range wreq.GetTimeseries() {
if h.matches(PrompbLabelsToPromLabels(ts.GetLabels())) {
// Remove elided labels.
for i, l := range ts.Labels {
if _, elide := h.elideLabelSet[l.Name]; elide {
ts.Labels = append(ts.Labels[:i], ts.Labels[i+1:]...)
}
}
wreq.Timeseries[n] = ts
n++
}
}
wreq.Timeseries = wreq.Timeseries[:n]
data, err := proto.Marshal(&wreq)
if err != nil {
msg := "failed to marshal proto"
level.Warn(logger).Log("msg", msg, "err", err)
http.Error(w, msg, http.StatusInternalServerError)
return
}
compressed := snappy.Encode(nil, data)
// Set body to this buffer for other handlers to read.
r.Body = ioutil.NopCloser(bytes.NewBuffer(compressed))
next.ServeHTTP(w, r)
}
}
func (h *Handler) matches(l labels.Labels) bool {
if len(h.matcherSets) == 0 {
return true
}
for _, matchers := range h.matcherSets {
for _, m := range matchers {
if v := l.Get(m.Name); !m.Matches(v) {
return false
}
}
}
return true
}
func PrompbLabelsToPromLabels(lset []prompb.Label) labels.Labels {
return *(*labels.Labels)(unsafe.Pointer(&lset))
}