forked from google/cloudprober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
356 lines (320 loc) · 11.4 KB
/
prometheus.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
356
// Copyright 2017 Google Inc.
//
// 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 prometheus provides a prometheus surfacer for Cloudprober. Prometheus
surfacer exports incoming metrics over a web interface in a format that
prometheus understands (http://prometheus.io).
This surfacer processes each incoming EventMetrics and holds the latest value
and timestamp for each metric in memory. These metrics are made available
through a web URL (default: /metrics), which Prometheus scrapes at a regular
interval.
Example /metrics page:
#TYPE sent counter
sent{ptype="dns",probe="vm-to-public-dns",dst="8.8.8.8"} 181299 1497330037000
sent{ptype="ping",probe="vm-to-public-dns",dst="8.8.4.4"} 362600 1497330037000
#TYPE rcvd counter
rcvd{ptype="dns",probe="vm-to-public-dns",dst="8.8.8.8"} 181234 1497330037000
rcvd{ptype="ping",probe="vm-to-public-dns",dst="8.8.4.4"} 362600 1497330037000
*/
package prometheus
import (
"context"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/google/cloudprober/logger"
"github.com/google/cloudprober/metrics"
)
// Prometheus metric and label names should match the following regular
// expressions. Since, "-" is commonly used in metric and label names, we
// replace it by "_". If a name still doesn't match the regular expression, we
// ignore it with a warning log message.
const (
ValidMetricNameRegex = "^[a-zA-Z_:]([a-zA-Z0-9_:])*$"
ValidLabelNameRegex = "^[a-zA-Z_]([a-zA-Z0-9_])*$"
)
const histogram = "histogram"
// queriesQueueSize defines how many queries can we queue before we start
// blocking on previous queries to finish.
const queriesQueueSize = 10
var (
// Cache of EventMetric label to prometheus label mapping. We use it to
// quickly lookup if we have already seen a label and we have a prometheus
// label corresponding to it.
promLabelNames = make(map[string]string)
// Cache of EventMetric metric to prometheus metric mapping. We use it to
// quickly lookup if we have already seen a metric and we have a prometheus
// metric name corresponding to it.
promMetricNames = make(map[string]string)
)
type promMetric struct {
typ string
data map[string]*dataPoint
dataKeys []string // To keep data keys ordered
}
type dataPoint struct {
value string
timestamp int64
}
// PromSurfacer implements a prometheus surfacer for Cloudprober. PromSurfacer
// organizes metrics into a two-level data structure:
// 1. Metric name -> PromMetric data structure dict.
// 2. A PromMetric organizes data associated with a metric in a
// Data key -> Data point map, where data point consists of a value
// and timestamp.
// Data key represents a unique combination of metric name and labels.
type PromSurfacer struct {
c *SurfacerConf // Configuration
emChan chan *metrics.EventMetrics // Buffered channel to store incoming EventMetrics
metrics map[string]*promMetric // Metric name to promMetric mapping
metricNames []string // Metric names, to keep names ordered.
queryChan chan http.ResponseWriter // Query channel
l *logger.Logger
// A handler that takes a promMetric and a dataKey and writes the
// corresponding metric string to the provided io.Writer.
dataWriter func(w io.Writer, pm *promMetric, dataKey string)
// Regexes for metric and label names.
metricNameRe *regexp.Regexp
labelNameRe *regexp.Regexp
}
// New returns a prometheus surfacer based on the config provided. It sets up a
// goroutine to process both the incoming EventMetrics and the web requests for
// the URL handler /metrics.
func New(config *SurfacerConf, l *logger.Logger) (*PromSurfacer, error) {
if config == nil {
config = &SurfacerConf{}
}
ps := &PromSurfacer{
c: config,
emChan: make(chan *metrics.EventMetrics, config.GetMetricsBufferSize()),
queryChan: make(chan http.ResponseWriter, queriesQueueSize),
metrics: make(map[string]*promMetric),
metricNameRe: regexp.MustCompile(ValidMetricNameRegex),
labelNameRe: regexp.MustCompile(ValidLabelNameRegex),
l: l,
}
if ps.c.GetIncludeTimestamp() {
ps.dataWriter = func(w io.Writer, pm *promMetric, k string) {
fmt.Fprintf(w, "%s %s %d\n", k, pm.data[k].value, pm.data[k].timestamp)
}
} else {
ps.dataWriter = func(w io.Writer, pm *promMetric, k string) {
fmt.Fprintf(w, "%s %s\n", k, pm.data[k].value)
}
}
done := make(chan interface{}, 1)
// Start a goroutine to process the incoming EventMetrics as well as
// the incoming web queries. To avoid data access race conditions, we do
// one thing at a time.
go func() {
for {
select {
case em := <-ps.emChan:
ps.record(em)
case w := <-ps.queryChan:
ps.writeData(w)
done <- true
}
}
}()
http.HandleFunc(ps.c.GetMetricsUrl(), func(w http.ResponseWriter, r *http.Request) {
ps.queryChan <- w
<-done
})
l.Infof("Initialized prometheus exporter at the URL: %s", ps.c.GetMetricsUrl())
return ps, nil
}
// Write simply queues the EventMetrics for later processing.
func (ps *PromSurfacer) Write(ctxIn context.Context, em *metrics.EventMetrics) {
ps.emChan <- em
}
func promType(em *metrics.EventMetrics) string {
switch em.Kind {
case metrics.CUMULATIVE:
return "counter"
case metrics.GAUGE:
return "gauge"
default:
return "unknown"
}
}
// promTime converts time.Time to Unix milliseconds.
func promTime(t time.Time) int64 {
return t.UnixNano() / (1000 * 1000)
}
func (ps *PromSurfacer) recordMetric(metricName string, labels []string, value string, em *metrics.EventMetrics, typ string) {
key := metricName + "{" + strings.Join(labels, ",") + "}"
// Recognized metric
if pm := ps.metrics[metricName]; pm != nil {
// Recognized metric name and labels combination.
if pm.data[key] != nil {
pm.data[key].value = value
pm.data[key].timestamp = promTime(em.Timestamp)
return
}
pm.data[key] = &dataPoint{
value: value,
timestamp: promTime(em.Timestamp),
}
pm.dataKeys = append(pm.dataKeys, key)
} else {
// Newly discovered metric name.
if typ == "" {
typ = promType(em)
}
ps.metrics[metricName] = &promMetric{
typ: typ,
data: map[string]*dataPoint{
key: &dataPoint{
value: value,
timestamp: promTime(em.Timestamp),
},
},
dataKeys: []string{key},
}
ps.metricNames = append(ps.metricNames, metricName)
}
return
}
// checkLabelName finds a prometheus label name for an incoming label. If label
// is found to be invalid even after some basic conversions, a zero string is
// returned.
func (ps *PromSurfacer) checkLabelName(k string) string {
// Before checking with regex, see if this label name is
// already known. This block will be entered only once per
// label name.
if promLabel, ok := promLabelNames[k]; ok {
return promLabel
}
ps.l.Infof("Checking validity of new label: %s", k)
// We'll come here only once per label name.
// Prometheus doesn't support "-" in metric names.
labelName := strings.Replace(k, "-", "_", -1)
if !ps.labelNameRe.MatchString(labelName) {
// Explicitly store a zero string so that we don't check it again.
promLabelNames[k] = ""
ps.l.Warningf("Ignoring invalid prometheus label name: %s", k)
return ""
}
promLabelNames[k] = labelName
return labelName
}
// checkMetricName finds a prometheus metric name for an incoming metric. If metric
// is found to be invalid even after some basic conversions, a zero string is
// returned.
func (ps *PromSurfacer) checkMetricName(k string) string {
// Before checking with regex, see if this metric name is
// already known. This block will be entered only once per
// metric name.
if metricName, ok := promMetricNames[k]; ok {
return metricName
}
ps.l.Infof("Checking validity of new metric: %s", k)
// We'll come here only once per metric name.
// Prometheus doesn't support "-" in metric names.
metricName := strings.Replace(k, "-", "_", -1)
if !ps.metricNameRe.MatchString(metricName) {
// Explicitly store a zero string so that we don't check it again.
promMetricNames[k] = ""
ps.l.Warningf("Ignoring invalid prometheus metric name: %s", k)
return ""
}
promMetricNames[k] = metricName
return metricName
}
// record processes the incoming EventMetrics and updates the in-memory
// database.
//
// Since prometheus doesn't support certain metrics.Value types, we handle them
// differently.
//
// metrics.Map value type: We break Map values into multiple data keys, with
// each map key corresponding to a label in the data key.
// For example, "resp-code map:code 200:45 500:2" gets converted into:
// resp-code{code=200} 45
// resp-code{code=500} 2
//
// metrics.String value type: We convert string value type into a data key with
// val="value" label.
// For example, "version cloudprober-20170608-RC00" gets converted into:
// version{val=cloudprober-20170608-RC00} 1
func (ps *PromSurfacer) record(em *metrics.EventMetrics) {
var labels []string
for _, k := range em.LabelsKeys() {
if labelName := ps.checkLabelName(k); labelName != "" {
labels = append(labels, labelName+"=\""+em.Label(k)+"\"")
}
}
for _, metricName := range em.MetricsKeys() {
pMetricName := ps.checkMetricName(metricName)
if pMetricName == "" {
// No prometheus metric name found for this metric.
continue
}
val := em.Metric(metricName)
// Map values get expanded into metrics with extra label.
if mapVal, ok := val.(*metrics.Map); ok {
labelName := ps.checkLabelName(mapVal.MapName)
if labelName == "" {
continue
}
for _, k := range mapVal.Keys() {
labelsWithMap := append(labels, labelName+"=\""+k+"\"")
ps.recordMetric(pMetricName, labelsWithMap, mapVal.GetKey(k).String(), em, "")
}
continue
}
// Distribution values get expanded into metrics with extra label "le".
if distVal, ok := val.(*metrics.Distribution); ok {
d := distVal.Data()
var val int64
ps.recordMetric(pMetricName+"_sum", labels, strconv.FormatFloat(d.Sum, 'f', -1, 64), em, histogram)
ps.recordMetric(pMetricName+"_count", labels, strconv.FormatInt(d.Count, 10), em, histogram)
for i := range d.LowerBounds {
val += d.BucketCounts[i]
var lb string
if i == len(d.LowerBounds)-1 {
lb = "+Inf"
} else {
lb = strconv.FormatFloat(d.LowerBounds[i+1], 'f', -1, 64)
}
labelsWithBucket := append(labels, "le=\""+lb+"\"")
ps.recordMetric(pMetricName+"_bucket", labelsWithBucket, strconv.FormatInt(val, 10), em, histogram)
}
continue
}
// String values get converted into a label.
if _, ok := val.(metrics.String); ok {
newLabels := append(labels, "val="+val.String())
ps.recordMetric(pMetricName, newLabels, "1", em, "")
continue
}
// All other value types, mostly numerical types.
ps.recordMetric(pMetricName, labels, val.String(), em, "")
}
}
// writeData writes metrics data on w io.Writer
func (ps *PromSurfacer) writeData(w io.Writer) {
for _, name := range ps.metricNames {
pm := ps.metrics[name]
fmt.Fprintf(w, "#TYPE %s %s\n", name, pm.typ)
for _, k := range pm.dataKeys {
ps.dataWriter(w, pm, k)
}
}
}