Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions exporter/collstats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package exporter

import (
"regexp"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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
}

Expand Down