Adjust collector to use separate connection per scrape (#931)
Fixes #921 Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
parent
f0f051cb9a
commit
2a5692c028
|
@ -167,19 +167,22 @@ func (p PostgresCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
|
func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
// copy the instance so that concurrent scrapes have independent instances
|
||||||
|
inst := p.instance.copy()
|
||||||
|
|
||||||
// Set up the database connection for the collector.
|
// Set up the database connection for the collector.
|
||||||
err := p.instance.setup()
|
err := inst.setup()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
level.Error(p.logger).Log("msg", "Error opening connection to database", "err", err)
|
level.Error(p.logger).Log("msg", "Error opening connection to database", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer p.instance.Close()
|
defer inst.Close()
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(len(p.Collectors))
|
wg.Add(len(p.Collectors))
|
||||||
for name, c := range p.Collectors {
|
for name, c := range p.Collectors {
|
||||||
go func(name string, c Collector) {
|
go func(name string, c Collector) {
|
||||||
execute(ctx, name, c, p.instance, ch, p.logger)
|
execute(ctx, name, c, inst, ch, p.logger)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}(name, c)
|
}(name, c)
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,13 @@ func newInstance(dsn string) (*instance, error) {
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy returns a copy of the instance.
|
||||||
|
func (i *instance) copy() *instance {
|
||||||
|
return &instance{
|
||||||
|
dsn: i.dsn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (i *instance) setup() error {
|
func (i *instance) setup() error {
|
||||||
db, err := sql.Open("postgres", i.dsn)
|
db, err := sql.Open("postgres", i.dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue