Skip to content

Commit d99d953

Browse files
committed
Added command-line flag to disable the default metrics
1 parent 578b950 commit d99d953

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ postgres_exporter_integration_test
66
*-stamp
77
.idea
88
*.iml
9+
.vscode
910
cover.out
1011
cover.*.out
1112
.coverage

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ rich self-documenting metrics for the exporter.
9999
The -extend.query-path command-line argument specifies a YAML file containing additional queries to run.
100100
Some examples are provided in [queries.yaml](queries.yaml).
101101

102+
### Working with non-officially-supported postgres versions
103+
104+
If you want to use this exporter to monitor a postgres installation that is not officially supported (e.g. 8.2.15) or a variant of postgres (e.g. Greenplum).
105+
You may try to disable all internal metrics using the -disable-default-metrics command-line argument, then supply your own set of metrics definitions in
106+
an external config file.
107+
102108
### Running as non-superuser
103109

104110
To be able to collect metrics from pg_stat_activity and pg_stat_replication as non-superuser you have to create views as a superuser, and assign permissions separately to those. In PostgreSQL, views run with the permissions of the user that created them so they can act as security barriers.

postgres_exporter.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ import (
3232
var Version = "0.0.1"
3333

3434
var (
35-
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9187").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_LISTEN_ADDRESS").String()
36-
metricPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_TELEMETRY_PATH").String()
37-
queriesPath = kingpin.Flag("extend.query-path", "Path to custom queries to run.").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_EXTEND_QUERY_PATH").String()
38-
onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool()
35+
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9187").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_LISTEN_ADDRESS").String()
36+
metricPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_TELEMETRY_PATH").String()
37+
disableDefaultMetrics = kingpin.Flag("disable-default-metrics", "Do not include default metrics.").Default("false").OverrideDefaultFromEnvar("PG_EXPORTER_DISABLE_DEFAULT_METRICS").Bool()
38+
queriesPath = kingpin.Flag("extend.query-path", "Path to custom queries to run.").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_EXTEND_QUERY_PATH").String()
39+
onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool()
3940
)
4041

4142
// Metric name parts.
@@ -664,13 +665,14 @@ type Exporter struct {
664665
// only, since it just points to the global.
665666
builtinMetricMaps map[string]map[string]ColumnMapping
666667

667-
dsn string
668-
userQueriesPath string
669-
duration prometheus.Gauge
670-
error prometheus.Gauge
671-
psqlUp prometheus.Gauge
672-
userQueriesError *prometheus.GaugeVec
673-
totalScrapes prometheus.Counter
668+
dsn string
669+
disableDefaultMetrics bool
670+
userQueriesPath string
671+
duration prometheus.Gauge
672+
error prometheus.Gauge
673+
psqlUp prometheus.Gauge
674+
userQueriesError *prometheus.GaugeVec
675+
totalScrapes prometheus.Counter
674676

675677
// dbDsn is the connection string used to establish the dbConnection
676678
dbDsn string
@@ -688,11 +690,12 @@ type Exporter struct {
688690
}
689691

690692
// NewExporter returns a new PostgreSQL exporter for the provided DSN.
691-
func NewExporter(dsn string, userQueriesPath string) *Exporter {
693+
func NewExporter(dsn string, disableDefaultMetrics bool, userQueriesPath string) *Exporter {
692694
return &Exporter{
693695
builtinMetricMaps: builtinMetricMaps,
694696
dsn: dsn,
695-
userQueriesPath: userQueriesPath,
697+
disableDefaultMetrics: disableDefaultMetrics,
698+
userQueriesPath: userQueriesPath,
696699
duration: prometheus.NewGauge(prometheus.GaugeOpts{
697700
Namespace: namespace,
698701
Subsystem: exporter,
@@ -910,7 +913,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
910913
if err != nil {
911914
return fmt.Errorf("Error parsing version string: %v", err)
912915
}
913-
if semanticVersion.LT(lowestSupportedVersion) {
916+
if !e.disableDefaultMetrics && semanticVersion.LT(lowestSupportedVersion) {
914917
log.Warnln("PostgreSQL version is lower then our lowest supported version! Got", semanticVersion.String(), "minimum supported is", lowestSupportedVersion.String())
915918
}
916919

@@ -919,8 +922,18 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
919922
log.Infoln("Semantic Version Changed:", e.lastMapVersion.String(), "->", semanticVersion.String())
920923
e.mappingMtx.Lock()
921924

922-
e.metricMap = makeDescMap(semanticVersion, e.builtinMetricMaps)
923-
e.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
925+
if e.disableDefaultMetrics {
926+
e.metricMap = make(map[string]MetricMapNamespace)
927+
} else {
928+
e.metricMap = makeDescMap(semanticVersion, e.builtinMetricMaps)
929+
}
930+
931+
if e.disableDefaultMetrics {
932+
e.queryOverrides = make(map[string]string)
933+
} else {
934+
e.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
935+
}
936+
924937
e.lastMapVersion = semanticVersion
925938

926939
if e.userQueriesPath != "" {
@@ -1097,7 +1110,7 @@ func main() {
10971110
log.Fatal("couldn't find environment variables describing the datasource to use")
10981111
}
10991112

1100-
exporter := NewExporter(dsn, *queriesPath)
1113+
exporter := NewExporter(dsn, *disableDefaultMetrics, *queriesPath)
11011114
defer func() {
11021115
if exporter.dbConnection != nil {
11031116
exporter.dbConnection.Close() // nolint: errcheck

postgres_exporter_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s *IntegrationSuite) SetUpSuite(c *C) {
3131
dsn := os.Getenv("DATA_SOURCE_NAME")
3232
c.Assert(dsn, Not(Equals), "")
3333

34-
exporter := NewExporter(dsn, "")
34+
exporter := NewExporter(dsn, false, "")
3535
c.Assert(exporter, NotNil)
3636
// Assign the exporter to the suite
3737
s.e = exporter
@@ -86,12 +86,12 @@ func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
8686
}()
8787

8888
// Send a bad DSN
89-
exporter := NewExporter("invalid dsn", *queriesPath)
89+
exporter := NewExporter("invalid dsn", false, *queriesPath)
9090
c.Assert(exporter, NotNil)
9191
exporter.scrape(ch)
9292

9393
// Send a DSN to a non-listening port.
94-
exporter = NewExporter("postgresql://nothing:[email protected]:1/nothing", *queriesPath)
94+
exporter = NewExporter("postgresql://nothing:[email protected]:1/nothing", false, *queriesPath)
9595
c.Assert(exporter, NotNil)
9696
exporter.scrape(ch)
9797
}
@@ -109,7 +109,7 @@ func (s *IntegrationSuite) TestUnknownMetricParsingDoesntCrash(c *C) {
109109
dsn := os.Getenv("DATA_SOURCE_NAME")
110110
c.Assert(dsn, Not(Equals), "")
111111

112-
exporter := NewExporter(dsn, "")
112+
exporter := NewExporter(dsn, false, "")
113113
c.Assert(exporter, NotNil)
114114

115115
// Convert the default maps into a list of empty maps.

0 commit comments

Comments
 (0)