-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.go
515 lines (461 loc) · 16.3 KB
/
server.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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/log/level"
"github.com/go-kit/kit/sd"
"github.com/go-kit/kit/sd/lb"
"github.com/go-kit/log"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promlog"
"github.com/prometheus/prometheus/prompb"
yaml "gopkg.in/yaml.v2"
)
const version = "0.5.2"
var (
listenAddress = flag.String("web.listen-address", ":9268", "Address to listen on for Prometheus requests.")
configFile = flag.String("config.file", "", "Path to the CrateDB endpoints configuration file.")
metricsExportPrefix = flag.String("metrics.export.prefix", "cratedb_prometheus_adapter_", "Prefix for exported CrateDB metrics.")
makeConfig = flag.Bool("config.make", false, "Print configuration file blueprint to stdout.")
printVersion = flag.Bool("version", false, "Print version information.")
writeDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: fmt.Sprintf("%swrite_latency_seconds", *metricsExportPrefix),
Help: "How long it took to respond to write requests.",
})
writeErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: fmt.Sprintf("%swrite_failed_total", *metricsExportPrefix),
Help: "How many write request returned errors.",
})
writeSamples = prometheus.NewSummary(prometheus.SummaryOpts{
Name: fmt.Sprintf("%swrite_timeseries_samples", *metricsExportPrefix),
Help: "How many samples each written timeseries has.",
})
writeCrateDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: fmt.Sprintf("%swrite_crate_latency_seconds", *metricsExportPrefix),
Help: "Latency for inserts to CrateDB.",
})
writeCrateErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: fmt.Sprintf("%swrite_crate_failed_total", *metricsExportPrefix),
Help: "How many inserts to CrateDB failed.",
})
readDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: fmt.Sprintf("%sread_latency_seconds", *metricsExportPrefix),
Help: "How long it took to respond to read requests.",
})
readErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: fmt.Sprintf("%sread_failed_total", *metricsExportPrefix),
Help: "How many read requests returned errors.",
})
readCrateDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: fmt.Sprintf("%sread_crate_latency_seconds", *metricsExportPrefix),
Help: "Latency for selects from CrateDB.",
})
readCrateErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: fmt.Sprintf("%sread_crate_failed_total", *metricsExportPrefix),
Help: "How many selects from CrateDB failed.",
})
readSamples = prometheus.NewSummary(prometheus.SummaryOpts{
Name: fmt.Sprintf("%sread_timeseries_samples", *metricsExportPrefix),
Help: "How many samples each returned timeseries has.",
})
)
// Module-wide `logger` variable, initialized by `setupLogging()`.
var logger log.Logger
func init() {
setupLogging()
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)
level.Info(logger).Log("msg", "Initialized CrateDB Prometheus Adapter", "version", version)
}
// Escaping for strings for Crate.io SQL.
var escaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"", "'", "\\'")
// Set up promlog logger.
func setupLogging() {
logLevel := promlog.AllowedLevel{}
logLevel.Set("debug")
logConfig := &promlog.Config{Level: &logLevel}
logger = promlog.New(logConfig)
}
// 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 CrateDB 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
}
// CrateDB 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, "valueRaw" 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 crateDbPrometheusAdapter struct {
ep endpoint.Endpoint
}
func (ca *crateDbPrometheusAdapter) runQuery(q *prompb.Query) ([]*prompb.TimeSeries, error) {
query, err := queryToSQL(q)
if err != nil {
return nil, err
}
level.Debug(logger).Log("msg", "runQuery", "stmt", query)
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 *crateDbPrometheusAdapter) handleRead(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(readDuration)
defer timer.ObserveDuration()
compressed, 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.StatusBadRequest)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
level.Error(logger).Log("msg", "Failed to decompress body", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.ReadRequest
if err := req.Unmarshal(reqBuf); err != nil {
level.Error(logger).Log("msg", "Failed to unmarshal body", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(req.Queries) != 1 {
level.Error(logger).Log("msg", "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 {
level.Warn(logger).Log("msg", "Failed to run select against CrateDB", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp := prompb.ReadResponse{
Results: []*prompb.QueryResult{
{Timeseries: result},
},
}
data, err := resp.Marshal()
if err != nil {
level.Error(logger).Log("msg", "Failed to marshal response", "err", err)
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 {
level.Error(logger).Log("msg", "Failed to compress response", "err", err)
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 *crateDbPrometheusAdapter) handleWrite(w http.ResponseWriter, r *http.Request) {
timer := prometheus.NewTimer(writeDuration)
defer timer.ObserveDuration()
compressed, 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
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
level.Error(logger).Log("msg", "Failed to decompress body", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.WriteRequest
if err := req.Unmarshal(reqBuf); err != nil {
level.Error(logger).Log("msg", "Failed to unmarshal body", "err", err)
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()
level.Error(logger).Log("msg", "Failed to write data to CrateDB", "err", err)
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"`
MaxConnections int `yaml:"max_connections"`
ReadPoolSize int `yaml:"read_pool_size_max"`
WritePoolSize int `yaml:"write_pool_size_max"`
ConnectTimeout int `yaml:"connect_timeout"`
ReadTimeout int `yaml:"read_timeout"`
WriteTimeout int `yaml:"write_timeout"`
EnableTLS bool `yaml:"enable_tls"`
AllowInsecureTLS bool `yaml:"allow_insecure_tls"`
}
func (ep *endpointConfig) toDSN() string {
// Convert endpointConfig data to libpq-compatible DSN-style connection string.
var params []string
if ep.Host != "" {
params = append(params, fmt.Sprintf("host=%s", ep.Host))
}
if ep.Port != 0 {
params = append(params, fmt.Sprintf("port=%v", ep.Port))
}
if ep.User != "" {
params = append(params, fmt.Sprintf("user=%s", ep.User))
}
if ep.Password != "" {
params = append(params, fmt.Sprintf("password=%s", ep.Password))
}
if ep.Schema != "" {
params = append(params, fmt.Sprintf("database=%s", ep.Schema))
}
if ep.ConnectTimeout != 0 {
params = append(params, fmt.Sprintf("connect_timeout=%v", ep.ConnectTimeout))
}
if ep.MaxConnections != 0 {
params = append(params, fmt.Sprintf("pool_max_conns=%v", ep.MaxConnections))
}
return strings.Join(params, " ")
}
type config struct {
Endpoints []endpointConfig `yaml:"cratedb_endpoints"`
}
func (c *config) toString() string {
var ep []string
for _, e := range c.Endpoints {
ep = append(ep, fmt.Sprintf("%s@%s:%d/%s", e.User, e.Host, e.Port, e.Schema))
}
return strings.Join(ep, ",")
}
func (c *config) toYaml() string {
data, err := yaml.Marshal(c)
if err != nil {
level.Error(logger).Log("msg", "Serialization to YAML failed", "err", err)
}
return string(data)
}
func loadConfig(filename string) (*config, error) {
conf := &config{}
if filename != "" {
level.Info(logger).Log("msg", "Reading configuration from file", "filename", filename)
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("reading configuration file %q failed: %v", filename, err)
} else {
if err = yaml.UnmarshalStrict(content, conf); err != nil {
return nil, fmt.Errorf("error unmarshaling YAML: %v", err)
}
}
} else {
level.Warn(logger).Log("msg", "No configuration file used, falling back to built-in configuration")
item := endpointConfig{}
conf.Endpoints = []endpointConfig{item}
}
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].ConnectTimeout == 0 {
conf.Endpoints[i].ConnectTimeout = 10
}
if conf.Endpoints[i].ReadTimeout == 0 {
conf.Endpoints[i].ReadTimeout = 5
}
if conf.Endpoints[i].WriteTimeout == 0 {
conf.Endpoints[i].WriteTimeout = 5
}
}
return conf, nil
}
func builtinConfig() *config {
blueprint, _ := loadConfig("")
return blueprint
}
func main() {
level.Info(logger).Log("msg", "Starting CrateDB Prometheus Adapter", "version", version)
flag.Parse()
if *printVersion {
fmt.Println(version)
return
}
if *makeConfig {
fmt.Println(builtinConfig().toYaml())
return
}
conf, err := loadConfig(*configFile)
if err != nil {
level.Error(logger).Log("msg", "Error loading configuration", "config", *configFile, "err", 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 := crateDbPrometheusAdapter{
ep: retry,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>CrateDB Prometheus Adapter</title></head>
<body>
<h1>CrateDB Prometheus Adapter</h1>
</body>
</html>`))
})
http.HandleFunc("/write", ca.handleWrite)
http.HandleFunc("/read", ca.handleRead)
http.Handle("/metrics", promhttp.Handler())
level.Info(logger).Log("msg", "Listening ...", "address", *listenAddress)
level.Info(logger).Log("msg", "Connecting ...", "endpoints", conf.toString())
listen_error := http.ListenAndServe(*listenAddress, nil)
level.Info(logger).Log("msg", "Final outcome", "err", listen_error)
}