Skip to content

Commit b090789

Browse files
author
Anton Kucherov
authored
PMM-3473 Fix panic. (#142)
PMM-3473 Fix panic.
1 parent 0dcd9d7 commit b090789

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616
- Fixed some function comments based on best practices from Effective Go #137, thx [@CodeLingoBot](https://github.com/CodeLingoBot).
17+
- [PMM-3473](https://jira.percona.com/browse/PMM-3473): Fixed panic and runtime error.
1718

1819
## [0.7.0]
1920
### Changed

collector/mongod/metrics.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,18 @@ type ReplStats struct {
407407

408408
// Export exposes the replication stats.
409409
func (replStats *ReplStats) Export(ch chan<- prometheus.Metric) {
410-
replStats.Apply.Export(ch)
411-
replStats.Buffer.Export(ch)
412-
replStats.Network.Export(ch)
413-
replStats.PreloadStats.Export(ch)
410+
if replStats.Apply != nil {
411+
replStats.Apply.Export(ch)
412+
}
413+
if replStats.Buffer != nil {
414+
replStats.Buffer.Export(ch)
415+
}
416+
if replStats.Network != nil {
417+
replStats.Network.Export(ch)
418+
}
419+
if replStats.PreloadStats != nil {
420+
replStats.PreloadStats.Export(ch)
421+
}
414422
// 3.0+ only
415423
if replStats.Executor != nil {
416424
replStats.Executor.Export(ch)
@@ -425,11 +433,15 @@ type PreloadStats struct {
425433

426434
// Export exposes the preload stats.
427435
func (preloadStats *PreloadStats) Export(ch chan<- prometheus.Metric) {
428-
metricsReplPreloadDocsNumTotal.Set(preloadStats.Docs.Num)
429-
metricsReplPreloadDocsTotalMilliseconds.Set(preloadStats.Docs.TotalMillis)
436+
if preloadStats.Docs != nil {
437+
metricsReplPreloadDocsNumTotal.Set(preloadStats.Docs.Num)
438+
metricsReplPreloadDocsTotalMilliseconds.Set(preloadStats.Docs.TotalMillis)
439+
}
430440

431-
metricsReplPreloadIndexesNumTotal.Set(preloadStats.Indexes.Num)
432-
metricsReplPreloadIndexesTotalMilliseconds.Set(preloadStats.Indexes.TotalMillis)
441+
if preloadStats.Indexes != nil {
442+
metricsReplPreloadIndexesNumTotal.Set(preloadStats.Indexes.Num)
443+
metricsReplPreloadIndexesTotalMilliseconds.Set(preloadStats.Indexes.TotalMillis)
444+
}
433445
}
434446

435447
// StorageStats are the stats associated with the storage.

collector/mongod/metrics_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mongod
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestReplStatsExportShouldNotPanic(t *testing.T) {
11+
rs := &ReplStats{}
12+
ch := make(chan prometheus.Metric)
13+
f := func() { rs.Export(ch) }
14+
15+
assert.NotPanics(t, f, "nil pointer in ReplStats")
16+
}
17+
18+
func TestPreloadStatsExportShouldNotPanic(t *testing.T) {
19+
ps := &PreloadStats{}
20+
ch := make(chan prometheus.Metric)
21+
f := func() { ps.Export(ch) }
22+
23+
assert.NotPanics(t, f, "nil pointer in PreloadStats")
24+
}

0 commit comments

Comments
 (0)