-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
couchdb-exporter.go
292 lines (272 loc) · 9.15 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"fmt"
"log"
"log/slog"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"github.com/gesellix/couchdb-prometheus-exporter/v30/fileutil"
"github.com/gesellix/couchdb-prometheus-exporter/v30/lib"
)
var (
version = "dev"
commit = "none"
date = time.Now().Format(time.RFC3339)
)
type webConfigType struct {
listenAddress string
metricsEndpoint string
}
type exporterConfigType struct {
couchdbURI string
couchdbUsername string
couchdbPassword string
couchdbInsecure bool
scrapeInterval time.Duration
databases string
databaseViews bool
databaseConcurrentRequests uint
schedulerJobs bool
}
var exporterConfig exporterConfigType
var webConfig webConfigType
var configFileFlagname = "config"
var webConfigFile = ""
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 ini file that configures the CouchDB connection",
EnvVars: []string{"CONFIG"},
Hidden: false,
},
&cli.StringFlag{
Name: "web.config",
Usage: "Path to config yaml file that can enable TLS or authentication",
EnvVars: []string{"WEB_CONFIG"},
Hidden: false,
Value: "",
Destination: &webConfigFile,
},
altsrc.NewStringFlag(&cli.StringFlag{
Name: "telemetry.address",
Usage: "Address on which to expose metrics",
EnvVars: []string{"TELEMETRY.ADDRESS", "TELEMETRY_ADDRESS"},
Hidden: false,
Value: "localhost:9984",
Destination: &webConfig.listenAddress,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "telemetry.endpoint",
Usage: "Path under which to expose metrics",
EnvVars: []string{"TELEMETRY.ENDPOINT", "TELEMETRY_ENDPOINT"},
Hidden: false,
Value: "/metrics",
Destination: &webConfig.metricsEndpoint,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "couchdb.uri",
Usage: "URI to the CouchDB instance",
EnvVars: []string{"COUCHDB.URI", "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",
EnvVars: []string{"COUCHDB.USERNAME", "COUCHDB_USERNAME"},
Hidden: false,
Value: "",
Destination: &exporterConfig.couchdbUsername,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "couchdb.password",
Usage: "Basic auth password for the CouchDB instance",
EnvVars: []string{"COUCHDB.PASSWORD", "COUCHDB_PASSWORD"},
Hidden: false,
Value: "",
Destination: &exporterConfig.couchdbPassword,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "couchdb.insecure",
Usage: "Ignore server certificate if using https",
EnvVars: []string{"COUCHDB.INSECURE", "COUCHDB_INSECURE"},
Hidden: false,
Value: true,
Destination: &exporterConfig.couchdbInsecure,
}),
altsrc.NewDurationFlag(&cli.DurationFlag{
Name: "scrape.interval",
Usage: fmt.Sprintf("Duration between metrics collection from the CouchDB cluster. '0s' collects only on Prometheus scrapes"),
EnvVars: []string{"SCRAPE_INTERVAL"},
Hidden: false,
Value: 0 * time.Second,
Destination: &exporterConfig.scrapeInterval,
}),
// TODO use cli.StringSliceFlag?
altsrc.NewStringFlag(&cli.StringFlag{
Name: "databases",
Usage: fmt.Sprintf("Comma separated list of database names, or '%s'", lib.AllDbs),
EnvVars: []string{"DATABASES"},
Hidden: false,
Value: "",
Destination: &exporterConfig.databases,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "databases.views",
Usage: "Collect view details of every observed database",
EnvVars: []string{"DATABASES.VIEWS", "DATABASES_VIEWS"},
Hidden: false,
Value: true,
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,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "scheduler.jobs",
Usage: "Collect active replication jobs (CouchDB 2.x+ only)",
EnvVars: []string{"SCHEDULER.JOBS", "SCHEDULER_JOBS"},
Hidden: false,
Destination: &exporterConfig.schedulerJobs,
}),
}
}
func ofBool(i bool) *bool {
return &i
}
func ofString(i string) *string {
return &i
}
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
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{
ScrapeInterval: exporterConfig.scrapeInterval,
Databases: databases,
CollectViews: exporterConfig.databaseViews,
CollectSchedulerJobs: exporterConfig.schedulerJobs,
ConcurrentRequests: exporterConfig.databaseConcurrentRequests,
},
exporterConfig.couchdbInsecure)
prometheus.MustRegister(exporter)
http.Handle(webConfig.metricsEndpoint, promhttp.Handler())
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "OK")
if err != nil {
slog.Error(fmt.Sprintf("%v", err))
}
})
redirectToMetricsHandler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", webConfig.metricsEndpoint)
w.WriteHeader(http.StatusFound)
_, err := w.Write([]byte(`<html>
<head><title>CouchDB Prometheus Exporter</title></head>
<body>
<h1>CouchDB Prometheus Exporter</h1>
<p><a href="` + webConfig.metricsEndpoint + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
slog.Error(fmt.Sprintf("%v", err))
}
}
landingPageHandler, err := web.NewLandingPage(web.LandingConfig{
Name: "CouchDB Prometheus Exporter",
Description: "CouchDB metrics exporter for Prometheus",
Version: fmt.Sprintf("%s (%s, %s)", version, commit, date),
Links: []web.LandingLinks{
{
Address: "./metrics",
Text: "Metrics",
Description: "Metrics, as scraped by Prometheus",
},
{
Address: "https://github.com/gesellix/couchdb-prometheus-exporter",
Text: "Project Homepage",
Description: "Source repository and primary project home page",
},
{
Address: "https://couchdb.apache.org/",
Text: "CouchDB Homepage",
Description: "CouchDB home page",
},
{
Address: "https://prometheus.io/",
Text: "Prometheus Homepage",
Description: "Prometheus home page",
},
},
})
if err != nil {
log.Printf("error creating landing page %v\n", err)
http.HandleFunc("/", redirectToMetricsHandler)
} else {
http.HandleFunc("/", landingPageHandler.ServeHTTP)
}
slog.Info(fmt.Sprintf("Starting exporter version %s at '%s' to read from CouchDB at '%s'", version, webConfig.listenAddress, exporterConfig.couchdbURI))
server := &http.Server{Addr: webConfig.listenAddress}
flags := web.FlagConfig{
WebListenAddresses: &([]string{webConfig.listenAddress}),
WebSystemdSocket: ofBool(false),
WebConfigFile: ofString(webConfigFile),
}
if err := web.ListenAndServe(server, &flags, logger); err != nil {
slog.Error("Failed to start the server", "err", err)
os.Exit(1)
}
return nil
}
app := cli.NewApp()
app.Name = "CouchDB Prometheus Exporter"
//app.Usage = ""
app.Description = "CouchDB stats exporter for Prometheus"
app.Version = fmt.Sprintf("%s (%s, %s)", version, commit, date)
app.Flags = appFlags
app.Before = beforeApp(appFlags)
app.Action = appAction
//defer klog.Flush()
err := app.Run(os.Args)
if err != nil {
slog.Error(fmt.Sprintf("%v", 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 nil
}
}