@@ -14,6 +14,7 @@ import (
1414 "io"
1515 "log/slog"
1616 "os"
17+ "slices"
1718 "strconv"
1819 "strings"
1920 "sync"
@@ -50,6 +51,20 @@ func maskDsn(dsn string) string {
5051func NewExporter (logger * slog.Logger , m * MetricsConfiguration ) * Exporter {
5152 var databases []* Database
5253 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+
5368 for dbname , dbconfig := range m .Databases {
5469 logger .Info ("Initializing database" , "database" , dbname )
5570 database := NewDatabase (logger , dbname , dbconfig )
@@ -91,12 +106,25 @@ func NewExporter(logger *slog.Logger, m *MetricsConfiguration) *Exporter {
91106 MetricsConfiguration : m ,
92107 databases : databases ,
93108 lastScraped : map [string ]* time.Time {},
109+ allConstLabels : allConstLabels ,
94110 }
95111 e .metricsToScrape = e .DefaultMetrics ()
96112
97113 return e
98114}
99115
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+
100128// Describe describes all the metrics exported by the Oracle DB exporter.
101129func (e * Exporter ) Describe (ch chan <- * prometheus.Desc ) {
102130 // We cannot know in advance what metrics the exporter will generate
@@ -148,8 +176,8 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
148176 ch <- e .error
149177 e .scrapeErrors .Collect (ch )
150178 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 () )
153181 }
154182}
155183
@@ -203,7 +231,7 @@ func (e *Exporter) scheduledScrape(tick *time.Time) {
203231 metricCh <- e .error
204232 e .scrapeErrors .Collect (metricCh )
205233 for _ , db := range e .databases {
206- metricCh <- db .UpMetric ()
234+ metricCh <- db .UpMetric (e . constLabels () )
207235 }
208236 close (metricCh )
209237 wg .Wait ()
@@ -399,7 +427,8 @@ func (e *Exporter) scrapeGenericValues(d *Database, ch chan<- prometheus.Metric,
399427 metricsDesc map [string ]string , metricsType map [string ]string , metricsBuckets map [string ]map [string ]string ,
400428 fieldToAppend string , ignoreZeroResult bool , request string , queryTimeout time.Duration ) error {
401429 metricsCount := 0
402- constLabels := d .constLabels ()
430+ constLabels := d .constLabels (e .constLabels ())
431+ e .logger .Debug ("labels" , constLabels )
403432 genericParser := func (row map [string ]string ) error {
404433 // Construct labels value
405434 labelsValues := []string {}
0 commit comments