Add the instance struct to handle connections
The intent is to use the instance struct to hold the connection to the database as well as metadata about the instance. Currently this metadata only includes the version of postgres for the instance which can be used in the collectors to decide what query to run. In the future this could hold more metadata but for now it keeps the Collector interface arguments to a reasonable number. Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
@@ -15,7 +15,6 @@ package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sync"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
@@ -27,7 +26,7 @@ type ProbeCollector struct {
|
||||
registry *prometheus.Registry
|
||||
collectors map[string]Collector
|
||||
logger log.Logger
|
||||
db *sql.DB
|
||||
instance *instance
|
||||
}
|
||||
|
||||
func NewProbeCollector(logger log.Logger, excludeDatabases []string, registry *prometheus.Registry, dsn config.DSN) (*ProbeCollector, error) {
|
||||
@@ -58,18 +57,16 @@ func NewProbeCollector(logger log.Logger, excludeDatabases []string, registry *p
|
||||
}
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", dsn.GetConnectionString())
|
||||
instance, err := newInstance(dsn.GetConnectionString())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
|
||||
return &ProbeCollector{
|
||||
registry: registry,
|
||||
collectors: collectors,
|
||||
logger: logger,
|
||||
db: db,
|
||||
instance: instance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -81,7 +78,7 @@ func (pc *ProbeCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
wg.Add(len(pc.collectors))
|
||||
for name, c := range pc.collectors {
|
||||
go func(name string, c Collector) {
|
||||
execute(context.TODO(), name, c, pc.db, ch, pc.logger)
|
||||
execute(context.TODO(), name, c, pc.instance, ch, pc.logger)
|
||||
wg.Done()
|
||||
}(name, c)
|
||||
}
|
||||
@@ -89,5 +86,5 @@ func (pc *ProbeCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
}
|
||||
|
||||
func (pc *ProbeCollector) Close() error {
|
||||
return pc.db.Close()
|
||||
return pc.instance.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user