Skip to content

Commit 2a5692c

Browse files
authored
Adjust collector to use separate connection per scrape (#931)
Fixes #921 Signed-off-by: Joe Adams <[email protected]>
1 parent f0f051c commit 2a5692c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

collector/collector.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,22 @@ func (p PostgresCollector) Describe(ch chan<- *prometheus.Desc) {
167167
func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
168168
ctx := context.TODO()
169169

170+
// copy the instance so that concurrent scrapes have independent instances
171+
inst := p.instance.copy()
172+
170173
// Set up the database connection for the collector.
171-
err := p.instance.setup()
174+
err := inst.setup()
172175
if err != nil {
173176
level.Error(p.logger).Log("msg", "Error opening connection to database", "err", err)
174177
return
175178
}
176-
defer p.instance.Close()
179+
defer inst.Close()
177180

178181
wg := sync.WaitGroup{}
179182
wg.Add(len(p.Collectors))
180183
for name, c := range p.Collectors {
181184
go func(name string, c Collector) {
182-
execute(ctx, name, c, p.instance, ch, p.logger)
185+
execute(ctx, name, c, inst, ch, p.logger)
183186
wg.Done()
184187
}(name, c)
185188
}

collector/instance.go

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func newInstance(dsn string) (*instance, error) {
4343
return i, nil
4444
}
4545

46+
// copy returns a copy of the instance.
47+
func (i *instance) copy() *instance {
48+
return &instance{
49+
dsn: i.dsn,
50+
}
51+
}
52+
4653
func (i *instance) setup() error {
4754
db, err := sql.Open("postgres", i.dsn)
4855
if err != nil {

0 commit comments

Comments
 (0)