Skip to content

Commit 5ceed65

Browse files
Shashank Sinhapercona-csalguerodenisok
authored
PMM-9757 Fixed panic on lost connection (#465)
Changed the way we instantiate the topology info object. Now we call the newTopologyInfo and it never returns an error and the object is never nill. If it is not possible to connect, topology labels remain empty but we return a valid and not nill object. Also added context termination check to avoid the ugly stack traces produced by context timeouts. Co-authored-by: Carlos Salguero <[email protected]> Co-authored-by: Denys Kondratenko <[email protected]>
1 parent 8a342d4 commit 5ceed65

13 files changed

+27
-32
lines changed

exporter/base_collector.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package exporter
1818

1919
import (
20+
"context"
2021
"sync"
2122

2223
"github.com/prometheus/client_golang/prometheus"
@@ -40,7 +41,13 @@ func newBaseCollector(client *mongo.Client, logger *logrus.Logger) *baseCollecto
4041
}
4142
}
4243

43-
func (d *baseCollector) Describe(ch chan<- *prometheus.Desc, collect func(mCh chan<- prometheus.Metric)) {
44+
func (d *baseCollector) Describe(ctx context.Context, ch chan<- *prometheus.Desc, collect func(mCh chan<- prometheus.Metric)) {
45+
select {
46+
case <-ctx.Done():
47+
return
48+
default:
49+
}
50+
4451
d.lock.Lock()
4552
defer d.lock.Unlock()
4653

exporter/collstats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func newCollectionStatsCollector(ctx context.Context, client *mongo.Client, logg
5252
}
5353

5454
func (d *collstatsCollector) Describe(ch chan<- *prometheus.Desc) {
55-
d.base.Describe(ch, d.collect)
55+
d.base.Describe(d.ctx, ch, d.collect)
5656
}
5757

5858
func (d *collstatsCollector) Collect(ch chan<- prometheus.Metric) {

exporter/dbstats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func newDBStatsCollector(ctx context.Context, client *mongo.Client, logger *logr
4949
}
5050

5151
func (d *dbstatsCollector) Describe(ch chan<- *prometheus.Desc) {
52-
d.base.Describe(ch, d.collect)
52+
d.base.Describe(d.ctx, ch, d.collect)
5353
}
5454

5555
func (d *dbstatsCollector) Collect(ch chan<- prometheus.Metric) {

exporter/diagnostic_data_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func newDiagnosticDataCollector(ctx context.Context, client *mongo.Client, logge
4646
}
4747

4848
func (d *diagnosticDataCollector) Describe(ch chan<- *prometheus.Desc) {
49-
d.base.Describe(ch, d.collect)
49+
d.base.Describe(d.ctx, ch, d.collect)
5050
}
5151

5252
func (d *diagnosticDataCollector) Collect(ch chan<- prometheus.Metric) {

exporter/diagnostic_data_collector_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,12 @@ func TestAllDiagnosticDataCollectorMetrics(t *testing.T) {
8080

8181
client := tu.DefaultTestClient(ctx, t)
8282

83-
ti, err := newTopologyInfo(ctx, client)
84-
require.NoError(t, err)
83+
ti := newTopologyInfo(ctx, client)
8584

8685
c := newDiagnosticDataCollector(ctx, client, logrus.New(), true, ti)
8786

8887
reg := prometheus.NewRegistry()
89-
err = reg.Register(c)
88+
err := reg.Register(c)
9089
require.NoError(t, err)
9190
metrics := helpers.CollectMetrics(c)
9291
actualMetrics := helpers.ReadMetrics(metrics)
@@ -121,12 +120,11 @@ func TestContextTimeout(t *testing.T) {
121120

122121
client := tu.DefaultTestClient(ctx, t)
123122

124-
ti, err := newTopologyInfo(ctx, client)
125-
require.NoError(t, err)
123+
ti := newTopologyInfo(ctx, client)
126124

127125
dbCount := 100
128126

129-
err = addTestData(ctx, client, dbCount)
127+
err := addTestData(ctx, client, dbCount)
130128
assert.NoError(t, err)
131129

132130
defer cleanTestData(ctx, client, dbCount) //nolint:errcheck

exporter/exporter.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,7 @@ func (e *Exporter) Handler() http.Handler {
296296
}
297297

298298
// Topology can change between requests, so we need to get it every time.
299-
var ti *topologyInfo
300-
if client != nil {
301-
ti, err = newTopologyInfo(ctx, client)
302-
if err != nil {
303-
e.logger.Errorf("Cannot get topology info: %v", err)
304-
http.Error(
305-
w,
306-
"An error has occurred while getting topology info:\n\n"+err.Error(),
307-
http.StatusInternalServerError,
308-
)
309-
}
310-
}
299+
ti := newTopologyInfo(ctx, client)
311300

312301
registry := e.makeRegistry(ctx, client, ti, requestOpts)
313302

exporter/general_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func newGeneralCollector(ctx context.Context, client *mongo.Client, logger *logr
4141
}
4242

4343
func (d *generalCollector) Describe(ch chan<- *prometheus.Desc) {
44-
d.base.Describe(ch, d.collect)
44+
d.base.Describe(d.ctx, ch, d.collect)
4545
}
4646

4747
func (d *generalCollector) Collect(ch chan<- prometheus.Metric) {

exporter/indexstats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newIndexStatsCollector(ctx context.Context, client *mongo.Client, logger *l
5353
}
5454

5555
func (d *indexstatsCollector) Describe(ch chan<- *prometheus.Desc) {
56-
d.base.Describe(ch, d.collect)
56+
d.base.Describe(d.ctx, ch, d.collect)
5757
}
5858

5959
func (d *indexstatsCollector) Collect(ch chan<- prometheus.Metric) {

exporter/replset_status_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newReplicationSetStatusCollector(ctx context.Context, client *mongo.Client,
5050
}
5151

5252
func (d *replSetGetStatusCollector) Describe(ch chan<- *prometheus.Desc) {
53-
d.base.Describe(ch, d.collect)
53+
d.base.Describe(d.ctx, ch, d.collect)
5454
}
5555

5656
func (d *replSetGetStatusCollector) Collect(ch chan<- prometheus.Metric) {

exporter/serverstatus_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newServerStatusCollector(ctx context.Context, client *mongo.Client, logger
4444
}
4545

4646
func (d *serverStatusCollector) Describe(ch chan<- *prometheus.Desc) {
47-
d.base.Describe(ch, d.collect)
47+
d.base.Describe(d.ctx, ch, d.collect)
4848
}
4949

5050
func (d *serverStatusCollector) Collect(ch chan<- prometheus.Metric) {

0 commit comments

Comments
 (0)