forked from iamseth/oracledb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
245 lines (218 loc) · 6.91 KB
/
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
package main
import (
"context"
"database/sql"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
// Exporter collects Oracle DB metrics. It implements prometheus.Collector.
type Exporter struct {
dsn string
duration, error prometheus.Gauge
totalScrapes prometheus.Counter
scrapeErrors *prometheus.CounterVec
scrapeResults []prometheus.Metric
up prometheus.Gauge
db *sql.DB
logger *logrus.Logger
}
// NewExporter returns a new Oracle DB exporter for the provided DSN.
func NewExporter(dsn string, logger *logrus.Logger) *Exporter {
db, _ := connect(dsn, logger)
return &Exporter{
dsn: dsn,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from Oracle DB.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times Oracle DB was scraped for metrics.",
}),
scrapeErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrape_errors_total",
Help: "Total number of times an error occured scraping a Oracle database.",
}, []string{"collector"}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from Oracle DB resulted in an error (1 for error, 0 for success).",
}),
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Whether the Oracle database server is up.",
}),
db: db,
logger: logger,
}
}
// Describe describes all the metrics exported by the Oracle DB exporter.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// We cannot know in advance what metrics the exporter will generate
// So we use the poor man's describe method: Run a collect
// and send the descriptors of all the collected metrics. The problem
// here is that we need to connect to the Oracle DB. If it is currently
// unavailable, the descriptors will be incomplete. Since this is a
// stand-alone exporter and not used as a library within other code
// implementing additional metrics, the worst that can happen is that we
// don't detect inconsistent metrics created by this exporter
// itself. Also, a change in the monitored Oracle instance may change the
// exported metrics during the runtime of the exporter.
defer panicLogger()
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
defer panicLogger()
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
defer panicLogger()
if *scrapeInterval == 0 { // if we are to scrape when the request is made
e.scrape(ch)
} else {
scrapeResults := e.scrapeResults // There is a risk that e.scrapeResults will be replaced while we traverse this look. This should mitigate that risk
for idx := range scrapeResults {
ch <- scrapeResults[idx]
}
}
ch <- e.duration
ch <- e.totalScrapes
ch <- e.error
e.scrapeErrors.Collect(ch)
ch <- e.up
}
func (e *Exporter) runScheduledScrapes() {
defer panicLogger()
if *scrapeInterval == 0 {
return // Do nothing as scrapes will be done on Collect requests
}
ticker := time.NewTicker(*scrapeInterval)
defer ticker.Stop()
for {
metricCh := make(chan prometheus.Metric, 5)
go func() {
defer panicLogger()
scrapeResults := []prometheus.Metric{}
for {
scrapeResult, more := <-metricCh
if more {
scrapeResults = append(scrapeResults, scrapeResult)
} else {
e.scrapeResults = scrapeResults
return
}
}
}()
e.scrape(metricCh)
close(metricCh)
<-ticker.C
}
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
defer panicLogger()
e.totalScrapes.Inc()
var err error
defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
if err == nil {
e.error.Set(0)
} else {
e.error.Set(1)
}
}(time.Now())
if e.db == nil {
e.logger.Info("Reconnecting to DB")
db, err := connect(e.dsn, e.logger)
if err != nil {
e.logger.Error("Reconnectiong to DB Error: ", err)
e.up.Set(0)
return
}
e.db = db
}
if err := ping(context.Background(), e.db); err != nil {
e.logger.Info("Reconnecting to DB")
db, err := connect(e.dsn, e.logger)
if err != nil {
e.logger.Error("Reconnectiong to DB Error: ", err)
e.up.Set(0)
return
}
e.db = db
}
if err := ping(context.Background(), e.db); err != nil {
e.logger.Error("Error pinging oracle: ", err)
//e.db.Close()
e.up.Set(0)
return
} else {
e.logger.Debug("Successfully pinged Oracle database: ", maskDsn(e.dsn))
e.up.Set(1)
}
if checkIfMetricsChanged(e.logger) {
reloadMetrics(e.logger)
}
wg := sync.WaitGroup{}
metricsLock.RLock()
defer metricsLock.RUnlock()
for _, metric := range metricsToScrap.Metric {
wg.Add(1)
metric := metric //https://golang.org/doc/faq#closures_and_goroutines
go func() {
defer panicLogger()
defer wg.Done()
e.logger.Debug("About to scrape metric: ")
e.logger.Debug("- Metric MetricsDesc: ", metric.MetricsDesc)
e.logger.Debug("- Metric Context: ", metric.Context)
e.logger.Debug("- Metric MetricsType: ", metric.MetricsType)
e.logger.Debug("- Metric MetricsBuckets: ", metric.MetricsBuckets, "(Ignored unless Histogram type)")
e.logger.Debug("- Metric Labels: ", metric.Labels)
e.logger.Debug("- Metric FieldToAppend: ", metric.FieldToAppend)
e.logger.Debug("- Metric IgnoreZeroResult: ", metric.IgnoreZeroResult)
e.logger.Debug("- Metric Request: ", metric.Request)
if len(metric.Request) == 0 {
e.logger.Error("Error scraping for ", metric.MetricsDesc, ". Did you forget to define request in your toml file?")
return
}
if len(metric.MetricsDesc) == 0 {
e.logger.Error("Error scraping for query", metric.Request, ". Did you forget to define metricsdesc in your toml file?")
return
}
for column, metricType := range metric.MetricsType {
if metricType == "histogram" {
_, ok := metric.MetricsBuckets[column]
if !ok {
e.logger.Error("Unable to find MetricsBuckets configuration key for metric. (metric=" + column + ")")
return
}
}
}
scrapeStart := time.Now()
if err = ScrapeMetric(e.db, ch, metric, e.logger); err != nil {
e.logger.Error("Error scraping for", metric.Context, "_", metric.MetricsDesc, time.Since(scrapeStart), ": ", err)
e.scrapeErrors.WithLabelValues(metric.Context).Inc()
} else {
e.logger.Debug("Successfully scraped metric: ", metric.Context, metric.MetricsDesc, time.Since(scrapeStart))
}
}()
}
wg.Wait()
}