@@ -14,6 +14,7 @@ import (
14
14
"io"
15
15
"log/slog"
16
16
"os"
17
+ "slices"
17
18
"strconv"
18
19
"strings"
19
20
"sync"
@@ -50,6 +51,20 @@ func maskDsn(dsn string) string {
50
51
func NewExporter (logger * slog.Logger , m * MetricsConfiguration ) * Exporter {
51
52
var databases []* Database
52
53
wg := & sync.WaitGroup {}
54
+
55
+ var allConstLabels []string
56
+ // All the metrics of the same name need to have the same set of labels
57
+ // If a label is set for a particular database, it must be included also
58
+ // in the same metrics collected from other databases. It will just be
59
+ // set to a blank value.
60
+ for _ , dbconfig := range m .Databases {
61
+ for label , _ := range dbconfig .Labels {
62
+ if (! slices .Contains (allConstLabels , label )) {
63
+ allConstLabels = append (allConstLabels , label )
64
+ }
65
+ }
66
+ }
67
+
53
68
for dbname , dbconfig := range m .Databases {
54
69
logger .Info ("Initializing database" , "database" , dbname )
55
70
database := NewDatabase (logger , dbname , dbconfig )
@@ -91,12 +106,25 @@ func NewExporter(logger *slog.Logger, m *MetricsConfiguration) *Exporter {
91
106
MetricsConfiguration : m ,
92
107
databases : databases ,
93
108
lastScraped : map [string ]* time.Time {},
109
+ allConstLabels : allConstLabels ,
94
110
}
95
111
e .metricsToScrape = e .DefaultMetrics ()
96
112
97
113
return e
98
114
}
99
115
116
+ func (e * Exporter ) constLabels () map [string ]string {
117
+ // All the metrics of the same name need to have the same labels
118
+ // If a label is set for a particular database, it must be included also
119
+ // in the same metrics collected from other databases. It will just be
120
+ // set to a blank value.
121
+ labels := map [string ]string {}
122
+ for _ , label := range e .allConstLabels {
123
+ labels [label ] = ""
124
+ }
125
+ return labels
126
+ }
127
+
100
128
// Describe describes all the metrics exported by the Oracle DB exporter.
101
129
func (e * Exporter ) Describe (ch chan <- * prometheus.Desc ) {
102
130
// We cannot know in advance what metrics the exporter will generate
@@ -148,8 +176,8 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
148
176
ch <- e .error
149
177
e .scrapeErrors .Collect (ch )
150
178
for _ , db := range e .databases {
151
- ch <- db .DBTypeMetric ()
152
- ch <- db .UpMetric ()
179
+ ch <- db .DBTypeMetric (e . constLabels () )
180
+ ch <- db .UpMetric (e . constLabels () )
153
181
}
154
182
}
155
183
@@ -203,7 +231,7 @@ func (e *Exporter) scheduledScrape(tick *time.Time) {
203
231
metricCh <- e .error
204
232
e .scrapeErrors .Collect (metricCh )
205
233
for _ , db := range e .databases {
206
- metricCh <- db .UpMetric ()
234
+ metricCh <- db .UpMetric (e . constLabels () )
207
235
}
208
236
close (metricCh )
209
237
wg .Wait ()
@@ -399,7 +427,8 @@ func (e *Exporter) scrapeGenericValues(d *Database, ch chan<- prometheus.Metric,
399
427
metricsDesc map [string ]string , metricsType map [string ]string , metricsBuckets map [string ]map [string ]string ,
400
428
fieldToAppend string , ignoreZeroResult bool , request string , queryTimeout time.Duration ) error {
401
429
metricsCount := 0
402
- constLabels := d .constLabels ()
430
+ constLabels := d .constLabels (e .constLabels ())
431
+ e .logger .Debug ("labels" , constLabels )
403
432
genericParser := func (row map [string ]string ) error {
404
433
// Construct labels value
405
434
labelsValues := []string {}
0 commit comments