-
Notifications
You must be signed in to change notification settings - Fork 457
/
push.go
355 lines (326 loc) · 10.6 KB
/
push.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2014 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handler
import (
"fmt"
"io"
"mime"
"net"
"net/http"
"sort"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/julienschmidt/httprouter"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/pushgateway/storage"
)
const (
pushMetricName = "push_time_seconds"
pushMetricHelp = "Last Unix time when this group was changed in the Pushgateway."
)
// Push returns an http.Handler which accepts samples over HTTP and stores them
// in the MetricStore. If replace is true, all metrics for the job and instance
// given by the request are deleted before new ones are stored.
//
// The returned handler is already instrumented for Prometheus.
func Push(
ms storage.MetricStore, replace bool,
) func(http.ResponseWriter, *http.Request, httprouter.Params) {
var ps httprouter.Params
var mtx sync.Mutex // Protects ps.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
job := ps.ByName("job")
labelsString := ps.ByName("labels")
mtx.Unlock()
labels, err := splitLabels(labelsString)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Debugf("Failed to parse URL: %v, %v", labelsString, err.Error())
return
}
if job == "" {
http.Error(w, "job name is required", http.StatusBadRequest)
log.Debug("job name is required")
return
}
labels["job"] = job
if replace {
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: time.Now(),
})
}
var metricFamilies map[string]*dto.MetricFamily
ctMediatype, ctParams, ctErr := mime.ParseMediaType(r.Header.Get("Content-Type"))
if ctErr == nil && ctMediatype == "application/vnd.google.protobuf" &&
ctParams["encoding"] == "delimited" &&
ctParams["proto"] == "io.prometheus.client.MetricFamily" {
metricFamilies = map[string]*dto.MetricFamily{}
for {
mf := &dto.MetricFamily{}
if _, err = pbutil.ReadDelimited(r.Body, mf); err != nil {
if err == io.EOF {
err = nil
}
break
}
metricFamilies[mf.GetName()] = mf
}
} else {
// We could do further content-type checks here, but the
// fallback for now will anyway be the text format
// version 0.0.4, so just go for it and see if it works.
var parser expfmt.TextParser
metricFamilies, err = parser.TextToMetricFamilies(r.Body)
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Debugf("Failed to parse text, %v", err.Error())
return
}
if timestampsPresent(metricFamilies) {
http.Error(w, "pushed metrics must not have timestamps", http.StatusBadRequest)
log.Debug("pushed metrics must not have timestamps")
return
}
now := time.Now()
addPushTimestamp(metricFamilies, now)
sanitizeLabels(metricFamilies, labels)
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: now,
MetricFamilies: metricFamilies,
})
w.WriteHeader(http.StatusAccepted)
})
instrumentedHandler := promhttp.InstrumentHandlerRequestSize(
httpPushSize, promhttp.InstrumentHandlerDuration(
httpPushDuration, promhttp.InstrumentHandlerCounter(
httpCnt.MustCurryWith(prometheus.Labels{"handler": "push"}),
handler,
)))
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
mtx.Lock()
ps = params
instrumentedHandler.ServeHTTP(w, r)
}
}
// LegacyPush returns an http.Handler which accepts samples over HTTP and stores
// them in the MetricStore. It uses the deprecated API (expecting a 'job'
// parameter and an optional 'instance' parameter). If replace is true, all
// metrics for the job and instance given by the request are deleted before new
// ones are stored.
//
// The returned handler is already instrumented for Prometheus.
func LegacyPush(
ms storage.MetricStore, replace bool,
) func(http.ResponseWriter, *http.Request, httprouter.Params) {
var ps httprouter.Params
var mtx sync.Mutex // Protects ps.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
job := ps.ByName("job")
instance := ps.ByName("instance")
mtx.Unlock()
var err error
if job == "" {
http.Error(w, "job name is required", http.StatusBadRequest)
log.Debug("job name is required")
return
}
if instance == "" {
// Remote IP number (without port).
instance, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil || instance == "" {
instance = "localhost"
}
}
labels := map[string]string{"job": job, "instance": instance}
if replace {
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: time.Now(),
})
}
var metricFamilies map[string]*dto.MetricFamily
ctMediatype, ctParams, ctErr := mime.ParseMediaType(r.Header.Get("Content-Type"))
if ctErr == nil && ctMediatype == "application/vnd.google.protobuf" &&
ctParams["encoding"] == "delimited" &&
ctParams["proto"] == "io.prometheus.client.MetricFamily" {
metricFamilies = map[string]*dto.MetricFamily{}
for {
mf := &dto.MetricFamily{}
if _, err = pbutil.ReadDelimited(r.Body, mf); err != nil {
if err == io.EOF {
err = nil
}
break
}
metricFamilies[mf.GetName()] = mf
}
} else {
// We could do further content-type checks here, but the
// fallback for now will anyway be the text format
// version 0.0.4, so just go for it and see if it works.
var parser expfmt.TextParser
metricFamilies, err = parser.TextToMetricFamilies(r.Body)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Debugf("Error parsing request body, %v", err.Error())
return
}
if timestampsPresent(metricFamilies) {
http.Error(w, "pushed metrics must not have timestamps", http.StatusBadRequest)
log.Debug("pushed metrics must not have timestamps")
return
}
now := time.Now()
addPushTimestamp(metricFamilies, now)
sanitizeLabels(metricFamilies, labels)
ms.SubmitWriteRequest(storage.WriteRequest{
Labels: labels,
Timestamp: now,
MetricFamilies: metricFamilies,
})
w.WriteHeader(http.StatusAccepted)
})
instrumentedHandler := promhttp.InstrumentHandlerRequestSize(
httpPushSize, promhttp.InstrumentHandlerDuration(
httpPushDuration, promhttp.InstrumentHandlerCounter(
httpCnt.MustCurryWith(prometheus.Labels{"handler": "legacy_push"}),
handler,
)))
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
mtx.Lock()
ps = params
instrumentedHandler.ServeHTTP(w, r)
}
}
// sanitizeLabels ensures that all the labels in groupingLabels and the
// `instance` label are present in each MetricFamily in metricFamilies. The
// label values from groupingLabels are set in each MetricFamily, no matter
// what. After that, if the 'instance' label is not present at all in a
// MetricFamily, it will be created (with an empty string as value).
//
// Finally, sanitizeLabels sorts the label pairs of all metrics.
func sanitizeLabels(
metricFamilies map[string]*dto.MetricFamily,
groupingLabels map[string]string,
) {
gLabelsNotYetDone := make(map[string]string, len(groupingLabels))
for _, mf := range metricFamilies {
metric:
for _, m := range mf.GetMetric() {
for ln, lv := range groupingLabels {
gLabelsNotYetDone[ln] = lv
}
hasInstanceLabel := false
for _, lp := range m.GetLabel() {
ln := lp.GetName()
if lv, ok := gLabelsNotYetDone[ln]; ok {
lp.Value = proto.String(lv)
delete(gLabelsNotYetDone, ln)
}
if ln == string(model.InstanceLabel) {
hasInstanceLabel = true
}
if len(gLabelsNotYetDone) == 0 && hasInstanceLabel {
sort.Sort(labelPairs(m.Label))
continue metric
}
}
for ln, lv := range gLabelsNotYetDone {
m.Label = append(m.Label, &dto.LabelPair{
Name: proto.String(ln),
Value: proto.String(lv),
})
if ln == string(model.InstanceLabel) {
hasInstanceLabel = true
}
delete(gLabelsNotYetDone, ln) // To prepare map for next metric.
}
if !hasInstanceLabel {
m.Label = append(m.Label, &dto.LabelPair{
Name: proto.String(string(model.InstanceLabel)),
Value: proto.String(""),
})
}
sort.Sort(labelPairs(m.Label))
}
}
}
// splitLabels splits a labels string into a label map mapping names to values.
func splitLabels(labels string) (map[string]string, error) {
result := map[string]string{}
if len(labels) <= 1 {
return result, nil
}
components := strings.Split(labels[1:], "/")
if len(components)%2 != 0 {
return nil, fmt.Errorf("odd number of components in label string %q", labels)
}
for i := 0; i < len(components)-1; i += 2 {
if !model.LabelNameRE.MatchString(components[i]) ||
strings.HasPrefix(components[i], model.ReservedLabelPrefix) {
return nil, fmt.Errorf("improper label name %q", components[i])
}
result[components[i]] = components[i+1]
}
return result, nil
}
// Checks if any timestamps have been specified.
func timestampsPresent(metricFamilies map[string]*dto.MetricFamily) bool {
for _, mf := range metricFamilies {
for _, m := range mf.GetMetric() {
if m.TimestampMs != nil {
return true
}
}
}
return false
}
// Add metric to indicate the push time.
func addPushTimestamp(metricFamilies map[string]*dto.MetricFamily, t time.Time) {
metricFamilies[pushMetricName] = &dto.MetricFamily{
Name: proto.String(pushMetricName),
Help: proto.String(pushMetricHelp),
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
{
Gauge: &dto.Gauge{
Value: proto.Float64(float64(t.UnixNano()) / 1e9),
},
},
},
}
}
// labelPairs implements sort.Interface. It provides a sortable version of a
// slice of dto.LabelPair pointers.
type labelPairs []*dto.LabelPair
func (s labelPairs) Len() int {
return len(s)
}
func (s labelPairs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s labelPairs) Less(i, j int) bool {
return s[i].GetName() < s[j].GetName()
}