@@ -452,7 +452,7 @@ func addQueries(content []byte, pgVersion semver.Version, server *Server) error
452
452
}
453
453
454
454
// Convert the loaded metric map into exporter representation
455
- partialExporterMap := makeDescMap (pgVersion , metricMaps )
455
+ partialExporterMap := makeDescMap (pgVersion , server . labels , metricMaps )
456
456
457
457
// Merge the two maps (which are now quite flatteend)
458
458
for k , v := range partialExporterMap {
@@ -480,15 +480,15 @@ func addQueries(content []byte, pgVersion semver.Version, server *Server) error
480
480
}
481
481
482
482
// Turn the MetricMap column mapping into a prometheus descriptor mapping.
483
- func makeDescMap (pgVersion semver.Version , metricMaps map [string ]map [string ]ColumnMapping ) map [string ]MetricMapNamespace {
483
+ func makeDescMap (pgVersion semver.Version , serverLabels prometheus. Labels , metricMaps map [string ]map [string ]ColumnMapping ) map [string ]MetricMapNamespace {
484
484
var metricMap = make (map [string ]MetricMapNamespace )
485
485
486
486
for namespace , mappings := range metricMaps {
487
487
thisMap := make (map [string ]MetricMap )
488
488
489
489
// Get the constant labels.
490
490
// Server label must be added to each metric.
491
- constLabels := []string { serverLabelName }
491
+ var constLabels []string
492
492
for columnName , columnMapping := range mappings {
493
493
if columnMapping .usage == LABEL {
494
494
constLabels = append (constLabels , columnName )
@@ -526,23 +526,23 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
526
526
case COUNTER :
527
527
thisMap [columnName ] = MetricMap {
528
528
vtype : prometheus .CounterValue ,
529
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
529
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , serverLabels ),
530
530
conversion : func (in interface {}) (float64 , bool ) {
531
531
return dbToFloat64 (in )
532
532
},
533
533
}
534
534
case GAUGE :
535
535
thisMap [columnName ] = MetricMap {
536
536
vtype : prometheus .GaugeValue ,
537
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
537
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , serverLabels ),
538
538
conversion : func (in interface {}) (float64 , bool ) {
539
539
return dbToFloat64 (in )
540
540
},
541
541
}
542
542
case MAPPEDMETRIC :
543
543
thisMap [columnName ] = MetricMap {
544
544
vtype : prometheus .GaugeValue ,
545
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
545
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , serverLabels ),
546
546
conversion : func (in interface {}) (float64 , bool ) {
547
547
text , ok := in .(string )
548
548
if ! ok {
@@ -559,7 +559,7 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
559
559
case DURATION :
560
560
thisMap [columnName ] = MetricMap {
561
561
vtype : prometheus .GaugeValue ,
562
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s_milliseconds" , namespace , columnName ), columnMapping .description , constLabels , nil ),
562
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s_milliseconds" , namespace , columnName ), columnMapping .description , constLabels , serverLabels ),
563
563
conversion : func (in interface {}) (float64 , bool ) {
564
564
var durationString string
565
565
switch t := in .(type ) {
@@ -725,8 +725,8 @@ func parseDSN(dsn string) (*url.URL, error) {
725
725
// Server describes a connection to Postgres.
726
726
// Also it contains metrics map and query overrides.
727
727
type Server struct {
728
- db * sql.DB
729
- fingerprint string
728
+ db * sql.DB
729
+ labels prometheus. Labels
730
730
731
731
// Last version used to calculate metric map. If mismatch on scrape,
732
732
// then maps are recalculated.
@@ -755,8 +755,10 @@ func NewServer(dsn string) (*Server, error) {
755
755
log .Infof ("Established new database connection to %q." , fingerprint )
756
756
757
757
return & Server {
758
- db : db ,
759
- fingerprint : fingerprint ,
758
+ db : db ,
759
+ labels : prometheus.Labels {
760
+ serverLabelName : fingerprint ,
761
+ },
760
762
}, nil
761
763
}
762
764
@@ -784,7 +786,7 @@ func (s *Server) Ping() error {
784
786
785
787
// String returns server's fingerprint.
786
788
func (s * Server ) String () string {
787
- return s .fingerprint
789
+ return s .labels [ serverLabelName ]
788
790
}
789
791
790
792
// Scrape loads metrics.
@@ -976,10 +978,10 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
976
978
e .userQueriesError .Collect (ch )
977
979
}
978
980
979
- func newDesc (subsystem , name , help string ) * prometheus.Desc {
981
+ func newDesc (subsystem , name , help string , labels prometheus. Labels ) * prometheus.Desc {
980
982
return prometheus .NewDesc (
981
983
prometheus .BuildFQName (namespace , subsystem , name ),
982
- help , [] string { serverLabelName }, nil ,
984
+ help , nil , labels ,
983
985
)
984
986
}
985
987
@@ -1040,9 +1042,8 @@ func queryNamespaceMapping(ch chan<- prometheus.Metric, server *Server, namespac
1040
1042
1041
1043
// Get the label values for this row.
1042
1044
labels := make ([]string , len (mapping .labels ))
1043
- labels [0 ] = server .String () // Server label must be added to each metric.
1044
- for idx := 1 ; idx < len (mapping .labels ); idx ++ {
1045
- labels [idx ], _ = dbToString (columnData [columnIdx [mapping.labels [idx ]]])
1045
+ for idx , label := range mapping .labels {
1046
+ labels [idx ], _ = dbToString (columnData [columnIdx [label ]])
1046
1047
}
1047
1048
1048
1049
// Loop over column names, and match to scan data. Unknown columns
@@ -1133,7 +1134,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server)
1133
1134
server .metricMap = make (map [string ]MetricMapNamespace )
1134
1135
server .queryOverrides = make (map [string ]string )
1135
1136
} else {
1136
- server .metricMap = makeDescMap (semanticVersion , e .builtinMetricMaps )
1137
+ server .metricMap = makeDescMap (semanticVersion , server . labels , e .builtinMetricMaps )
1137
1138
server .queryOverrides = makeQueryOverrideMap (semanticVersion , queryOverrides )
1138
1139
}
1139
1140
@@ -1166,10 +1167,10 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server)
1166
1167
1167
1168
// Output the version as a special metric
1168
1169
versionDesc := prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , staticLabelName ),
1169
- "Version string as reported by postgres" , []string {serverLabelName , "version" , "short_version" }, nil )
1170
+ "Version string as reported by postgres" , []string {"version" , "short_version" }, server . labels )
1170
1171
1171
1172
ch <- prometheus .MustNewConstMetric (versionDesc ,
1172
- prometheus .UntypedValue , 1 , server . String (), versionString , semanticVersion .String ())
1173
+ prometheus .UntypedValue , 1 , versionString , semanticVersion .String ())
1173
1174
return nil
1174
1175
}
1175
1176
0 commit comments