Skip to content

Commit 84b7d0c

Browse files
PMM-9968 use logger with level (#508)
* PMM-9968 use logger with level * PMM-9968 fix tests
1 parent 0d4b3c3 commit 84b7d0c

9 files changed

+16
-12
lines changed

exporter/diagnostic_data_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
8787
debugResult(logger, m)
8888

8989
metrics := makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode)
90-
metrics = append(metrics, locksMetrics(m)...)
90+
metrics = append(metrics, locksMetrics(logger, m)...)
9191

9292
if d.compatibleMode {
9393
metrics = append(metrics, specialMetrics(d.ctx, client, m, logger)...)

exporter/diagnostic_data_collector_test.go

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

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

83-
ti := newTopologyInfo(ctx, client)
83+
ti := newTopologyInfo(ctx, client, logrus.New())
8484

8585
c := newDiagnosticDataCollector(ctx, client, logrus.New(), true, ti)
8686

@@ -120,7 +120,7 @@ func TestContextTimeout(t *testing.T) {
120120

121121
client := tu.DefaultTestClient(ctx, t)
122122

123-
ti := newTopologyInfo(ctx, client)
123+
ti := newTopologyInfo(ctx, client, logrus.New())
124124

125125
dbCount := 100
126126

exporter/exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (e *Exporter) Handler() http.Handler {
301301
}
302302

303303
// Topology can change between requests, so we need to get it every time.
304-
ti := newTopologyInfo(ctx, client)
304+
ti := newTopologyInfo(ctx, client, e.logger)
305305

306306
registry := e.makeRegistry(ctx, client, ti, requestOpts)
307307

exporter/serverstatus_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (d *serverStatusCollector) collect(ch chan<- prometheus.Metric) {
6666
return
6767
}
6868

69-
logrus.Debug("serverStatus result:")
69+
logger.Debug("serverStatus result:")
7070
debugResult(logger, m)
7171

7272
for _, metric := range makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode) {

exporter/top_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (d *topCollector) collect(ch chan<- prometheus.Metric) {
7171
return
7272
}
7373

74-
logrus.Debug("top result:")
74+
logger.Debug("top result:")
7575
debugResult(logger, m)
7676

7777
totals, ok := m["totals"].(primitive.M)

exporter/topology_info.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,25 @@ type topologyInfo struct {
5656
// by a new connector, able to reconnect if needed. In case of reconnection, we should
5757
// call loadLabels to refresh the labels because they might have changed
5858
client *mongo.Client
59+
logger *logrus.Logger
5960
rw sync.RWMutex
6061
labels map[string]string
6162
}
6263

6364
// ErrCannotGetTopologyLabels Cannot read topology labels.
6465
var ErrCannotGetTopologyLabels = fmt.Errorf("cannot get topology labels")
6566

66-
func newTopologyInfo(ctx context.Context, client *mongo.Client) *topologyInfo {
67+
func newTopologyInfo(ctx context.Context, client *mongo.Client, logger *logrus.Logger) *topologyInfo {
6768
ti := &topologyInfo{
6869
client: client,
70+
logger: logger,
6971
labels: make(map[string]string),
7072
rw: sync.RWMutex{},
7173
}
7274

7375
err := ti.loadLabels(ctx)
7476
if err != nil {
75-
logrus.Warnf("cannot load topology labels: %s", err)
77+
logger.Warnf("cannot load topology labels: %s", err)
7678
}
7779

7880
return ti

exporter/topology_info_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/sirupsen/logrus"
2526
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
2728

@@ -88,7 +89,7 @@ func TestTopologyLabels(t *testing.T) {
8889
require.NoError(t, err)
8990

9091
client := tu.TestClient(ctx, port, t)
91-
ti := newTopologyInfo(ctx, client)
92+
ti := newTopologyInfo(ctx, client, logrus.New())
9293
bl := ti.baseLabels()
9394
assert.Equal(t, tc.want[labelReplicasetName], bl[labelReplicasetName], tc.containerName)
9495
assert.Equal(t, tc.want[labelReplicasetState], bl[labelReplicasetState], tc.containerName)

exporter/v1_compatibility.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ func lockMetrics() []lockMetric {
697697
// This function reads the human readable list from lockMetrics() and creates a slice of metrics
698698
// ready to be exposed, taking the value for each metric from th provided bson.M structure from
699699
// getDiagnosticData.
700-
func locksMetrics(m bson.M) []prometheus.Metric {
700+
func locksMetrics(logger *logrus.Logger, m bson.M) []prometheus.Metric {
701701
metrics := lockMetrics()
702702
res := make([]prometheus.Metric, 0, len(metrics))
703703

@@ -707,7 +707,7 @@ func locksMetrics(m bson.M) []prometheus.Metric {
707707
continue
708708
}
709709
if err != nil {
710-
logrus.Errorf("cannot convert lock metric %s to old style: %s", mm.Desc(), err)
710+
logger.Errorf("cannot convert lock metric %s to old style: %s", mm.Desc(), err)
711711
continue
712712
}
713713
res = append(res, mm)

exporter/v1_compatibility_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/prometheus/client_golang/prometheus"
3030
dto "github.com/prometheus/client_model/go"
31+
"github.com/sirupsen/logrus"
3132
"github.com/stretchr/testify/assert"
3233
"go.mongodb.org/mongo-driver/bson"
3334

@@ -108,7 +109,7 @@ func TestAddLocksMetrics(t *testing.T) {
108109
assert.NoError(t, err)
109110

110111
var metrics []prometheus.Metric
111-
metrics = locksMetrics(m)
112+
metrics = locksMetrics(logrus.New(), m)
112113

113114
desc := make([]string, 0, len(metrics))
114115
for _, metric := range metrics {

0 commit comments

Comments
 (0)