@@ -16,6 +16,7 @@ import (
16
16
_ "github.com/lib/pq"
17
17
"github.com/prometheus/client_golang/prometheus"
18
18
"github.com/prometheus/common/log"
19
+ "regexp"
19
20
)
20
21
21
22
var Version string = "0.0.1"
@@ -45,8 +46,15 @@ const (
45
46
namespace = "pg"
46
47
// Subsystems.
47
48
exporter = "exporter"
49
+ // Metric label used for static string data thats handy to send to Prometheus
50
+ // e.g. version
51
+ staticLabelName = "static"
48
52
)
49
53
54
+ // Highest version of Postgres we have explicit behavior for. This is the
55
+ // assumed default if the version string does match any known versions.
56
+ const HighestSupportedVersion = "9.6"
57
+
50
58
// landingPage contains the HTML served at '/'.
51
59
// TODO: Make this nicer and more informative.
52
60
var landingPage = []byte (`<html>
@@ -69,8 +77,25 @@ const (
69
77
DURATION ColumnUsage = iota // This column should be interpreted as a text duration (and converted to milliseconds)
70
78
)
71
79
72
- // Which metric mapping should be acquired using "SHOW" queries
73
- const SHOW_METRIC = "pg_runtime_variables"
80
+ // Special case matric mappings
81
+ const (
82
+ // Which metric mapping should be acquired using "SHOW" queries
83
+ SHOW_METRIC = "pg_runtime_variables"
84
+ )
85
+
86
+ // Regex used to get the "short-version" from the postgres version field.
87
+ var versionRegex = regexp .MustCompile (`^\w+ (\d+\.\d+)` )
88
+
89
+ // Parses the version of postgres into the short version string we can use to
90
+ // match behaviors.
91
+ func parseVersion (versionString string ) string {
92
+ submatches := versionRegex .FindStringSubmatch (versionString )
93
+ if len (submatches ) > 1 {
94
+ return submatches [1 ]
95
+ }
96
+ log .Debugln ("Could not parse postgres version regex:" , versionString )
97
+ return ""
98
+ }
74
99
75
100
// User-friendly representation of a prometheus descriptor map
76
101
type ColumnMapping struct {
@@ -580,6 +605,20 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
580
605
}
581
606
defer db .Close ()
582
607
608
+ log .Debugln ("Querying Postgres Version" )
609
+ versionRow := db .QueryRow ("SELECT version();" )
610
+ var versionString string
611
+ err = versionRow .Scan (& versionString )
612
+ if err != nil {
613
+ log .Errorln ("Error scanning version string:" , err )
614
+ e .error .Set (1 )
615
+ return
616
+ }
617
+ shortVersion := parseVersion (versionString )
618
+ // Output the version as a special metric
619
+ versionDesc := prometheus .NewDesc (fmt .Sprintf ("%s_%s" , namespace , staticLabelName ), "Version string as reported by postgres" , []string {"version" , "short_version" }, nil )
620
+ ch <- prometheus .MustNewConstMetric (versionDesc , prometheus .UntypedValue , 1 , versionString , shortVersion )
621
+
583
622
log .Debugln ("Querying SHOW variables" )
584
623
for _ , mapping := range e .variableMap {
585
624
for columnName , columnMapping := range mapping .columnMappings {
0 commit comments