-
Notifications
You must be signed in to change notification settings - Fork 237
/
main.go
130 lines (113 loc) · 4.56 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
package main
import (
"context"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
_ "github.com/sijms/go-ora/v2"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/go-kit/log/level"
// Required for debugging
// _ "net/http/pprof"
"github.com/iamseth/oracledb_exporter/collector"
)
var (
// Version will be set at build time.
Version = "0.0.0.dev"
metricPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics. (env: TELEMETRY_PATH)").Default(getEnv("TELEMETRY_PATH", "/metrics")).String()
dsn = kingpin.Flag("database.dsn",
"Connection string to a data source. (env: DATA_SOURCE_NAME)",
).Default(getEnv("DATA_SOURCE_NAME", "")).String()
dsnFile = kingpin.Flag("database.dsnFile",
"File to read a string to a data source from. (env: DATA_SOURCE_NAME_FILE)",
).Default(getEnv("DATA_SOURCE_NAME_FILE", "")).String()
defaultFileMetrics = kingpin.Flag(
"default.metrics",
"File with default metrics in a toml or yaml format. (env: DEFAULT_METRICS)",
).Default(getEnv("DEFAULT_METRICS", "default-metrics.toml")).String()
customMetrics = kingpin.Flag(
"custom.metrics",
"File that may contain various custom metrics in a toml or yaml format. (env: CUSTOM_METRICS)",
).Default(getEnv("CUSTOM_METRICS", "")).String()
queryTimeout = kingpin.Flag(
"query.timeout",
"Query timeout (in seconds). (env: QUERY_TIMEOUT)",
).Default(getEnv("QUERY_TIMEOUT", "5")).Int()
maxIdleConns = kingpin.Flag(
"database.maxIdleConns",
"Number of maximum idle connections in the connection pool. (env: DATABASE_MAXIDLECONNS)",
).Default(getEnv("DATABASE_MAXIDLECONNS", "0")).Int()
maxOpenConns = kingpin.Flag(
"database.maxOpenConns",
"Number of maximum open connections in the connection pool. (env: DATABASE_MAXOPENCONNS)",
).Default(getEnv("DATABASE_MAXOPENCONNS", "10")).Int()
scrapeInterval = kingpin.Flag(
"scrape.interval",
"Interval between each scrape. Default is to scrape on collect requests",
).Default("0s").Duration()
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9161")
)
func main() {
promLogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promLogConfig)
kingpin.HelpFlag.Short('\n')
kingpin.Version(version.Print("oracledb_exporter"))
kingpin.Parse()
logger := promlog.New(promLogConfig)
if dsnFile != nil && *dsnFile != "" {
dsnFileContent, err := os.ReadFile(*dsnFile)
if err != nil {
level.Error(logger).Log("msg", "Unable to read DATA_SOURCE_NAME_FILE", "file", dsnFile, "error", err)
os.Exit(1)
}
*dsn = string(dsnFileContent)
}
config := &collector.Config{
DSN: *dsn,
MaxOpenConns: *maxOpenConns,
MaxIdleConns: *maxIdleConns,
CustomMetrics: *customMetrics,
QueryTimeout: *queryTimeout,
DefaultMetricsFile: *defaultFileMetrics,
}
exporter, err := collector.NewExporter(logger, config)
if err != nil {
level.Error(logger).Log("unable to connect to DB", err)
}
if *scrapeInterval != 0 {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go exporter.RunScheduledScrapes(ctx, *scrapeInterval)
}
prometheus.MustRegister(exporter)
prometheus.MustRegister(collectors.NewBuildInfoCollector())
level.Info(logger).Log("msg", "Starting oracledb_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build", version.BuildContext())
level.Info(logger).Log("msg", "Collect from: ", "metricPath", *metricPath)
opts := promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
}
http.Handle(*metricPath, promhttp.HandlerFor(prometheus.DefaultGatherer, opts))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<html><head><title>Oracle DB Exporter " + Version + "</title></head><body><h1>Oracle DB Exporter " + Version + "</h1><p><a href='" + *metricPath + "'>Metrics</a></p></body></html>"))
})
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, logger); err != nil {
level.Error(logger).Log("msg", "Listening error", "reason", err)
os.Exit(1)
}
}
// getEnv returns the value of an environment variable, or returns the provided fallback value
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}