38
38
disableDefaultMetrics = kingpin .Flag ("disable-default-metrics" , "Do not include default metrics." ).Default ("false" ).OverrideDefaultFromEnvar ("PG_EXPORTER_DISABLE_DEFAULT_METRICS" ).Bool ()
39
39
queriesPath = kingpin .Flag ("extend.query-path" , "Path to custom queries to run." ).Default ("" ).OverrideDefaultFromEnvar ("PG_EXPORTER_EXTEND_QUERY_PATH" ).String ()
40
40
onlyDumpMaps = kingpin .Flag ("dumpmaps" , "Do not run, simply dump the maps." ).Bool ()
41
+ constantLabelsList = kingpin .Flag ("constantLabels" , "A list of label=value separated by comma(,)." ).Default ("" ).OverrideDefaultFromEnvar ("PG_EXPORTER_CONTANT_LABELS" ).String ()
41
42
)
42
43
43
44
// Metric name parts.
@@ -484,13 +485,15 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
484
485
thisMap := make (map [string ]MetricMap )
485
486
486
487
// Get the constant labels
487
- var constLabels []string
488
+ var variableLabels []string
488
489
for columnName , columnMapping := range mappings {
489
490
if columnMapping .usage == LABEL {
490
- constLabels = append (constLabels , columnName )
491
+ variableLabels = append (variableLabels , columnName )
491
492
}
492
493
}
493
494
495
+ constLabels := newConstLabels ()
496
+
494
497
for columnName , columnMapping := range mappings {
495
498
// Check column version compatibility for the current map
496
499
// Force to discard if not compatible.
@@ -522,23 +525,23 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
522
525
case COUNTER :
523
526
thisMap [columnName ] = MetricMap {
524
527
vtype : prometheus .CounterValue ,
525
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
528
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , variableLabels , constLabels ),
526
529
conversion : func (in interface {}) (float64 , bool ) {
527
530
return dbToFloat64 (in )
528
531
},
529
532
}
530
533
case GAUGE :
531
534
thisMap [columnName ] = MetricMap {
532
535
vtype : prometheus .GaugeValue ,
533
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
536
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , variableLabels , constLabels ),
534
537
conversion : func (in interface {}) (float64 , bool ) {
535
538
return dbToFloat64 (in )
536
539
},
537
540
}
538
541
case MAPPEDMETRIC :
539
542
thisMap [columnName ] = MetricMap {
540
543
vtype : prometheus .GaugeValue ,
541
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , constLabels , nil ),
544
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , columnName ), columnMapping .description , variableLabels , constLabels ),
542
545
conversion : func (in interface {}) (float64 , bool ) {
543
546
text , ok := in .(string )
544
547
if ! ok {
@@ -555,7 +558,7 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
555
558
case DURATION :
556
559
thisMap [columnName ] = MetricMap {
557
560
vtype : prometheus .GaugeValue ,
558
- desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s_milliseconds" , namespace , columnName ), columnMapping .description , constLabels , nil ),
561
+ desc : prometheus .NewDesc (fmt .Sprintf ("%s_%s_milliseconds" , namespace , columnName ), columnMapping .description , variableLabels , constLabels ),
559
562
conversion : func (in interface {}) (float64 , bool ) {
560
563
var durationString string
561
564
switch t := in .(type ) {
@@ -583,7 +586,7 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
583
586
}
584
587
}
585
588
586
- metricMap [namespace ] = MetricMapNamespace {constLabels , thisMap }
589
+ metricMap [namespace ] = MetricMapNamespace {variableLabels , thisMap }
587
590
}
588
591
589
592
return metricMap
@@ -711,33 +714,38 @@ func NewExporter(dsn string, disableDefaultMetrics bool, userQueriesPath string)
711
714
disableDefaultMetrics : disableDefaultMetrics ,
712
715
userQueriesPath : userQueriesPath ,
713
716
duration : prometheus .NewGauge (prometheus.GaugeOpts {
714
- Namespace : namespace ,
715
- Subsystem : exporter ,
716
- Name : "last_scrape_duration_seconds" ,
717
- Help : "Duration of the last scrape of metrics from PostgresSQL." ,
717
+ Namespace : namespace ,
718
+ Subsystem : exporter ,
719
+ Name : "last_scrape_duration_seconds" ,
720
+ Help : "Duration of the last scrape of metrics from PostgresSQL." ,
721
+ ConstLabels : newConstLabels (),
718
722
}),
719
723
totalScrapes : prometheus .NewCounter (prometheus.CounterOpts {
720
- Namespace : namespace ,
721
- Subsystem : exporter ,
722
- Name : "scrapes_total" ,
723
- Help : "Total number of times PostgresSQL was scraped for metrics." ,
724
+ Namespace : namespace ,
725
+ Subsystem : exporter ,
726
+ Name : "scrapes_total" ,
727
+ Help : "Total number of times PostgresSQL was scraped for metrics." ,
728
+ ConstLabels : newConstLabels (),
724
729
}),
725
730
error : prometheus .NewGauge (prometheus.GaugeOpts {
726
- Namespace : namespace ,
727
- Subsystem : exporter ,
728
- Name : "last_scrape_error" ,
729
- Help : "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success)." ,
731
+ Namespace : namespace ,
732
+ Subsystem : exporter ,
733
+ Name : "last_scrape_error" ,
734
+ Help : "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success)." ,
735
+ ConstLabels : newConstLabels (),
730
736
}),
731
737
psqlUp : prometheus .NewGauge (prometheus.GaugeOpts {
732
- Namespace : namespace ,
733
- Name : "up" ,
734
- Help : "Whether the last scrape of metrics from PostgreSQL was able to connect to the server (1 for yes, 0 for no)." ,
738
+ Namespace : namespace ,
739
+ Name : "up" ,
740
+ Help : "Whether the last scrape of metrics from PostgreSQL was able to connect to the server (1 for yes, 0 for no)." ,
741
+ ConstLabels : newConstLabels (),
735
742
}),
736
743
userQueriesError : prometheus .NewGaugeVec (prometheus.GaugeOpts {
737
- Namespace : namespace ,
738
- Subsystem : exporter ,
739
- Name : "user_queries_load_error" ,
740
- Help : "Whether the user queries file was loaded and parsed successfully (1 for error, 0 for success)." ,
744
+ Namespace : namespace ,
745
+ Subsystem : exporter ,
746
+ Name : "user_queries_load_error" ,
747
+ Help : "Whether the user queries file was loaded and parsed successfully (1 for error, 0 for success)." ,
748
+ ConstLabels : newConstLabels (),
741
749
}, []string {"filename" , "hashsum" }),
742
750
metricMap : nil ,
743
751
queryOverrides : nil ,
@@ -783,10 +791,32 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
783
791
e .userQueriesError .Collect (ch )
784
792
}
785
793
794
+ func newConstLabels () prometheus.Labels {
795
+ if constantLabelsList == nil || * constantLabelsList == "" {
796
+ return nil
797
+ }
798
+
799
+ var constLabels = make (prometheus.Labels )
800
+ parts := strings .Split (* constantLabelsList , "," )
801
+ for _ , p := range parts {
802
+ keyValue := strings .Split (strings .TrimSpace (p ), "=" )
803
+ if len (keyValue ) != 2 {
804
+ continue
805
+ }
806
+ key := strings .TrimSpace (keyValue [0 ])
807
+ value := strings .TrimSpace (keyValue [1 ])
808
+ if key == "" || value == "" {
809
+ continue
810
+ }
811
+ constLabels [key ] = value
812
+ }
813
+ return constLabels
814
+ }
815
+
786
816
func newDesc (subsystem , name , help string ) * prometheus.Desc {
787
817
return prometheus .NewDesc (
788
818
prometheus .BuildFQName (namespace , subsystem , name ),
789
- help , nil , nil ,
819
+ help , nil , newConstLabels () ,
790
820
)
791
821
}
792
822
@@ -872,7 +902,7 @@ func queryNamespaceMapping(ch chan<- prometheus.Metric, db *sql.DB, namespace st
872
902
} else {
873
903
// Unknown metric. Report as untyped if scan to float64 works, else note an error too.
874
904
metricLabel := fmt .Sprintf ("%s_%s" , namespace , columnName )
875
- desc := prometheus .NewDesc (metricLabel , fmt .Sprintf ("Unknown metric from %s" , namespace ), mapping .labels , nil )
905
+ desc := prometheus .NewDesc (metricLabel , fmt .Sprintf ("Unknown metric from %s" , namespace ), mapping .labels , newConstLabels () )
876
906
877
907
// Its not an error to fail here, since the values are
878
908
// unexpected anyway.
@@ -976,7 +1006,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
976
1006
977
1007
// Output the version as a special metric
978
1008
versionDesc := prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , staticLabelName ),
979
- "Version string as reported by postgres" , []string {"version" , "short_version" }, nil )
1009
+ "Version string as reported by postgres" , []string {"version" , "short_version" }, newConstLabels () )
980
1010
981
1011
ch <- prometheus .MustNewConstMetric (versionDesc ,
982
1012
prometheus .UntypedValue , 1 , versionString , semanticVersion .String ())
0 commit comments