diff --git a/exporter/collstats_collector_test.go b/exporter/collstats_collector_test.go index b098418a3..491ca2a58 100644 --- a/exporter/collstats_collector_test.go +++ b/exporter/collstats_collector_test.go @@ -25,7 +25,10 @@ import ( "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/common/promslog" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "github.com/percona/mongodb_exporter/internal/tu" ) @@ -93,3 +96,61 @@ mongodb_collstats_storageStats_capped{collection="testcol_02",database="testdb"} err := testutil.CollectAndCompare(c, expected, filter...) assert.NoError(t, err) } + +func TestCollStatsForFakeCountType(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + + client := tu.DefaultTestClient(ctx, t) + + database := client.Database("testdb") + database.Drop(ctx) //nolint + + defer func() { + err := database.Drop(ctx) + require.NoError(t, err) + }() + + collName := "test_collection_account" + coll := database.Collection(collName) + + _, err := coll.InsertOne(ctx, bson.M{"account_id": 1, "count": 10}) + require.NoError(t, err) + _, err = coll.InsertOne(ctx, bson.M{"account_id": 2, "count": 20}) + require.NoError(t, err) + + indexModel := mongo.IndexModel{ + Keys: bson.D{{Key: "account_id", Value: 1}}, + Options: options.Index().SetName("test_index_account"), + } + _, err = coll.Indexes().CreateOne(ctx, indexModel) + require.NoError(t, err) + + indexModel = mongo.IndexModel{ + Keys: bson.D{{Key: "count", Value: 1}}, + Options: options.Index().SetName("test_index_count"), + } + _, err = coll.Indexes().CreateOne(ctx, indexModel) + require.NoError(t, err) + + ti := labelsGetterMock{} + + collection := []string{"testdb.test_collection_account"} + logger := promslog.New(&promslog.Config{}) + c := newCollectionStatsCollector(ctx, client, logger, false, ti, collection, false) + + expected := strings.NewReader(` + # HELP mongodb_collstats_storageStats_indexSizes collstats.storageStats.indexSizes + # TYPE mongodb_collstats_storageStats_indexSizes untyped + mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="_id_"} 4096 + mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_account"} 20480 + mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_count"} 20480 + `) + + filter := []string{ + "mongodb_collstats_storageStats_indexSizes", + } + err = testutil.CollectAndCompare(c, expected, filter...) + require.NoError(t, err) +} diff --git a/exporter/metrics.go b/exporter/metrics.go index d4e28dc18..0fe13afec 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -17,6 +17,7 @@ package exporter import ( "regexp" + "slices" "strings" "sync" "time" @@ -220,7 +221,10 @@ func makeRawMetric(prefix, name string, value interface{}, labels map[string]str fqName, label := nameAndLabel(prefix, name) metricType := prometheus.UntypedValue - if strings.HasSuffix(strings.ToLower(name), "count") { + + // reservedPrefixes are used for metrics that might include the word ‘count’ in their name but are not actual counters. + reservedPrefixes := []string{"collstats.storageStats.indexSizes."} + if !slices.Contains(reservedPrefixes, prefix) && strings.HasSuffix(strings.ToLower(name), "count") { metricType = prometheus.CounterValue }