-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
couchdb-exporter.go
269 lines (247 loc) · 8.32 KB
/
couchdb-exporter.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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli"
"github.com/urfave/cli/altsrc"
"k8s.io/klog"
"github.com/gesellix/couchdb-prometheus-exporter/fileutil"
"github.com/gesellix/couchdb-prometheus-exporter/lib"
)
type exporterConfigType struct {
listenAddress string
metricsEndpoint string
couchdbURI string
couchdbUsername string
couchdbPassword string
couchdbInsecure bool
databases string
databaseViews bool
databaseConcurrentRequests uint
schedulerJobs bool
}
type loggingConfigType struct {
toStderr bool // The -logtostderr flag.
alsoToStderr bool // The -alsologtostderr flag.
verbosity int // V logging level, the value of the -v flag/
stderrThreshold int // The -stderrthreshold flag.
logDir string // The -log_dir flag.
}
var exporterConfig exporterConfigType
var configFileFlagname = "config"
// custom exposed (but hidden) logging config flags
var loggingConfig loggingConfigType
var appFlags []cli.Flag
// TODO graceful migration to new parameter names
// 1) Warn, for deprecated parameters to be removed/renamed
// 2) Fail at startup, when deprecated parameters are used. Maybe allow override by explicit "i-know-what-i-am-doing"-parameter
// 3) Remove (ignore) deprecated parameters
func init() {
appFlags = []cli.Flag{
cli.StringFlag{
Name: configFileFlagname,
Usage: "Path to config file",
EnvVar: "CONFIG",
Hidden: false,
},
altsrc.NewStringFlag(cli.StringFlag{
Name: "telemetry.address",
Usage: "Address on which to expose metrics",
EnvVar: "TELEMETRY.ADDRESS",
Hidden: false,
Value: "localhost:9984",
Destination: &exporterConfig.listenAddress,
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "telemetry.endpoint",
Usage: "Path under which to expose metrics",
EnvVar: "TELEMETRY.ENDPOINT",
Hidden: false,
Value: "/metrics",
Destination: &exporterConfig.metricsEndpoint,
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "couchdb.uri",
Usage: "URI to the CouchDB instance",
EnvVar: "COUCHDB.URI",
Hidden: false,
Value: "http://localhost:5984",
Destination: &exporterConfig.couchdbURI,
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "couchdb.username",
Usage: "Basic auth username for the CouchDB instance",
EnvVar: "COUCHDB.USERNAME",
Hidden: false,
Value: "",
Destination: &exporterConfig.couchdbUsername,
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "couchdb.password",
Usage: "Basic auth password for the CouchDB instance",
EnvVar: "COUCHDB.PASSWORD",
Hidden: false,
Value: "",
Destination: &exporterConfig.couchdbPassword,
}),
// TODO doesn't print the default when showing the command help
altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "couchdb.insecure",
Usage: "Ignore server certificate if using https",
EnvVar: "COUCHDB.INSECURE",
Hidden: false,
Destination: &exporterConfig.couchdbInsecure,
}),
// TODO use cli.StringSliceFlag?
altsrc.NewStringFlag(cli.StringFlag{
Name: "databases",
Usage: fmt.Sprintf("Comma separated list of database names, or '%s'", lib.AllDbs),
EnvVar: "DATABASES",
Hidden: false,
Value: "",
Destination: &exporterConfig.databases,
}),
// TODO doesn't print the default when showing the command help
altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "databases.views",
Usage: "Collect view details of every observed database",
EnvVar: "DATABASES.VIEWS",
Hidden: false,
Destination: &exporterConfig.databaseViews,
}),
altsrc.NewUintFlag(cli.UintFlag{
Name: "database.concurrent.requests",
Usage: "maximum concurrent calls to CouchDB, or 0 for unlimited",
Value: 0,
Hidden: false,
Destination: &exporterConfig.databaseConcurrentRequests,
}),
// TODO doesn't print the default when showing the command help
altsrc.NewBoolFlag(cli.BoolFlag{
Name: "scheduler.jobs",
Usage: "Collect active replication jobs (CouchDB 2.x+ only)",
EnvVar: "SCHEDULER.JOBS",
Hidden: false,
Destination: &exporterConfig.schedulerJobs,
}),
altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "logtostderr",
Usage: "log to standard error instead of files",
Hidden: true,
Destination: &loggingConfig.toStderr,
}),
altsrc.NewBoolFlag(cli.BoolFlag{
Name: "alsologtostderr",
Usage: "log to standard error as well as files",
Hidden: true,
Destination: &loggingConfig.alsoToStderr,
}),
// TODO `v` clashed with urfave/cli's "version" shortcut `-v`.
altsrc.NewIntFlag(cli.IntFlag{
Name: "verbosity",
Usage: "log level for V logs",
Value: 0,
Hidden: true,
Destination: &loggingConfig.verbosity,
}),
altsrc.NewIntFlag(cli.IntFlag{
Name: "stderrthreshold",
Usage: "logs at or above this threshold go to stderr",
Value: 2,
Hidden: true,
Destination: &loggingConfig.stderrThreshold,
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "log_dir",
Usage: "If non-empty, write log files in this directory",
Hidden: true,
Destination: &loggingConfig.logDir,
}),
}
}
func main() {
var appAction = func(c *cli.Context) error {
var databases []string
if exporterConfig.databases != "" {
databases = strings.Split(exporterConfig.databases, ",")
}
exporter := lib.NewExporter(
exporterConfig.couchdbURI,
lib.BasicAuth{
Username: exporterConfig.couchdbUsername,
Password: exporterConfig.couchdbPassword},
lib.CollectorConfig{
Databases: databases,
CollectViews: exporterConfig.databaseViews,
CollectSchedulerJobs: exporterConfig.schedulerJobs,
ConcurrentRequests: exporterConfig.databaseConcurrentRequests,
},
exporterConfig.couchdbInsecure)
prometheus.MustRegister(exporter)
http.Handle(exporterConfig.metricsEndpoint, promhttp.Handler())
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "OK")
if err != nil {
klog.Error(err)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Please GET %s", exporterConfig.metricsEndpoint), http.StatusNotFound)
})
klog.Infof("Starting exporter at '%s' to read from CouchDB at '%s'", exporterConfig.listenAddress, exporterConfig.couchdbURI)
err := http.ListenAndServe(exporterConfig.listenAddress, nil)
if err != nil {
klog.Fatal(err)
}
return err
}
app := cli.NewApp()
app.Name = "CouchDB Prometheus Exporter"
//app.Usage = ""
app.Description = "CouchDB stats exporter for Prometheus"
//app.Version = ""
app.Flags = appFlags
app.Before = beforeApp(appFlags)
app.Action = appAction
defer klog.Flush()
err := app.Run(os.Args)
if err != nil {
klog.Fatal(err)
}
}
func beforeApp(appFlags []cli.Flag) cli.BeforeFunc {
return func(context *cli.Context) error {
// TODO decide on a preferred config file format, maybe support different ones.
inputSource := fileutil.NewPropertiesSourceFromFlagFunc(configFileFlagname)
//inputSource := altsrc.NewYamlSourceFromFlagFunc(configFileFlagname)
//inputSource := altsrc.NewTomlSourceFromFlagFunc(configFileFlagname)
if err := altsrc.InitInputSourceWithContext(appFlags, inputSource)(context); err != nil {
return err
}
return initKlogFlags(context)
}
}
func initKlogFlags(_ *cli.Context) error {
klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(klogFlags)
flags := map[string]string{
"logtostderr": strconv.FormatBool(loggingConfig.toStderr),
"alsologtostderr": strconv.FormatBool(loggingConfig.alsoToStderr),
"stderrthreshold": strconv.Itoa(loggingConfig.stderrThreshold),
"v": strconv.Itoa(loggingConfig.verbosity),
"log_dir": loggingConfig.logDir,
}
for k, v := range flags {
if err := klogFlags.Set(k, v); err != nil {
return err
}
}
klog.Infof("adopted logging config: %+v\n", loggingConfig)
return nil
}