-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpostgres_exporter.go
More file actions
359 lines (302 loc) · 10.9 KB
/
postgres_exporter.go
File metadata and controls
359 lines (302 loc) · 10.9 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/tracelog"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/rnaveiras/postgres_exporter/collector"
)
const (
errorKey = "error"
exitCodeError = 1
// Server timeouts
readTimeout = 5 * time.Second
writeTimeout = 10 * time.Second
idleTimeout = 120 * time.Second
readHeaderTimeout = 5 * time.Second
// Global request timeout
// globalRequestTimeout = 30 * time.Second
// Graceful shutdown timeout
shutdownTimeout = 30 * time.Second
)
var handlerLock sync.Mutex
type flagConfig struct {
ListenAddress string `json:"listen_address"`
MetricsPath string `json:"metrics_path"`
DataSource string `json:"data_source"`
LogLevel string `json:"log_level"`
LogFormat string `json:"log_format"`
Pprof bool `json:"pprof"`
ExcludedDatabases []string `json:"excluded_databases"`
}
// LogValue implemnts LogValuer interface
func (f flagConfig) LogValue() slog.Value {
return slog.GroupValue(
slog.String("listen_address", f.ListenAddress),
slog.String("metrics_path", f.MetricsPath),
slog.String("log_level", f.LogLevel),
slog.String("log_format", f.LogFormat),
slog.Bool("pprof", f.Pprof),
slog.Any("exclude_databases", f.ExcludedDatabases),
)
}
func main() {
cfg := flagConfig{}
a := kingpin.New(filepath.Base(os.Args[0]), "The Postgres Exporter").UsageWriter(os.Stdout)
a.Version(version.Print("postgres_exporter"))
a.HelpFlag.Short('h')
a.Flag("web.listen-address", "Address on which to expose metrics and web interface.").
Default("0.0.0.0:9187").StringVar(&cfg.ListenAddress)
a.Flag("web.telemetry-path", "Path under which to expose metrics").
Default("/metrics").StringVar(&cfg.MetricsPath)
a.Flag("db.data-source", "libpq compatible connection string, e.g `user=postgres host=/var/run/postgresql`. Leave blank for libqp envs").
StringVar(&cfg.DataSource)
a.Flag("db.excluded-databases", "Repeat this flag for each database to exclude from monitoring").
Default("cloudsdqladmin", "rdsadmin").StringsVar(&cfg.ExcludedDatabases)
a.Flag("log.level", "Only log messages with the given severity or above. One of: [debug, info, warn, error]").
Default("info").EnumVar(&cfg.LogLevel, "debug", "info", "warn", "error")
a.Flag("log.format", "Output format of log messages. One of: [logfmt, json]").
Default("logfmt").EnumVar(&cfg.LogFormat, "logfmt", "json")
a.Flag("web.enabled-pprof", "").
Default("false").BoolVar(&cfg.Pprof)
_, err := a.Parse(os.Args[1:])
if err != nil {
//nolint:revive // Exiting anyway, so we can ignore
fmt.Fprintln(os.Stderr, fmt.Errorf("error parsing command line arguments: %w", err))
a.Usage(os.Args[1:])
os.Exit(exitCodeError)
}
// Setup log level
logLevel := new(slog.LevelVar)
logger, err := setupLogger(logLevel, cfg.LogFormat, cfg.LogLevel)
if err != nil {
//nolint:revive // Exiting anyway, so we can ignore
fmt.Fprintln(os.Stderr, err)
os.Exit(exitCodeError)
}
// Booting
logger.Info("starting postgres exporter", "version", version.Info())
logger.Info("", "build_context", version.BuildContext())
// Log cfg configuration
logger.Debug("cfg", "cfg", cfg)
// ParseConfig creates a ConnConfig from a connection string.
connConfig, err := pgx.ParseConfig(cfg.DataSource)
if err != nil {
logger.Error("error parse config",
slog.Any(errorKey, err))
os.Exit(exitCodeError)
}
logger.Info("connection string",
"user", connConfig.User,
"host", connConfig.Host,
"dbname", connConfig.Database,
"port", connConfig.Port,
)
// Configure the connection tracer for PostgreSQL query logging
// - Uses a custom SlogAdapter to integrate with our structured logging
// - LogLevel is set to None by default to avoid excessive logging
// This tracer can be used to debug database operations if needed
// by changing the LogLevel to tracelog.LogLevelDebug
connConfig.Tracer = &tracelog.TraceLog{
Logger: &SlogAdapter{logger: logger},
LogLevel: tracelog.LogLevelNone,
}
// Set PostgreSQL session parameters for this connection:
// - client_encoding: ensures proper character encoding (UTF8)
// - application_name: identifies this connection in pg_stat_activity
// making it easier to track exporter connections in the database
connConfig.RuntimeParams = map[string]string{
"client_encoding": "UTF8",
"application_name": "postgres_exporter",
}
// create a new servemux
mux := http.NewServeMux()
// register http endpoints
mux.Handle(cfg.MetricsPath, metricsHandler(logger, connConfig, cfg))
mux.Handle("/admin/loglevel", logLevelHandler(logger, logLevel))
mux.Handle("/", catchHandler(logger, cfg.MetricsPath))
// enable runtime profiling endpoints when pprof flag is set
if cfg.Pprof {
// Create a dedicated mux for pprof endpoints
debugMux := http.NewServeMux()
debugMux.HandleFunc("/debug/pprof/", pprof.Index)
debugMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
debugMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
debugMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
debugMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/pprof", debugMux)
}
logger = logger.With("component", "web")
logger.Info("start listening for connections",
"address", cfg.ListenAddress,
)
server := &http.Server{
Addr: cfg.ListenAddress,
Handler: mux,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
ReadHeaderTimeout: readHeaderTimeout,
BaseContext: func(_ net.Listener) context.Context { return context.Background() },
}
go func() {
err = server.ListenAndServe()
if err != nil {
logger.Error("failed listen and server",
slog.Any(errorKey, err))
}
}()
logger.Info("ready")
// Create a context that will be canceled on receiving a shutdown signal
// signal.NotifyContext handles signal setup and cleanup automatically
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Wait for interrupt signal
<-ctx.Done()
logger.Info("shutting down server - received signal",
errorKey, ctx.Err())
// Create a deadline to wait for current operations to complete
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Error("server forced to shutdown",
slog.Any(errorKey, err))
}
logger.Info("server gracefully stopped")
}
// catchHandler creates an HTTP handler that serves the index page of the exporter.
func catchHandler(logger *slog.Logger, metricsPath string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`<html>
<head><title>postgres Exporter</title></head>
<body>
<h1>Postgres Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
logger.Error("catch all handler",
slog.Any(errorKey, err))
}
})
}
// metricsHandler creates an HTTP handler that serves Prometheus metrics for PostgreSQL.
func metricsHandler(logger *slog.Logger, connConfig *pgx.ConnConfig, cfg flagConfig) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlerLock.Lock()
defer handlerLock.Unlock()
registry := prometheus.NewRegistry()
registry.MustRegister(versioncollector.NewCollector("postgres_exporter"))
registry.MustRegister(collector.NewExporter(r.Context(), logger, connConfig, cfg.ExcludedDatabases))
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry, // postgres_exporter metrics
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
Registry: registry,
MaxRequestsInFlight: 15,
}))
h.ServeHTTP(w, r)
})
}
// logLevelHandler creates an HTTP handler that enables dynamic log level
// adjustment
func logLevelHandler(logger *slog.Logger, logLevel *slog.LevelVar) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type logLevelJSON struct {
Level string `json:"level"`
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
// Return current logLevel
currentLevel := logLevel.Level().String()
if err := json.NewEncoder(w).Encode(logLevelJSON{Level: currentLevel}); err != nil {
http.Error(w, "error failed to encode JSON respose", http.StatusInternalServerError)
return
}
case http.MethodPatch:
var req logLevelJSON
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "error invalid request body", http.StatusBadRequest)
return
}
// Validate user input
validLevels := map[string]slog.Level{
"debug": slog.LevelDebug,
"info": slog.LevelInfo,
"warn": slog.LevelWarn,
"error": slog.LevelError,
}
level, ok := validLevels[strings.ToLower(req.Level)]
if !ok {
http.Error(w, "error invalid log level", http.StatusBadRequest)
return
}
logLevel.Set(level)
logger.Info("log level changed", "level", level)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
})
}
// setupLogger configures the logger,
func setupLogger(logLevelVar *slog.LevelVar, logFormat string, logLevel string) (*slog.Logger, error) {
// setup LogLevel
if err := setLogLevel(logLevelVar, logLevel); err != nil {
return nil, fmt.Errorf("error setting log level %w", err)
}
handlerOpts := slog.HandlerOptions{
Level: logLevelVar,
AddSource: false,
}
var handler slog.Handler
if logFormat == "logfmt" {
handler = slog.NewTextHandler(os.Stderr, &handlerOpts)
} else {
handler = slog.NewJSONHandler(os.Stderr, &handlerOpts)
}
logger := slog.New(handler)
slog.SetDefault(logger)
return logger, nil
}
// setLogLevel configures the log level from a string value.
// Valid levels are: debug, info, warn, error
func setLogLevel(logLevel *slog.LevelVar, level string) error {
switch strings.ToLower(level) {
case "debug":
logLevel.Set(slog.LevelDebug)
case "info":
logLevel.Set(slog.LevelInfo)
case "warn":
logLevel.Set(slog.LevelWarn)
case "error":
logLevel.Set(slog.LevelError)
default:
return fmt.Errorf("invalid log level %q, valid levels are: debug, info, warn, error", level)
}
return nil
}