-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
545 lines (454 loc) · 20.8 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
the License. A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file 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.
*/
// This file creates a local server when running from precompiled binaries or a Docker container, which will listen for
// Prometheus remote read and write requests. When running on AWS Lambda, the lambdaHandler function will listen for
// Prometheus remote read and write request sent to Amazon API Gateway.
package main
import (
"encoding/base64"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/go-kit/log"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/prometheus/prompb"
"github.com/alecthomas/kingpin/v2"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"timestream-prometheus-connector/errors"
"timestream-prometheus-connector/timestream"
)
const (
readHeader = "x-prometheus-remote-read-version"
writeHeader = "x-prometheus-remote-write-version"
basicAuthHeader = "authorization"
writeClientMaxRetries = 10
)
var (
// Store the initialization function calls and client retrieval calls to allow unit tests to mock the creation of real clients.
createWriteClient = func(timestreamClient *timestream.Client, logger log.Logger, configs *aws.Config, failOnLongMetricLabelName bool, failOnInvalidSample bool) {
timestreamClient.NewWriteClient(logger, configs, failOnLongMetricLabelName, failOnInvalidSample)
}
createQueryClient = func(timestreamClient *timestream.Client, logger log.Logger, configs *aws.Config, maxRetries int) {
configs.MaxRetries = aws.Int(maxRetries)
timestreamClient.NewQueryClient(logger, configs)
}
getWriteClient = func(timestreamClient *timestream.Client) writer { return timestreamClient.WriteClient() }
getQueryClient = func(timestreamClient *timestream.Client) reader { return timestreamClient.QueryClient() }
halt = os.Exit
)
type writer interface {
Write(req *prompb.WriteRequest, credentials *credentials.Credentials) error
Name() string
}
type reader interface {
Read(req *prompb.ReadRequest, credentials *credentials.Credentials) (*prompb.ReadResponse, error)
Name() string
}
type clientConfig struct {
region string
}
type connectionConfig struct {
clientConfig *clientConfig
defaultDatabase string
defaultTable string
enableLogging bool
failOnLongMetricLabelName bool
failOnInvalidSample bool
listenAddr string
promlogConfig promlog.Config
telemetryPath string
maxRetries int
certificate string
key string
}
func main() {
if len(os.Getenv("LAMBDA_TASK_ROOT")) != 0 {
// Start the AWS Lambda lambdaHandler if the connector is executing in an AWS Lambda environment.
lambda.Start(lambdaHandler)
} else {
var writers []writer
var readers []reader
cfg := parseFlags()
http.Handle(cfg.telemetryPath, promhttp.Handler())
logger := cfg.createLogger()
awsQueryConfigs := cfg.buildAWSConfig()
awsWriteConfigs := cfg.buildAWSConfig()
timestreamClient := timestream.NewBaseClient(cfg.defaultDatabase, cfg.defaultTable)
awsQueryConfigs.MaxRetries = aws.Int(cfg.maxRetries)
timestreamClient.NewQueryClient(logger, awsQueryConfigs)
awsWriteConfigs.MaxRetries = aws.Int(writeClientMaxRetries)
timestreamClient.NewWriteClient(logger, awsWriteConfigs, cfg.failOnLongMetricLabelName, cfg.failOnInvalidSample)
timestream.LogInfo(logger, fmt.Sprintf("Timestream connection is initialized (Database: %s, Table: %s, Region: %s)", cfg.defaultDatabase, cfg.defaultTable, cfg.clientConfig.region))
// Register TimestreamClient to Prometheus for it to scrape metrics
prometheus.MustRegister(timestreamClient)
writers = append(writers, timestreamClient.WriteClient())
readers = append(readers, timestreamClient.QueryClient())
timestream.LogInfo(logger, "The Prometheus Connector is now ready to begin serving ingestion and query requests.")
if err := serve(logger, cfg.listenAddr, writers, readers, cfg.certificate, cfg.key); err != nil {
timestream.LogError(logger, "Error occurred while listening for requests.", err)
os.Exit(1)
}
}
}
// lambdaHandler receives Prometheus read or write requests sent by API Gateway.
func lambdaHandler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if (len(os.Getenv(defaultDatabaseConfig.envFlag)) == 0 || len(os.Getenv(defaultTableConfig.envFlag)) == 0) {
return createErrorResponse(errors.NewMissingDestinationError().(*errors.MissingDestinationError).Message())
}
cfg, err := parseEnvironmentVariables()
if err != nil {
return createErrorResponse(err.Error())
}
logger := cfg.createLogger()
awsCredentials, ok := parseBasicAuth(req.Headers[basicAuthHeader])
if !ok {
return createErrorResponse(errors.NewParseBasicAuthHeaderError().(*errors.ParseBasicAuthHeaderError).Message())
}
awsConfigs := cfg.buildAWSConfig()
timestreamClient := timestream.NewBaseClient(cfg.defaultDatabase, cfg.defaultTable)
requestBody, err := base64.StdEncoding.DecodeString(req.Body)
if err != nil {
return createErrorResponse("Error occurred while decoding the API Gateway request body: " + err.Error())
}
reqBuf, err := snappy.Decode(nil, requestBody)
if err != nil {
return createErrorResponse("Error occurred while reading the write request sent by Prometheus: " + err.Error())
}
if len(req.Headers[writeHeader]) != 0 {
return handleWriteRequest(reqBuf, timestreamClient, awsConfigs, cfg, logger, awsCredentials)
} else if len(req.Headers[readHeader]) != 0 {
return handleReadRequest(reqBuf, timestreamClient, awsConfigs, cfg, logger, awsCredentials)
}
return createErrorResponse(errors.NewMissingHeaderError(readHeader, writeHeader).(*errors.MissingHeaderError).Message())
}
// handleWriteRequest handles a Prometheus write request.
func handleWriteRequest(reqBuf []byte, timestreamClient *timestream.Client, awsConfigs *aws.Config, cfg *connectionConfig, logger log.Logger, credentials *credentials.Credentials) (events.APIGatewayProxyResponse, error) {
var writeRequest prompb.WriteRequest
if err := proto.Unmarshal(reqBuf, &writeRequest); err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: "Error occurred while unmarshalling the decoded write request from Prometheus.",
}, nil
}
createWriteClient(timestreamClient, logger, awsConfigs, cfg.failOnLongMetricLabelName, cfg.failOnInvalidSample)
timestream.LogInfo(logger, fmt.Sprintf("Timestream write connection is initialized (Database: %s, Table: %s, Region: %s)", cfg.defaultDatabase, cfg.defaultTable, cfg.clientConfig.region))
if err := getWriteClient(timestreamClient).Write(&writeRequest, credentials); err != nil {
errorCode := http.StatusBadRequest
if requestError, ok := err.(awserr.RequestFailure); ok {
errorCode = requestError.StatusCode()
}
return events.APIGatewayProxyResponse{
StatusCode: errorCode,
Body: err.Error(),
}, nil
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
}, nil
}
// handleReadRequest handles a Prometheus read request.
func handleReadRequest(reqBuf []byte, timestreamClient *timestream.Client, awsConfigs *aws.Config, cfg *connectionConfig, logger log.Logger, credentials *credentials.Credentials) (events.APIGatewayProxyResponse, error) {
var readRequest prompb.ReadRequest
if err := proto.Unmarshal(reqBuf, &readRequest); err != nil {
timestream.LogError(logger, "Error occurred while unmarshalling the decoded read request from Prometheus.", err)
return createErrorResponse(err.Error())
}
createQueryClient(timestreamClient, logger, awsConfigs, cfg.maxRetries)
timestream.LogInfo(logger, fmt.Sprintf("Timestream query connection is initialized (Database: %s, Table: %s, Region: %s)", cfg.defaultDatabase, cfg.defaultTable, cfg.clientConfig.region))
response, err := getQueryClient(timestreamClient).Read(&readRequest, credentials)
if err != nil {
timestream.LogError(logger, "Error occurred while reading the data back from Timestream.", err)
if requestError, ok := err.(awserr.RequestFailure); ok {
return events.APIGatewayProxyResponse{
StatusCode: requestError.StatusCode(),
Body: err.Error(),
}, nil
}
return createErrorResponse(err.Error())
}
data, err := proto.Marshal(response)
if err != nil {
timestream.LogError(logger, "Error occurred while marshalling the Prometheus ReadResponse.", err)
return createErrorResponse(err.Error())
}
snappyEncodeData := snappy.Encode(nil, data)
base64EncodeData := make([]byte, base64.StdEncoding.EncodedLen(len(snappyEncodeData)))
base64.StdEncoding.Encode(base64EncodeData, snappyEncodeData)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
IsBase64Encoded: true,
Headers: map[string]string{
"Content-Type": "application/x-protobuf",
"Content-Encoding": "snappy",
},
Body: string(base64EncodeData),
}, nil
}
// parseBasicAuth parses the encoded HTTP Basic Authentication Header.
func parseBasicAuth(encoded string) (awsCredentials *credentials.Credentials, ok bool) {
auth := strings.SplitN(encoded, " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
return nil, false
}
credentialsBytes, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
return nil, false
}
credentialsSlice := strings.SplitN(string(credentialsBytes), ":", 2)
if len(credentialsSlice) != 2 {
return nil, false
}
return credentials.NewStaticCredentials(credentialsSlice[0], credentialsSlice[1], ""), true
}
// createLogger creates a new logger for the clients.
func (cfg *connectionConfig) createLogger() (logger log.Logger) {
if cfg.enableLogging {
logger = promlog.New(&cfg.promlogConfig)
} else {
logger = log.NewNopLogger()
}
timestream.LogInfo(logger, "timestream-prometheus-connector", "version", timestream.Version, "go version", timestream.GoVersion)
return logger
}
// parseBoolFromStrings parses the boolean configuration options from the strings in connectionConfig.
func (cfg *connectionConfig) parseBoolFromStrings(enableLogging, failOnLongMetricLabelName, failOnInvalidSample string) error {
var err error
cfg.enableLogging, err = strconv.ParseBool(enableLogging)
if err != nil {
timestreamError := errors.NewParseEnableLoggingError(enableLogging)
fmt.Println(timestreamError.Error())
return timestreamError
}
cfg.failOnLongMetricLabelName, err = strconv.ParseBool(failOnLongMetricLabelName)
if err != nil {
timestreamError := errors.NewParseMetricLabelError(failOnLongMetricLabelName)
fmt.Println(timestreamError.Error())
return timestreamError
}
cfg.failOnInvalidSample, err = strconv.ParseBool(failOnInvalidSample)
if err != nil {
timestreamError := errors.NewParseSampleOptionError(failOnInvalidSample)
fmt.Println(timestreamError.Error())
return timestreamError
}
return nil
}
// getOrDefault returns the value if the key exists as an environment variable; returns the default value otherwise.
func getOrDefault(key *configuration) string {
if value, exists := os.LookupEnv(key.envFlag); exists {
return value
}
return key.defaultValue
}
// parseEnvironmentVariables parses the connector configuration options from the AWS Lambda function's environment variables.
func parseEnvironmentVariables() (*connectionConfig, error) {
cfg := &connectionConfig{
clientConfig: &clientConfig{},
promlogConfig: promlog.Config{},
}
cfg.clientConfig.region = getOrDefault(regionConfig)
cfg.defaultDatabase = getOrDefault(defaultDatabaseConfig)
cfg.defaultTable = getOrDefault(defaultTableConfig)
var err error
err = cfg.parseBoolFromStrings(getOrDefault(enableLogConfig), getOrDefault(failOnLabelConfig), getOrDefault(failOnInvalidSampleConfig))
if err != nil {
return nil, err
}
retries := getOrDefault(maxRetriesConfig)
cfg.maxRetries, err = strconv.Atoi(retries)
if err != nil {
return nil, errors.NewParseRetriesError(retries)
}
cfg.promlogConfig = promlog.Config{Level: &promlog.AllowedLevel{}, Format: &promlog.AllowedFormat{}}
cfg.promlogConfig.Level.Set(getOrDefault(promlogLevelConfig))
cfg.promlogConfig.Format.Set(getOrDefault(promlogFormatConfig))
return cfg, nil
}
// parseFlags parses command line flags and return a connectionConfig pointer.
func parseFlags() *connectionConfig {
a := kingpin.New(filepath.Base(os.Args[0]), "Remote storage adapter")
a.HelpFlag.Short('h')
cfg := &connectionConfig{
clientConfig: &clientConfig{},
promlogConfig: promlog.Config{},
}
var enableLogging string
var failOnLongMetricLabelName string
var failOnInvalidSample string
a.Flag(enableLogConfig.flag, "Enables or disables logging in the connector. Default to 'true'.").Default(enableLogConfig.defaultValue).StringVar(&enableLogging)
a.Flag(regionConfig.flag, "The signing region for the Timestream service. Default to 'us-east-1'.").Default(regionConfig.defaultValue).StringVar(&cfg.clientConfig.region)
a.Flag(maxRetriesConfig.flag, "The maximum number of times the read request will be retried for failures. Default to 3.").Default(maxRetriesConfig.defaultValue).IntVar(&cfg.maxRetries)
a.Flag(defaultDatabaseConfig.flag, "The Prometheus label containing the database name for data ingestion.").Default(defaultDatabaseConfig.defaultValue).StringVar(&cfg.defaultDatabase)
a.Flag(defaultTableConfig.flag, "The Prometheus label containing the table name for data ingestion.").Default(defaultTableConfig.defaultValue).StringVar(&cfg.defaultTable)
a.Flag(listenAddrConfig.flag, "Address to listen on for web endpoints.").Default(listenAddrConfig.defaultValue).StringVar(&cfg.listenAddr)
a.Flag(telemetryPathConfig.flag, "Address to listen on for web endpoints.").Default(telemetryPathConfig.defaultValue).StringVar(&cfg.telemetryPath)
a.Flag(failOnLabelConfig.flag, "Enables or disables the option to halt the program immediately when a Prometheus Label name exceeds 256 bytes. Default to 'false'.").
Default(failOnLabelConfig.defaultValue).StringVar(&failOnLongMetricLabelName)
a.Flag(failOnInvalidSampleConfig.flag, "Enables or disables the option to halt the program immediately when a Sample contains a non-finite float value. Default to 'false'.").
Default(failOnInvalidSampleConfig.defaultValue).StringVar(&failOnInvalidSample)
a.Flag(certificateConfig.flag, "TLS server certificate file.").Default(certificateConfig.defaultValue).StringVar(&cfg.certificate)
a.Flag(keyConfig.flag, "TLS server private key file.").Default(keyConfig.defaultValue).StringVar(&cfg.key)
flag.AddFlags(a, &cfg.promlogConfig)
if _, err := a.Parse(os.Args[1:]); err != nil {
kingpin.Errorf("error occurred while parsing command line flags: '%s'", err)
os.Exit(1)
}
if err := cfg.parseBoolFromStrings(enableLogging, failOnLongMetricLabelName, failOnInvalidSample); err != nil {
os.Exit(1)
}
if cfg.defaultDatabase == "" {
kingpin.Errorf("The default database value must be set through the flag --default-database")
os.Exit(1)
}
if cfg.defaultTable == "" {
kingpin.Errorf("The default table value must be set through the flag --default-table")
os.Exit(1)
}
return cfg
}
// buildAWSConfig builds a aws.Config and return the pointer of the config.
func (cfg *connectionConfig) buildAWSConfig() *aws.Config {
clientConfig := cfg.clientConfig
awsConfig := &aws.Config{
Region: aws.String(clientConfig.region),
}
return awsConfig
}
// serve listens for requests and remote writes and reads to Timestream.
func serve(logger log.Logger, address string, writers []writer, readers []reader, certificate string, key string) error {
http.HandleFunc("/write", createWriteHandler(logger, writers))
http.HandleFunc("/read", createReadHandler(logger, readers))
server := http.Server{
Addr: address,
}
if certificate == "" || key == "" {
return server.ListenAndServe()
} else {
return server.ListenAndServeTLS(certificate, key)
}
}
// createWriteHandler creates a handler func(ResponseWriter, *Request) to handle Prometheus write requests.
func createWriteHandler(logger log.Logger, writers []writer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
awsCredentials, authOk := parseBasicAuth(r.Header.Get(basicAuthHeader))
if !authOk {
err := errors.NewParseBasicAuthHeaderError()
timestream.LogError(logger, "Error occurred while parsing the basic authentication header.", err)
http.Error(w, err.(*errors.ParseBasicAuthHeaderError).Message(), http.StatusBadRequest)
return
}
compressed, err := io.ReadAll(r.Body)
if err != nil {
timestream.LogError(logger, "Error occurred while reading the write request sent by Prometheus.", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
timestream.LogError(logger, "Error occurred while decoding the write request from Prometheus.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.WriteRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
timestream.LogError(logger, "Error occurred while unmarshalling the decoded write request from Prometheus.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := writers[0].Write(&req, awsCredentials); err != nil {
switch err := err.(type) {
case awserr.RequestFailure:
http.Error(w, err.Error(), err.StatusCode())
case *errors.SDKNonRequestError:
http.Error(w, err.Error(), http.StatusBadRequest)
case *errors.MissingDatabaseWithWriteError:
http.Error(w, err.Error(), http.StatusBadRequest)
case *errors.MissingTableWithWriteError:
http.Error(w, err.Error(), http.StatusBadRequest)
default:
// Others will halt the program.
halt(1)
}
}
}
}
// createReadHandler creates a handler func(ResponseWriter, *Request) to handle Prometheus read requests.
func createReadHandler(logger log.Logger, readers []reader) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
awsCredentials, authOk := parseBasicAuth(r.Header.Get(basicAuthHeader))
if !authOk {
err := errors.NewParseBasicAuthHeaderError()
timestream.LogError(logger, "Error occurred while parsing the basic authentication header.", err)
http.Error(w, err.(*errors.ParseBasicAuthHeaderError).Message(), http.StatusBadRequest)
return
}
compressed, err := io.ReadAll(r.Body)
if err != nil {
timestream.LogError(logger, "Error occurred while reading the read request sent by Prometheus.", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
timestream.LogError(logger, "Error occurred while decoding the read request from Prometheus.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req prompb.ReadRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
timestream.LogError(logger, "Error occurred while unmarshalling the decoded read request from Prometheus.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
response, err := readers[0].Read(&req, awsCredentials)
if err != nil {
timestream.LogError(logger, "Error occurred while reading the data back from Timestream.", err)
if requestError, ok := err.(awserr.RequestFailure); ok {
http.Error(w, err.Error(), requestError.StatusCode())
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data, err := proto.Marshal(response)
if err != nil {
timestream.LogError(logger, "Error occurred while marshalling the Prometheus ReadResponse.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Content-Encoding", "snappy")
if _, err := w.Write(snappy.Encode(nil, data)); err != nil {
timestream.LogError(logger, "Error occurred while writing the encoded ReadResponse to the connection as part of an HTTP reply.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
}
// createErrorResponse creates an events.APIGatewayProxyResponse with a 400 Status Code and the given error message.
func createErrorResponse(msg string) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: msg,
}, nil
}