Skip to content

Commit 6802f4b

Browse files
committed
Add test cases for unknown metric parsing.
1 parent df3a71e commit 6802f4b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

postgres_exporter.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ type MetricMap struct {
125125

126126
// TODO: revisit cu with the semver system
127127
func dumpMaps() {
128+
// TODO: make this function part of the exporter
128129
for name, cmap := range builtinMetricMaps {
129130
query, ok := queryOverrides[name]
130131
if !ok {
@@ -659,6 +660,10 @@ func dbToString(t interface{}) (string, bool) {
659660

660661
// Exporter collects Postgres metrics. It implements prometheus.Collector.
661662
type Exporter struct {
663+
// Holds a reference to the build in column mappings. Currently this is for testing purposes
664+
// only, since it just points to the global.
665+
builtinMetricMaps map[string]map[string]ColumnMapping
666+
662667
dsn string
663668
userQueriesPath string
664669
duration prometheus.Gauge
@@ -685,8 +690,9 @@ type Exporter struct {
685690
// NewExporter returns a new PostgreSQL exporter for the provided DSN.
686691
func NewExporter(dsn string, userQueriesPath string) *Exporter {
687692
return &Exporter{
688-
dsn: dsn,
689-
userQueriesPath: userQueriesPath,
693+
builtinMetricMaps: builtinMetricMaps,
694+
dsn: dsn,
695+
userQueriesPath: userQueriesPath,
690696
duration: prometheus.NewGauge(prometheus.GaugeOpts{
691697
Namespace: namespace,
692698
Subsystem: exporter,
@@ -913,7 +919,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
913919
log.Infoln("Semantic Version Changed:", e.lastMapVersion.String(), "->", semanticVersion.String())
914920
e.mappingMtx.Lock()
915921

916-
e.metricMap = makeDescMap(semanticVersion, builtinMetricMaps)
922+
e.metricMap = makeDescMap(semanticVersion, e.builtinMetricMaps)
917923
e.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
918924
e.lastMapVersion = semanticVersion
919925

postgres_exporter_integration_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,30 @@ func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
9595
c.Assert(exporter, NotNil)
9696
exporter.scrape(ch)
9797
}
98+
99+
// TestUnknownMetricParsingDoesntCrash deliberately deletes all the column maps out
100+
// of an exporter to test that the default metric handling code can cope with unknown columns.
101+
func (s *IntegrationSuite) TestUnknownMetricParsingDoesntCrash(c *C) {
102+
// Setup a dummy channel to consume metrics
103+
ch := make(chan prometheus.Metric, 100)
104+
go func() {
105+
for range ch {
106+
}
107+
}()
108+
109+
dsn := os.Getenv("DATA_SOURCE_NAME")
110+
c.Assert(dsn, Not(Equals), "")
111+
112+
exporter := NewExporter(dsn, "")
113+
c.Assert(exporter, NotNil)
114+
115+
// Convert the default maps into a list of empty maps.
116+
emptyMaps := make(map[string]map[string]ColumnMapping, 0)
117+
for k := range exporter.builtinMetricMaps {
118+
emptyMaps[k] = map[string]ColumnMapping{}
119+
}
120+
exporter.builtinMetricMaps = emptyMaps
121+
122+
// scrape the exporter and make sure it works
123+
exporter.scrape(ch)
124+
}

0 commit comments

Comments
 (0)