forked from crate/cratedb-prometheus-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
421 lines (376 loc) · 12.8 KB
/
server.go
File metadata and controls
421 lines (376 loc) · 12.8 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"math"
"net/http"
"regexp"
"sort"
"strings"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/sd"
"github.com/go-kit/kit/sd/lb"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/prompb"
yaml "gopkg.in/yaml.v2"
)
const version = "0.3.0-dev"
var (
listenAddress = flag.String("web.listen-address", ":9268", "Address to listen on for Prometheus requests.")
configFile = flag.String("config.file", "config.yml", "Path to the CrateDB endpoints configuration file.")
print_version = flag.Bool("version", false, "Print version information.")
writeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "crate_adapter_write_latency_seconds",
Help: "How long it took us to respond to write requests.",
})
writeErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "crate_adapter_write_failed_total",
Help: "How many write request we returned errors for.",
})
writeSamples = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "crate_adapter_write_timeseries_samples",
Help: "How many samples each written timeseries has.",
})
writeCrateDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "crate_adapter_write_crate_latency_seconds",
Help: "Latency for inserts to Crate.",
})
writeCrateErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "crate_adapter_write_crate_failed_total",
Help: "How many inserts to Crate failed.",
})
readDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "crate_adapter_read_latency_seconds",
Help: "How long it took us to respond to read requests.",
})
readErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "crate_adapter_read_failed_total",
Help: "How many read requests we returned errors for.",
})
readCrateDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "crate_adapter_read_crate_latency_seconds",
Help: "Latency for selects from Crate.",
})
readCrateErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "crate_adapter_read_crate_failed_total",
Help: "How many selects from Crate failed.",
})
readSamples = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "crate_adapter_read_timeseries_samples",
Help: "How many samples each returned timeseries has.",
})
)
func init() {
prometheus.MustRegister(writeDuration)
prometheus.MustRegister(writeErrors)
prometheus.MustRegister(writeSamples)
prometheus.MustRegister(writeCrateDuration)
prometheus.MustRegister(writeCrateErrors)
prometheus.MustRegister(readDuration)
prometheus.MustRegister(readErrors)
prometheus.MustRegister(readSamples)
prometheus.MustRegister(readCrateDuration)
prometheus.MustRegister(readCrateErrors)
}
// Escaping for strings for Crate.io SQL.
var escaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"", "'", "\\'")
// Escape a labelname for use in SQL as a column name.
func escapeLabelName(s string) string {
return "labels['" + escaper.Replace(s) + "']"
}
// Escape a labelvalue for use in SQL as a string value.
func escapeLabelValue(s string) string {
return "'" + escaper.Replace(s) + "'"
}
// Convert a read query into a Crate SQL query.
func queryToSQL(q *prompb.Query) (string, error) {
selectors := make([]string, 0, len(q.Matchers)+2)
for _, m := range q.Matchers {
switch m.Type {
case prompb.LabelMatcher_EQ:
if m.Value == "" {
// Empty labels are recorded as NULL.
// In PromQL, empty labels and missing labels are the same thing.
selectors = append(selectors, fmt.Sprintf("(%s IS NULL)", escapeLabelName(m.Name)))
} else {
selectors = append(selectors, fmt.Sprintf("(%s = %s)", escapeLabelName(m.Name), escapeLabelValue(m.Value)))
}
case prompb.LabelMatcher_NEQ:
if m.Value == "" {
selectors = append(selectors, fmt.Sprintf("(%s IS NOT NULL)", escapeLabelName(m.Name)))
} else {
selectors = append(selectors, fmt.Sprintf("(%s != %s)", escapeLabelName(m.Name), escapeLabelValue(m.Value)))
}
case prompb.LabelMatcher_RE:
re := "(" + m.Value + ")"
matchesEmpty, err := regexp.MatchString(re, "")
if err != nil {
return "", err
}
// Crate regexes are not RE2, so there may be small semantic differences here.
if matchesEmpty {
selectors = append(selectors, fmt.Sprintf("(%s ~ %s OR %s IS NULL)", escapeLabelName(m.Name), escapeLabelValue(re), escapeLabelName(m.Name)))
} else {
selectors = append(selectors, fmt.Sprintf("(%s ~ %s)", escapeLabelName(m.Name), escapeLabelValue(re)))
}
case prompb.LabelMatcher_NRE:
re := "(" + m.Value + ")"
matchesEmpty, err := regexp.MatchString(re, "")
if err != nil {
return "", err
}
if matchesEmpty {
selectors = append(selectors, fmt.Sprintf("(%s !~ %s)", escapeLabelName(m.Name), escapeLabelValue(re)))
} else {
selectors = append(selectors, fmt.Sprintf("(%s !~ %s OR %s IS NULL)", escapeLabelName(m.Name), escapeLabelValue(re), escapeLabelName(m.Name)))
}
}
}
selectors = append(selectors, fmt.Sprintf("(timestamp <= %d)", q.EndTimestampMs))
selectors = append(selectors, fmt.Sprintf("(timestamp >= %d)", q.StartTimestampMs))
return fmt.Sprintf(`SELECT labels, labels_hash, timestamp, value, "value_raw" FROM metrics WHERE %s ORDER BY timestamp`, strings.Join(selectors, " AND ")), nil
}
func responseToTimeseries(data *crateReadResponse) []*prompb.TimeSeries {
timeseries := map[string]*prompb.TimeSeries{}
for _, row := range data.rows {
metric := model.Metric{}
for k, v := range row.labels {
metric[model.LabelName(k)] = model.LabelValue(v)
}
t := row.timestamp.UnixNano() / 1e6
v := math.Float64frombits(uint64(row.valueRaw))
ts, ok := timeseries[metric.String()]
if !ok {
ts = &prompb.TimeSeries{}
labelnames := make([]string, 0, len(metric))
for k := range metric {
labelnames = append(labelnames, string(k))
}
sort.Strings(labelnames) // Sort for unittests.
for _, k := range labelnames {
ts.Labels = append(ts.Labels, &prompb.Label{Name: string(k), Value: string(metric[model.LabelName(k)])})
}
timeseries[metric.String()] = ts
}
ts.Samples = append(ts.Samples, &prompb.Sample{Value: v, Timestamp: t})
}
names := make([]string, 0, len(timeseries))
for k := range timeseries {
names = append(names, k)
}
sort.Strings(names)
resp := make([]*prompb.TimeSeries, 0, len(timeseries))
for _, name := range names {
readSamples.Observe(float64(len(timeseries[name].Samples)))
resp = append(resp, timeseries[name])
}
return resp
}
type crateAdapter struct {
ep endpoint.Endpoint
}
func (ca *crateAdapter) runQuery(q *prompb.Query) ([]*prompb.TimeSeries, error) {
query, err := queryToSQL(q)
if err != nil {
return nil, err
}
request := &crateReadRequest{stmt: query}
timer := prometheus.NewTimer(readCrateDuration)
result, err := ca.ep(context.Background(), request)
timer.ObserveDuration()
if err != nil {
readCrateErrors.Inc()
return nil, err
}
return responseToTimeseries(result.(*crateReadResponse)), nil
}
func (ca *crateAdapter) handleRead(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(readDuration)
defer timer.ObserveDuration()
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
log.With("err", err).Error("Failed to read body.")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
log.With("err", err).Error("Failed to decompress body.")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.ReadRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
log.With("err", err).Error("Failed to unmarshal body.")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(req.Queries) != 1 {
log.Error("More than one query sent.")
http.Error(w, "Can only handle one query.", http.StatusBadRequest)
return
}
result, err := ca.runQuery(req.Queries[0])
if err != nil {
log.With("err", err).Error("Failed to run select against Crate.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp := prompb.ReadResponse{
Results: []*prompb.QueryResult{
{Timeseries: result},
},
}
data, err := proto.Marshal(&resp)
if err != nil {
log.With("err", err).Error("Failed to marshal response.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/x-protobuf")
if _, err := w.Write(snappy.Encode(nil, data)); err != nil {
log.With("err", err).Error("Failed to compress response.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func writesToCrateRequest(req *prompb.WriteRequest) *crateWriteRequest {
request := &crateWriteRequest{
rows: make([]*crateRow, 0, len(req.Timeseries)),
}
for _, ts := range req.Timeseries {
metric := make(model.Metric, len(ts.Labels))
for _, l := range ts.Labels {
metric[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
fp := metric.Fingerprint().String()
for _, s := range ts.Samples {
request.rows = append(request.rows, &crateRow{
labels: metric,
labelsHash: fp,
timestamp: time.Unix(0, s.Timestamp*1e6).UTC(),
value: s.Value,
// Crate.io can't handle full NaN values as required by Prometheus 2.0,
// so store the raw bits as an int64.
valueRaw: int64(math.Float64bits(s.Value)),
})
}
writeSamples.Observe(float64(len(ts.Samples)))
}
return request
}
func (ca *crateAdapter) handleWrite(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(writeDuration)
defer timer.ObserveDuration()
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
log.With("err", err).Error("Failed to read body")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
log.With("err", err).Error("Failed to decompress body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.WriteRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
log.With("err", err).Error("Failed to unmarshal body")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
request := writesToCrateRequest(&req)
writeTimer := prometheus.NewTimer(writeCrateDuration)
_, err = ca.ep(context.Background(), request)
writeTimer.ObserveDuration()
if err != nil {
writeCrateErrors.Inc()
log.With("err", err).Error("Failed to POST inserts to Crate.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
type endpointConfig struct {
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Schema string `yaml:"schema"`
EnableTLS bool `yaml:"enable_tls"`
AllowInsecureTLS bool `yaml:"allow_insecure_tls"`
MaxConnections int `yaml:"max_connections"`
}
type config struct {
Endpoints []endpointConfig `yaml:"crate_endpoints"`
}
func loadConfig(filename string) (*config, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading configuration file: %v", err)
}
conf := &config{}
if err = yaml.UnmarshalStrict(content, conf); err != nil {
return nil, fmt.Errorf("error unmarshaling YAML: %v", err)
}
if len(conf.Endpoints) == 0 {
return nil, fmt.Errorf("no CrateDB endpoints provided in configuration file")
}
for i := range conf.Endpoints {
if conf.Endpoints[i].Host == "" {
conf.Endpoints[i].Host = "localhost"
}
if conf.Endpoints[i].Port == 0 {
conf.Endpoints[i].Port = 5432
}
if conf.Endpoints[i].User == "" {
conf.Endpoints[i].User = "crate"
}
if conf.Endpoints[i].MaxConnections == 0 {
conf.Endpoints[i].MaxConnections = 5
}
}
return conf, nil
}
func main() {
flag.Parse()
if *print_version == true {
fmt.Println(version)
return
}
conf, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("Error loading configuration file %q: %v", *configFile, err)
}
subscriber := sd.FixedEndpointer{}
for _, epConf := range conf.Endpoints {
subscriber = append(subscriber, newCrateEndpoint(&epConf).endpoint())
}
balancer := lb.NewRoundRobin(subscriber)
// Try each endpoint once.
retry := lb.Retry(len(conf.Endpoints), 1*time.Minute, balancer)
ca := crateAdapter{
ep: retry,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Crate.io Prometheus Adapter</title></head>
<body>
<h1>Crate.io Prometheus Adapter</h1>
</body>
</html>`))
})
http.HandleFunc("/write", ca.handleWrite)
http.HandleFunc("/read", ca.handleRead)
http.Handle("/metrics", promhttp.Handler())
log.With("address", *listenAddress).Info("Listening")
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}