@@ -2,25 +2,24 @@ package main
2
2
3
3
import (
4
4
"database/sql"
5
+ "errors"
5
6
"flag"
6
7
"fmt"
7
8
"io/ioutil"
8
9
"math"
9
10
"net/http"
10
11
"os"
11
- "strconv"
12
- "time"
13
12
"regexp"
14
- "errors "
13
+ "strconv "
15
14
"sync"
15
+ "time"
16
16
17
17
"gopkg.in/yaml.v2"
18
18
19
+ "github.com/blang/semver"
19
20
_ "github.com/lib/pq"
20
21
"github.com/prometheus/client_golang/prometheus"
21
22
"github.com/prometheus/common/log"
22
- "github.com/blang/semver"
23
-
24
23
)
25
24
26
25
var Version string = "0.0.1"
@@ -116,10 +115,10 @@ func parseVersion(versionString string) (semver.Version, error) {
116
115
117
116
// User-friendly representation of a prometheus descriptor map
118
117
type ColumnMapping struct {
119
- usage ColumnUsage `yaml:"usage"`
120
- description string `yaml:"description"`
121
- mapping map [string ]float64 `yaml:"metric_mapping"` // Optional column mapping for MAPPEDMETRIC
122
- supportedVersions semver.Range `yaml:"pg_version"` // Semantic version ranges which are supported. Unsupported columns are not queried (internally converted to DISCARD).
118
+ usage ColumnUsage `yaml:"usage"`
119
+ description string `yaml:"description"`
120
+ mapping map [string ]float64 `yaml:"metric_mapping"` // Optional column mapping for MAPPEDMETRIC
121
+ supportedVersions semver.Range `yaml:"pg_version"` // Semantic version ranges which are supported. Unsupported columns are not queried (internally converted to DISCARD).
123
122
}
124
123
125
124
func (this * ColumnMapping ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
@@ -232,7 +231,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
232
231
"count" : {GAUGE , "Number of locks" , nil , nil },
233
232
},
234
233
"pg_stat_replication" : map [string ]ColumnMapping {
235
- "procpid" : {DISCARD , "Process ID of a WAL sender process" , nil , semver .MustParseRange ("<9.2.0" )},
234
+ "procpid" : {DISCARD , "Process ID of a WAL sender process" , nil , semver .MustParseRange ("<9.2.0" )},
236
235
"pid" : {DISCARD , "Process ID of a WAL sender process" , nil , semver .MustParseRange (">=9.2.0" )},
237
236
"usesysid" : {DISCARD , "OID of the user logged into this WAL sender process" , nil , nil },
238
237
"usename" : {DISCARD , "Name of the user logged into this WAL sender process" , nil , nil },
@@ -277,7 +276,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
277
276
// the semver matching we do for columns.
278
277
type OverrideQuery struct {
279
278
versionRange semver.Range
280
- query string
279
+ query string
281
280
}
282
281
283
282
// Overriding queries for namespaces above.
@@ -359,7 +358,6 @@ var queryOverrides = map[string][]OverrideQuery{
359
358
},
360
359
// No query is applicable for 9.1 that gives any sensible data.
361
360
},
362
-
363
361
}
364
362
365
363
// Convert the query override file to the version-specific query override file
@@ -510,7 +508,10 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
510
508
// Check column version compatibility for the current map
511
509
// Force to discard if not compatible.
512
510
if columnMapping .supportedVersions != nil {
513
- if columnMapping .supportedVersions (pgVersion ) {
511
+ if ! columnMapping .supportedVersions (pgVersion ) {
512
+ // It's very useful to be able to see what columns are being
513
+ // rejected.
514
+ log .Debugln (columnName , "is being forced to discard due to version incompatibility." )
514
515
thisMap [columnName ] = MetricMap {
515
516
discard : true ,
516
517
conversion : func (in interface {}) (float64 , bool ) {
@@ -684,26 +685,26 @@ func dbToString(t interface{}) (string, bool) {
684
685
// Exporter collects Postgres metrics. It implements prometheus.Collector.
685
686
type Exporter struct {
686
687
dsn string
687
- userQueriesPath string
688
+ userQueriesPath string
688
689
duration , error prometheus.Gauge
689
690
totalScrapes prometheus.Counter
690
691
691
692
// Last version used to calculate metric map. If mismatch on scrape,
692
693
// then maps are recalculated.
693
- lastMapVersion semver.Version
694
+ lastMapVersion semver.Version
694
695
// Currently active variable map
695
- variableMap map [string ]MetricMapNamespace
696
+ variableMap map [string ]MetricMapNamespace
696
697
// Currently active metric map
697
- metricMap map [string ]MetricMapNamespace
698
+ metricMap map [string ]MetricMapNamespace
698
699
// Currently active query overrides
699
- queryOverrides map [string ]string
700
- mappingMtx sync.RWMutex
700
+ queryOverrides map [string ]string
701
+ mappingMtx sync.RWMutex
701
702
}
702
703
703
704
// NewExporter returns a new PostgreSQL exporter for the provided DSN.
704
705
func NewExporter (dsn string , userQueriesPath string ) * Exporter {
705
706
return & Exporter {
706
- dsn : dsn ,
707
+ dsn : dsn ,
707
708
userQueriesPath : userQueriesPath ,
708
709
duration : prometheus .NewGauge (prometheus.GaugeOpts {
709
710
Namespace : namespace ,
@@ -723,8 +724,8 @@ func NewExporter(dsn string, userQueriesPath string) *Exporter {
723
724
Name : "last_scrape_error" ,
724
725
Help : "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success)." ,
725
726
}),
726
- variableMap : nil ,
727
- metricMap : nil ,
727
+ variableMap : nil ,
728
+ metricMap : nil ,
728
729
queryOverrides : nil ,
729
730
}
730
731
}
@@ -949,7 +950,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
949
950
e .lastMapVersion = semanticVersion
950
951
951
952
if e .userQueriesPath != "" {
952
- if err := addQueries (e .userQueriesPath , semanticVersion , e .metricMap , e .queryOverrides ) ; err != nil {
953
+ if err := addQueries (e .userQueriesPath , semanticVersion , e .metricMap , e .queryOverrides ); err != nil {
953
954
log .Errorln ("Failed to reload user queries:" , e .userQueriesPath , err )
954
955
}
955
956
}
0 commit comments