Skip to content

Commit

Permalink
Add test cases for unknown metric parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrouesnel committed Nov 29, 2017
1 parent 401e36d commit a8cd4d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 9 additions & 3 deletions postgres_exporter.go
Expand Up @@ -125,6 +125,7 @@ type MetricMap struct {

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

// Exporter collects Postgres metrics. It implements prometheus.Collector.
type Exporter struct {
// Holds a reference to the build in column mappings. Currently this is for testing purposes
// only, since it just points to the global.
builtinMetricMaps map[string]map[string]ColumnMapping

dsn string
userQueriesPath string
duration prometheus.Gauge
Expand All @@ -685,8 +690,9 @@ type Exporter struct {
// NewExporter returns a new PostgreSQL exporter for the provided DSN.
func NewExporter(dsn string, userQueriesPath string) *Exporter {
return &Exporter{
dsn: dsn,
userQueriesPath: userQueriesPath,
builtinMetricMaps: builtinMetricMaps,
dsn: dsn,
userQueriesPath: userQueriesPath,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Expand Down Expand Up @@ -913,7 +919,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
log.Infoln("Semantic Version Changed:", e.lastMapVersion.String(), "->", semanticVersion.String())
e.mappingMtx.Lock()

e.metricMap = makeDescMap(semanticVersion, builtinMetricMaps)
e.metricMap = makeDescMap(semanticVersion, e.builtinMetricMaps)
e.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
e.lastMapVersion = semanticVersion

Expand Down
27 changes: 27 additions & 0 deletions postgres_exporter_integration_test.go
Expand Up @@ -95,3 +95,30 @@ func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
c.Assert(exporter, NotNil)
exporter.scrape(ch)
}

// TestUnknownMetricParsingDoesntCrash deliberately deletes all the column maps out
// of an exporter to test that the default metric handling code can cope with unknown columns.
func (s *IntegrationSuite) TestUnknownMetricParsingDoesntCrash(c *C) {
// Setup a dummy channel to consume metrics
ch := make(chan prometheus.Metric, 100)
go func() {
for range ch {
}
}()

dsn := os.Getenv("DATA_SOURCE_NAME")
c.Assert(dsn, Not(Equals), "")

exporter := NewExporter(dsn, "")
c.Assert(exporter, NotNil)

// Convert the default maps into a list of empty maps.
emptyMaps := make(map[string]map[string]ColumnMapping, 0)
for k := range exporter.builtinMetricMaps {
emptyMaps[k] = map[string]ColumnMapping{}
}
exporter.builtinMetricMaps = emptyMaps

// scrape the exporter and make sure it works
exporter.scrape(ch)
}

0 comments on commit a8cd4d9

Please sign in to comment.