/
main.go
424 lines (387 loc) · 12.5 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"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"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/promlog"
"github.com/prometheus/prometheus/prompb"
"golang.org/x/sync/errgroup"
)
const (
PROMETHEUS_MAXIMUM_POINTS = 11000
)
type adapterConfig struct {
listenAddr string
configFile string
storagePath string
}
func runQuery(ctx context.Context, indexer *Indexer, archiver *Archiver, q *prompb.Query, lookbackDelta time.Duration, logger log.Logger) ([]*prompb.TimeSeries, error) {
result := make(resultMap)
namespace := ""
period := ""
debugMode := false
debugIndexMode := false
originalJobLabel := ""
matchers := make([]*prompb.LabelMatcher, 0)
for _, m := range q.Matchers {
if m.Type == prompb.LabelMatcher_EQ && m.Name == "job" {
originalJobLabel = m.Value
continue
}
if m.Type == prompb.LabelMatcher_EQ && m.Name == "debug" {
debugMode = true
continue
}
if m.Type == prompb.LabelMatcher_EQ && m.Name == "debug_index" {
debugIndexMode = true
continue
}
if m.Type == prompb.LabelMatcher_EQ && m.Name == "Namespace" {
namespace = m.Value
}
if m.Type == prompb.LabelMatcher_EQ && m.Name == "Period" {
period = m.Value
}
matchers = append(matchers, m)
}
q.Matchers = matchers
// return label name/value list for query editor
if namespace == "" || q.Hints == nil {
m, err := fromLabelMatchers(q.Matchers)
if err != nil {
return nil, fmt.Errorf("failed to generate internal query")
}
matchedLabelsList, err := indexer.getMatchedLabels(ctx, m, q.StartTimestampMs, q.EndTimestampMs)
if err != nil {
return nil, fmt.Errorf("failed to generate internal query")
}
for i, matchedLabels := range matchedLabelsList {
ts := &prompb.TimeSeries{}
for _, label := range matchedLabels {
if label.Name == "MetricName" {
continue
}
ts.Labels = append(ts.Labels, prompb.Label{Name: label.Name, Value: label.Value})
}
ts.Labels = append(ts.Labels, prompb.Label{Name: "job", Value: originalJobLabel})
t := time.Unix(int64(q.EndTimestampMs/1000), int64(q.EndTimestampMs%1000*1000))
ts.Samples = append(ts.Samples, prompb.Sample{Value: 0, Timestamp: t.Unix() * 1000})
result[fmt.Sprint(i)] = ts
}
//level.Debug(logger).Log("msg", "namespace is required")
return result.slice(), nil
}
startTime := time.Unix(int64(q.Hints.StartMs/1000), int64(q.Hints.StartMs%1000*1000))
endTime := time.Unix(int64(q.Hints.EndMs/1000), int64(q.Hints.EndMs%1000*1000))
now := time.Now().UTC()
if endTime.After(now) {
q.Hints.EndMs = now.Unix() * 1000
endTime = time.Unix(int64(q.Hints.EndMs/1000), int64(q.Hints.EndMs%1000*1000))
}
maximumStep := int64(math.Ceil(float64(q.Hints.StepMs) / float64(1000)))
if maximumStep == 0 {
maximumStep = 1 // q.Hints.StepMs == 0 in some query...
}
if debugIndexMode {
result, err := indexer.Query(ctx, q, maximumStep, lookbackDelta)
if err != nil {
level.Error(logger).Log("err", err)
return nil, fmt.Errorf("failed to get time series from index")
}
return result.slice(), nil
}
// get time series from past(archived) time range
if period == "" && q.Hints.StartMs < q.Hints.EndMs && archiver.isArchived(startTime, []string{namespace}) {
if archiver.isExpired(startTime) && !indexer.isExpired(startTime, []string{namespace}) {
expiredTime := time.Now().UTC().Add(-archiver.retention)
if endTime.Before(expiredTime) {
expiredTime = endTime
}
baq := *q
baq.Hints = &prompb.ReadHints{}
*baq.Hints = *q.Hints
baq.Hints.EndMs = expiredTime.Unix() * 1000
offsetForStepMs := q.Hints.StartMs % q.Hints.StepMs
q.Hints.StartMs = (baq.Hints.EndMs + q.Hints.StepMs) - ((baq.Hints.EndMs + q.Hints.StepMs) % q.Hints.StepMs) + offsetForStepMs
if debugMode {
level.Info(logger).Log("msg", "querying for CloudWatch with index before archived period", "query", fmt.Sprintf("%+v", baq))
}
region, queries, err := getQueryWithIndex(ctx, &baq, indexer, maximumStep)
if err != nil {
level.Error(logger).Log("err", err)
return nil, fmt.Errorf("failed to generate internal query")
}
err = queryCloudWatch(ctx, region, queries, &baq, lookbackDelta, result)
if err != nil {
level.Error(logger).Log("err", err)
return nil, fmt.Errorf("failed to get time series from CloudWatch")
}
}
if q.Hints.StartMs < q.Hints.EndMs {
aq := *q
aq.Hints = &prompb.ReadHints{}
*aq.Hints = *q.Hints
if aq.Hints.EndMs > archiver.s.Timestamp[namespace]*1000 {
aq.Hints.EndMs = archiver.s.Timestamp[namespace] * 1000 // tsdb query maxt is inclusive
}
q.Hints.StartMs = aq.Hints.EndMs
if debugMode {
level.Info(logger).Log("msg", "querying for archive", "query", fmt.Sprintf("%+v", aq))
}
archivedResult, err := archiver.Query(ctx, &aq, maximumStep, lookbackDelta)
if err != nil {
level.Error(logger).Log("err", err)
return nil, fmt.Errorf("failed to get time series from archive")
}
if debugMode {
level.Info(logger).Log("msg", "dump archive query result", "result", fmt.Sprintf("%+v", archivedResult))
level.Info(logger).Log("msg", fmt.Sprintf("Get %d time series from archive.", len(archivedResult)))
}
result.append(archivedResult)
}
}
// get time series from recent time range
if q.Hints.StartMs < q.Hints.EndMs {
var region string
var queries []*cloudwatch.GetMetricStatisticsInput
var err error
if indexer.isExpired(endTime, []string{namespace}) {
if debugMode {
level.Info(logger).Log("msg", "querying for CloudWatch without index", "query", fmt.Sprintf("%+v", q))
}
region, queries, err = getQueryWithoutIndex(q, indexer, maximumStep)
} else {
if debugMode {
level.Info(logger).Log("msg", "querying for CloudWatch with index", "query", fmt.Sprintf("%+v", q))
}
region, queries, err = getQueryWithIndex(ctx, q, indexer, maximumStep)
}
if err != nil {
level.Error(logger).Log("err", err)
return nil, fmt.Errorf("failed to generate internal query")
}
if region != "" && len(queries) > 0 {
err = queryCloudWatch(ctx, region, queries, q, lookbackDelta, result)
if err != nil {
level.Error(logger).Log("err", err, "query", queries)
return nil, fmt.Errorf("failed to get time series from CloudWatch")
}
}
if debugMode {
level.Info(logger).Log("msg", "dump query result", "result", fmt.Sprintf("%+v", result))
}
}
if originalJobLabel != "" {
for _, ts := range result {
ts.Labels = append(ts.Labels, prompb.Label{Name: "job", Value: originalJobLabel})
}
}
if debugMode {
level.Info(logger).Log("msg", fmt.Sprintf("Returned %d time series.", len(result)))
}
return result.slice(), nil
}
func main() {
var cfg adapterConfig
flag.StringVar(&cfg.listenAddr, "web.listen-address", ":9415", "Address to listen on for web endpoints.")
flag.StringVar(&cfg.configFile, "config.file", "./cloudwatch_read_adapter.yml", "Configuration file path.")
flag.StringVar(&cfg.storagePath, "storage.tsdb.path", "./data", "Base path for metrics storage.")
flag.Parse()
logLevel := promlog.AllowedLevel{}
logLevel.Set("info")
format := promlog.AllowedFormat{}
format.Set("json")
config := promlog.Config{Level: &logLevel, Format: &format}
logger := promlog.New(&config)
readCfg, err := LoadConfig(cfg.configFile)
if err != nil {
level.Error(logger).Log("err", err)
panic(err)
}
if len(readCfg.Targets) == 0 {
level.Info(logger).Log("msg", "no targets")
os.Exit(0)
}
// set default region
region, err := GetDefaultRegion()
if err != nil {
level.Error(logger).Log("err", err)
panic(err)
}
if len(readCfg.Targets[0].Index.Region) == 0 {
readCfg.Targets[0].Index.Region = append(readCfg.Targets[0].Index.Region, region)
}
if len(readCfg.Targets[0].Archive.Region) == 0 {
readCfg.Targets[0].Archive.Region = append(readCfg.Targets[0].Archive.Region, region)
}
for _, n := range readCfg.Targets[0].Archive.Namespace {
found := false
for _, nn := range readCfg.Targets[0].Index.Namespace {
if n == nn {
found = true
}
}
if !found {
err := "archive target namespace should be indexed"
level.Error(logger).Log("err", err)
panic(err)
}
}
pctx, cancel := context.WithCancel(context.Background())
eg, ctx := errgroup.WithContext(pctx)
indexer, err := NewIndexer(readCfg.Targets[0].Index, cfg.storagePath, log.With(logger, "component", "indexer"))
if err != nil {
level.Error(logger).Log("err", err)
panic(err)
}
indexer.start(eg, ctx)
archiver, err := NewArchiver(readCfg.Targets[0].Archive, cfg.storagePath, indexer, log.With(logger, "component", "archiver"))
if err != nil {
level.Error(logger).Log("err", err)
panic(err)
}
archiver.start(eg, ctx)
srv := &http.Server{Addr: cfg.listenAddr}
http.HandleFunc("/metrics", func(rsp http.ResponseWriter, req *http.Request) {
httpError := func(rsp http.ResponseWriter, err error) {
rsp.Header().Del("Content-Encoding")
http.Error(
rsp,
"An error has occurred while serving metrics:\n\n"+err.Error(),
http.StatusInternalServerError,
)
}
mfs, err := prometheus.DefaultGatherer.Gather()
if err != nil {
httpError(rsp, err)
return
}
mfsi, err := indexer.registry.Gather()
if err != nil {
httpError(rsp, err)
return
}
mfsa, err := archiver.registry.Gather()
if err != nil {
httpError(rsp, err)
return
}
ln := "database"
lv1 := "index"
for _, mf := range mfsi {
for _, m := range mf.Metric {
m.Label = []*io_prometheus_client.LabelPair{}
for _, l := range m.Label {
m.Label = append(m.Label, &io_prometheus_client.LabelPair{Name: l.Name, Value: l.Value})
}
m.Label = append(m.Label, &io_prometheus_client.LabelPair{Name: &ln, Value: &lv1})
}
}
mfs = append(mfs, mfsi...)
lv2 := "archive"
for _, mf := range mfsa {
for _, m := range mf.Metric {
m.Label = []*io_prometheus_client.LabelPair{}
for _, l := range m.Label {
m.Label = append(m.Label, &io_prometheus_client.LabelPair{Name: l.Name, Value: l.Value})
}
m.Label = append(m.Label, &io_prometheus_client.LabelPair{Name: &ln, Value: &lv2})
}
}
mfs = append(mfs, mfsa...)
contentType := expfmt.Negotiate(req.Header)
header := rsp.Header()
header.Set("Content-Type", string(contentType))
w := io.Writer(rsp)
enc := expfmt.NewEncoder(w, contentType)
for _, mf := range mfs {
if err := enc.Encode(mf); err != nil {
httpError(rsp, err)
return
}
}
})
http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.ReadRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(req.Queries) != 1 {
http.Error(w, "Can only handle one query.", http.StatusBadRequest)
return
}
timeSeries, err := runQuery(ctx, indexer, archiver, req.Queries[0], readCfg.LookbackDelta, logger)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp := prompb.ReadResponse{
Results: []*prompb.QueryResult{
{Timeseries: timeSeries},
},
}
data, err := proto.Marshal(&resp)
if err != nil {
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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
defer func() {
signal.Stop(term)
cancel()
}()
go func() {
select {
case <-term:
level.Warn(logger).Log("msg", "Received SIGTERM, exiting gracefully...")
cancel()
if err := eg.Wait(); err != nil {
level.Error(logger).Log("err", err)
}
ctxHttp, _ := context.WithTimeout(context.Background(), 60*time.Second)
if err := srv.Shutdown(ctxHttp); err != nil {
level.Error(logger).Log("err", err)
}
case <-pctx.Done():
}
}()
level.Info(logger).Log("msg", "Listening on "+cfg.listenAddr)
if err := srv.ListenAndServe(); err != nil {
level.Error(logger).Log("err", err)
}
}