This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
forked from iamseth/oracledb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (79 loc) · 3.83 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
package main
import (
"context"
"net/http"
"os"
_ "net/http/pprof"
"github.com/go-kit/log/level"
"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"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/observiq/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()
defaultFileMetrics = kingpin.Flag("default.metrics", "File with default metrics in a TOML file. (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 file. (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)
dsn := os.Getenv("DATA_SOURCE_NAME")
config := &collector.Config{
DSN: dsn,
MaxOpenConns: *maxOpenConns,
MaxIdleConns: *maxIdleConns,
CustomMetrics: *customMetrics,
QueryTimeout: *queryTimeout,
}
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(version.NewCollector("oracledb_exporter"))
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
}