Skip to content

Delay database connection until scrape #882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ func (p PostgresCollector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements the prometheus.Collector interface.
func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
ctx := context.TODO()

// Set up the database connection for the collector.
err := p.instance.setup()
if err != nil {
level.Error(p.logger).Log("msg", "Error opening connection to database", "err", err)
return
}

wg := sync.WaitGroup{}
wg.Add(len(p.Collectors))
for name, c := range p.Collectors {
Expand Down
30 changes: 22 additions & 8 deletions collector/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,43 @@ import (
)

type instance struct {
dsn string
db *sql.DB
version semver.Version
}

func newInstance(dsn string) (*instance, error) {
i := &instance{}
i := &instance{
dsn: dsn,
}

// "Create" a database handle to verify the DSN provided is valid.
// Open is not guaranteed to create a connection.
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
db.Close()

return i, nil
}

func (i *instance) setup() error {
db, err := sql.Open("postgres", i.dsn)
if err != nil {
return err
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
i.db = db

version, err := queryVersion(db)
version, err := queryVersion(i.db)
if err != nil {
db.Close()
return nil, err
return fmt.Errorf("error querying postgresql version: %w", err)
} else {
i.version = version
}

i.version = version

return i, nil
return nil
}

func (i *instance) getDB() *sql.DB {
Expand Down
8 changes: 8 additions & 0 deletions collector/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/config"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -74,6 +75,13 @@ func (pc *ProbeCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (pc *ProbeCollector) Collect(ch chan<- prometheus.Metric) {
// Set up the database connection for the collector.
err := pc.instance.setup()
if err != nil {
level.Error(pc.logger).Log("msg", "Error opening connection to database", "err", err)
return
}

wg := sync.WaitGroup{}
wg.Add(len(pc.collectors))
for name, c := range pc.collectors {
Expand Down