You've already forked postgres_exporter
mirror of
https://github.com/prometheus-community/postgres_exporter.git
synced 2025-06-14 17:22:33 +03:00
Refactor exporter so that it can be used as a library.
This commit is contained in:
committed by
Will Rouesnel
parent
4117fb2afc
commit
2ab8f10935
124
collector/postgres_exporter_integration_test.go
Normal file
124
collector/postgres_exporter_integration_test.go
Normal file
@ -0,0 +1,124 @@
|
||||
// These are specialized integration tests. We only build them when we're doing
|
||||
// a lot of additional work to keep the external docker environment they require
|
||||
// working.
|
||||
// +build integration
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
// Hook up gocheck into the "go test" runner.
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type IntegrationSuite struct {
|
||||
e *Exporter
|
||||
}
|
||||
|
||||
var _ = Suite(&IntegrationSuite{})
|
||||
|
||||
func (s *IntegrationSuite) SetUpSuite(c *C) {
|
||||
dsn := os.Getenv("DATA_SOURCE_NAME")
|
||||
c.Assert(dsn, Not(Equals), "")
|
||||
|
||||
exporter := NewExporter(dsn, "")
|
||||
c.Assert(exporter, NotNil)
|
||||
// Assign the exporter to the suite
|
||||
s.e = exporter
|
||||
|
||||
prometheus.MustRegister(exporter)
|
||||
}
|
||||
|
||||
// TODO: it would be nice if cu didn't mostly just recreate the scrape function
|
||||
func (s *IntegrationSuite) TestAllNamespacesReturnResults(c *C) {
|
||||
// Setup a dummy channel to consume metrics
|
||||
ch := make(chan prometheus.Metric, 100)
|
||||
go func() {
|
||||
for range ch {
|
||||
}
|
||||
}()
|
||||
|
||||
// Open a database connection
|
||||
db, err := sql.Open("postgres", s.e.dsn)
|
||||
c.Assert(db, NotNil)
|
||||
c.Assert(err, IsNil)
|
||||
defer db.Close()
|
||||
|
||||
// Do a version update
|
||||
err = s.e.checkMapVersions(ch, db)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
err = querySettings(ch, db)
|
||||
if !c.Check(err, Equals, nil) {
|
||||
fmt.Println("## ERRORS FOUND")
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// This should never happen in our test cases.
|
||||
errMap := queryNamespaceMappings(ch, db, s.e.metricMap, s.e.queryOverrides)
|
||||
if !c.Check(len(errMap), Equals, 0) {
|
||||
fmt.Println("## NAMESPACE ERRORS FOUND")
|
||||
for namespace, err := range errMap {
|
||||
fmt.Println(namespace, ":", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvalidDsnDoesntCrash tests that specifying an invalid DSN doesn't crash
|
||||
// the exporter. Related to https://github.com/wrouesnel/postgres_exporter/issues/93
|
||||
// although not a replication of the scenario.
|
||||
func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
|
||||
queriesPath := os.Getenv("PG_EXPORTER_EXTEND_QUERY_PATH")
|
||||
|
||||
// Setup a dummy channel to consume metrics
|
||||
ch := make(chan prometheus.Metric, 100)
|
||||
go func() {
|
||||
for range ch {
|
||||
}
|
||||
}()
|
||||
|
||||
// Send a bad DSN
|
||||
exporter := NewExporter("invalid dsn", queriesPath)
|
||||
c.Assert(exporter, NotNil)
|
||||
exporter.scrape(ch)
|
||||
|
||||
// Send a DSN to a non-listening port.
|
||||
exporter = NewExporter("postgresql://nothing:nothing@127.0.0.1:1/nothing", queriesPath)
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user