Skip to content

Commit 0804dd0

Browse files
sysadmindBupycHuk
authored andcommitted
Adjust collector to use separate connection per scrape (prometheus-community#931)
Fixes prometheus-community#921 Signed-off-by: Joe Adams <[email protected]> (cherry picked from commit 2a5692c)
1 parent af66408 commit 0804dd0

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
@@ -50,6 +50,13 @@ func newInstance(dsn string) (*instance, error) {
5050
return i, nil
5151
}
5252

53+
// copy returns a copy of the instance.
54+
func (i *instance) copy() *instance {
55+
return &instance{
56+
dsn: i.dsn,
57+
}
58+
}
59+
5360
func (i *instance) setup() error {
5461
db, err := sql.Open("postgres", i.dsn)
5562
if err != nil {

0 commit comments

Comments
 (0)