forked from dpattmann/prometheus-timestream-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (137 loc) · 4.54 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
// Copyright 2020 Dennis Pattmann <[email protected]>
//
// 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.
//
// @author Dennis Pattmann <[email protected]>
// @copyright 2020 Dennis Pattmann <[email protected]>
// @license Apache-2.0
package main
import (
"log"
"net/http"
"os"
"go.uber.org/zap"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/prometheus/prompb"
flag "github.com/spf13/pflag"
)
type config struct {
help bool
awsRegion string
databaseName string
listenAddr string
logLevel string
tableName string
telemetryPath string
tls bool
tlsCert string
tlsKey string
}
var (
cfg = new(config)
receivedSamples = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "received_samples_total",
Help: "Total number of received samples.",
},
)
sentSamples = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "sent_samples_total",
Help: "Total number of processed samples sent to remote storage.",
},
[]string{"remote"},
)
failedSamples = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "failed_samples_total",
Help: "Total number of processed samples which failed on send to remote storage.",
},
[]string{"remote"},
)
sentBatchDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "sent_batch_duration_seconds",
Help: "Duration of sample batch send calls to the remote storage.",
Buckets: prometheus.DefBuckets,
},
[]string{"remote"},
)
)
func init() {
prometheus.MustRegister(failedSamples)
prometheus.MustRegister(receivedSamples)
prometheus.MustRegister(sentBatchDuration)
prometheus.MustRegister(sentSamples)
flag.BoolVar(&cfg.help, "help", false, "")
flag.BoolVar(&cfg.tls, "tls", false, "")
flag.StringVar(&cfg.awsRegion, "awsRegion", "eu-central-1", "")
flag.StringVar(&cfg.databaseName, "databaseName", "prometheus-database", "")
flag.StringVar(&cfg.listenAddr, "listenAddr", ":9201", "")
flag.StringVar(&cfg.logLevel, "logLevel", "error", "")
flag.StringVar(&cfg.tableName, "tableName", "prometheus-table", "")
flag.StringVar(&cfg.telemetryPath, "telemetryPath", "/metric", "")
flag.StringVar(&cfg.tlsCert, "tlsCert", "tls.cert", "")
flag.StringVar(&cfg.tlsKey, "tlsKey", "tls.key", "")
flag.Parse()
}
func main() {
if cfg.help {
flag.PrintDefaults()
return
}
zapConfig := zap.NewProductionConfig()
zapConfig.DisableStacktrace = true
switch cfg.logLevel {
case "warning":
zapConfig.Level = zap.NewAtomicLevelAt(zap.WarnLevel)
case "error":
zapConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel)
case "info":
zapConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
case "debug":
zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
default:
os.Exit(1)
}
sugarLogger, err := zapConfig.Build()
if err != nil {
log.Fatalf("Error: %v", err)
}
defer sugarLogger.Sync() // flushes buffer, if any
sugar := sugarLogger.Sugar()
sugar.Debugf("Sending logs to timeseries database %s in AWS Region %s", cfg.databaseName, cfg.awsRegion)
timeStreamAdapter := newTimeStreamAdapter(sugar, cfg, nil, nil)
if err := serve(sugar, cfg.listenAddr, timeStreamAdapter); err != nil {
sugar.Errorw("Failed to listen", "addr", cfg.listenAddr, "err", err)
os.Exit(1)
}
}
type PrometheusRemoteStorageAdapter interface {
Write(records *prompb.WriteRequest) error
Read(request *prompb.ReadRequest) (*prompb.ReadResponse, error)
Name() string
}
func serve(logger *zap.SugaredLogger, addr string, storageAdapter PrometheusRemoteStorageAdapter) error {
http.Handle(cfg.telemetryPath, promhttp.Handler())
http.Handle("/write", writeHandler(logger, storageAdapter))
http.Handle("/read", readHandler(logger, storageAdapter))
http.Handle("/health", healthHandler(logger))
if cfg.tls {
logger.Debugf("Listening on port %s with cert %s and key %s", addr, cfg.tlsCert, cfg.tlsKey)
return http.ListenAndServeTLS(addr, cfg.tlsCert, cfg.tlsKey, nil)
}
logger.Debugf("Listening on port %s", addr)
return http.ListenAndServe(addr, nil)
}