Skip to content

Commit

Permalink
PMM-13477 fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
BupycHuk committed Nov 4, 2024
1 parent a6a06a1 commit 38fa4f0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
18 changes: 15 additions & 3 deletions exporter/diagnostic_data_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,25 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
err = errors.Wrapf(errUnexpectedDataType, "%T for data field", m["data"])
logger.Errorf("cannot decode getDiagnosticData: %s", err)
}
if c, ok := m["common"].(bson.M); ok {
m = c
}

logger.Debug("getDiagnosticData result")
debugResult(logger, m)

// MongoDB 8.0 splits the diagnostic data into multiple blocks, so we need to merge them
if d.buildInfo.VersionArray[0] >= 8 {
b := bson.M{}
for _, mv := range m {
block, ok := mv.(bson.M)
if !ok {
continue
}
for k, v := range block {
b[k] = v
}
}
m = b
}

metrics = makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode)
metrics = append(metrics, locksMetrics(logger, m)...)

Expand Down
2 changes: 1 addition & 1 deletion exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
// replSetGetStatus is not supported through mongos.
if e.opts.EnableReplicasetStatus && nodeType != typeMongos && requestOpts.EnableReplicasetStatus {
rsgsc := newReplicationSetStatusCollector(ctx, client, e.opts.Logger,
e.opts.CompatibleMode, topologyInfo, dbBuildInfo)
e.opts.CompatibleMode, topologyInfo)
registry.MustRegister(rsgsc)
}

Expand Down
9 changes: 2 additions & 7 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
"sync"
"testing"

"github.com/percona/mongodb_exporter/internal/tu"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/percona/mongodb_exporter/internal/tu"
)

// Use this for testing because labels like cluster ID are not constant in docker containers
Expand Down Expand Up @@ -187,10 +185,7 @@ func TestMongoS(t *testing.T) {
assert.NoError(t, err)

e := New(exporterOpts)

version, err := retrieveMongoDBBuildInfo(ctx, client, exporterOpts.Logger.WithField("component", "test"))
require.NoError(t, err)
rsgsc := newReplicationSetStatusCollector(ctx, client, e.opts.Logger, e.opts.CompatibleMode, new(labelsGetterMock), version)
rsgsc := newReplicationSetStatusCollector(ctx, client, e.opts.Logger, e.opts.CompatibleMode, new(labelsGetterMock))

r := e.makeRegistry(ctx, client, new(labelsGetterMock), *e.opts)

Expand Down
20 changes: 1 addition & 19 deletions exporter/replset_status_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package exporter

import (
"context"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -36,19 +34,16 @@ type replSetGetStatusCollector struct {

compatibleMode bool
topologyInfo labelsGetter

version buildInfo
}

// newReplicationSetStatusCollector creates a collector for statistics on replication set.
func newReplicationSetStatusCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter, version buildInfo) *replSetGetStatusCollector {
func newReplicationSetStatusCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter) *replSetGetStatusCollector {
return &replSetGetStatusCollector{
ctx: ctx,
base: newBaseCollector(client, logger.WithFields(logrus.Fields{"collector": "replset_status"})),

compatibleMode: compatible,
topologyInfo: topology,
version: version,
}
}

Expand Down Expand Up @@ -88,19 +83,6 @@ func (d *replSetGetStatusCollector) collect(ch chan<- prometheus.Metric) {
for _, metric := range makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode) {
ch <- metric
}

if strings.HasPrefix(d.version.Version, "8.") {
for _, metric := range makeMetrics("rs", m, d.topologyInfo.baseLabels(), d.compatibleMode) {
ch <- metric
}
if d.compatibleMode {
logger.Infof("collecting compatibility metrics for version %s", d.version.Version)
metrics := replSetMetrics(m, logger)
for _, metric := range metrics {
ch <- metric
}
}
}
}

var _ prometheus.Collector = (*replSetGetStatusCollector)(nil)
15 changes: 4 additions & 11 deletions exporter/replset_status_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (
"testing"
"time"

"github.com/percona/mongodb_exporter/internal/tu"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/percona/mongodb_exporter/internal/tu"
)

func TestReplsetStatusCollector(t *testing.T) {
Expand All @@ -37,9 +35,7 @@ func TestReplsetStatusCollector(t *testing.T) {

ti := labelsGetterMock{}

version, err := retrieveMongoDBBuildInfo(ctx, client, logrus.New().WithField("component", "test"))
require.NoError(t, err)
c := newReplicationSetStatusCollector(ctx, client, logrus.New(), false, ti, version)
c := newReplicationSetStatusCollector(ctx, client, logrus.New(), false, ti)

// The last \n at the end of this string is important
expected := strings.NewReader(`
Expand All @@ -57,7 +53,7 @@ func TestReplsetStatusCollector(t *testing.T) {
"mongodb_myState",
"mongodb_ok",
}
err = testutil.CollectAndCompare(c, expected, filter...)
err := testutil.CollectAndCompare(c, expected, filter...)
assert.NoError(t, err)
}

Expand All @@ -69,10 +65,7 @@ func TestReplsetStatusCollectorNoSharding(t *testing.T) {

ti := labelsGetterMock{}

version, err := retrieveMongoDBBuildInfo(ctx, client, logrus.New().WithField("component", "test"))
require.NoError(t, err)

c := newReplicationSetStatusCollector(ctx, client, logrus.New(), false, ti, version)
c := newReplicationSetStatusCollector(ctx, client, logrus.New(), false, ti)

// Replication set metrics should not be generated for unsharded server
count := testutil.CollectAndCount(c)
Expand Down

0 comments on commit 38fa4f0

Please sign in to comment.