@@ -2,25 +2,24 @@ package main
22
33import (
44 "database/sql"
5+ "errors"
56 "flag"
67 "fmt"
78 "io/ioutil"
89 "math"
910 "net/http"
1011 "os"
11- "strconv"
12- "time"
1312 "regexp"
14- "errors "
13+ "strconv "
1514 "sync"
15+ "time"
1616
1717 "gopkg.in/yaml.v2"
1818
19+ "github.com/blang/semver"
1920 _ "github.com/lib/pq"
2021 "github.com/prometheus/client_golang/prometheus"
2122 "github.com/prometheus/common/log"
22- "github.com/blang/semver"
23-
2423)
2524
2625var Version string = "0.0.1"
@@ -116,10 +115,10 @@ func parseVersion(versionString string) (semver.Version, error) {
116115
117116// User-friendly representation of a prometheus descriptor map
118117type 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).
123122}
124123
125124func (this * ColumnMapping ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
@@ -232,7 +231,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
232231 "count" : {GAUGE , "Number of locks" , nil , nil },
233232 },
234233 "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" )},
236235 "pid" : {DISCARD , "Process ID of a WAL sender process" , nil , semver .MustParseRange (">=9.2.0" )},
237236 "usesysid" : {DISCARD , "OID of the user logged into this WAL sender process" , nil , nil },
238237 "usename" : {DISCARD , "Name of the user logged into this WAL sender process" , nil , nil },
@@ -276,7 +275,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
276275// the semver matching we do for columns.
277276type OverrideQuery struct {
278277 versionRange semver.Range
279- query string
278+ query string
280279}
281280
282281// Overriding queries for namespaces above.
@@ -358,7 +357,6 @@ var queryOverrides = map[string][]OverrideQuery{
358357 },
359358 // No query is applicable for 9.1 that gives any sensible data.
360359 },
361-
362360}
363361
364362// Convert the query override file to the version-specific query override file
@@ -509,7 +507,7 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
509507 // Check column version compatibility for the current map
510508 // Force to discard if not compatible.
511509 if columnMapping .supportedVersions != nil {
512- if columnMapping .supportedVersions (pgVersion ) {
510+ if ! columnMapping .supportedVersions (pgVersion ) {
513511 thisMap [columnName ] = MetricMap {
514512 discard : true ,
515513 conversion : func (in interface {}) (float64 , bool ) {
@@ -683,26 +681,26 @@ func dbToString(t interface{}) (string, bool) {
683681// Exporter collects Postgres metrics. It implements prometheus.Collector.
684682type Exporter struct {
685683 dsn string
686- userQueriesPath string
684+ userQueriesPath string
687685 duration , error prometheus.Gauge
688686 totalScrapes prometheus.Counter
689687
690688 // Last version used to calculate metric map. If mismatch on scrape,
691689 // then maps are recalculated.
692- lastMapVersion semver.Version
690+ lastMapVersion semver.Version
693691 // Currently active variable map
694- variableMap map [string ]MetricMapNamespace
692+ variableMap map [string ]MetricMapNamespace
695693 // Currently active metric map
696- metricMap map [string ]MetricMapNamespace
694+ metricMap map [string ]MetricMapNamespace
697695 // Currently active query overrides
698- queryOverrides map [string ]string
699- mappingMtx sync.RWMutex
696+ queryOverrides map [string ]string
697+ mappingMtx sync.RWMutex
700698}
701699
702700// NewExporter returns a new PostgreSQL exporter for the provided DSN.
703701func NewExporter (dsn string , userQueriesPath string ) * Exporter {
704702 return & Exporter {
705- dsn : dsn ,
703+ dsn : dsn ,
706704 userQueriesPath : userQueriesPath ,
707705 duration : prometheus .NewGauge (prometheus.GaugeOpts {
708706 Namespace : namespace ,
@@ -722,8 +720,8 @@ func NewExporter(dsn string, userQueriesPath string) *Exporter {
722720 Name : "last_scrape_error" ,
723721 Help : "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success)." ,
724722 }),
725- variableMap : nil ,
726- metricMap : nil ,
723+ variableMap : nil ,
724+ metricMap : nil ,
727725 queryOverrides : nil ,
728726 }
729727}
@@ -948,7 +946,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
948946 e .lastMapVersion = semanticVersion
949947
950948 if e .userQueriesPath != "" {
951- if err := addQueries (e .userQueriesPath , semanticVersion , e .metricMap , e .queryOverrides ) ; err != nil {
949+ if err := addQueries (e .userQueriesPath , semanticVersion , e .metricMap , e .queryOverrides ); err != nil {
952950 log .Errorln ("Failed to reload user queries:" , e .userQueriesPath , err )
953951 }
954952 }
0 commit comments