Skip to content

Commit 0c4332e

Browse files
committed
PMM-14431 Fix and related test.
1 parent c2c8342 commit 0c4332e

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

exporter/collstats_collector_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
"github.com/prometheus/client_golang/prometheus/testutil"
2626
"github.com/prometheus/common/promslog"
2727
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
2829
"go.mongodb.org/mongo-driver/bson"
30+
"go.mongodb.org/mongo-driver/mongo"
31+
"go.mongodb.org/mongo-driver/mongo/options"
2932

3033
"github.com/percona/mongodb_exporter/internal/tu"
3134
)
@@ -93,3 +96,61 @@ mongodb_collstats_storageStats_capped{collection="testcol_02",database="testdb"}
9396
err := testutil.CollectAndCompare(c, expected, filter...)
9497
assert.NoError(t, err)
9598
}
99+
100+
func TestCollStatsForFakeCountType(t *testing.T) {
101+
t.Parallel()
102+
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
103+
defer cancel()
104+
105+
client := tu.DefaultTestClient(ctx, t)
106+
107+
database := client.Database("testdb")
108+
database.Drop(ctx) //nolint
109+
110+
defer func() {
111+
err := database.Drop(ctx)
112+
require.NoError(t, err)
113+
}()
114+
115+
collName := "test_collection_account"
116+
coll := database.Collection(collName)
117+
118+
_, err := coll.InsertOne(ctx, bson.M{"account_id": 1, "count": 10})
119+
require.NoError(t, err)
120+
_, err = coll.InsertOne(ctx, bson.M{"account_id": 2, "count": 20})
121+
require.NoError(t, err)
122+
123+
indexModel := mongo.IndexModel{
124+
Keys: bson.D{{Key: "account_id", Value: 1}},
125+
Options: options.Index().SetName("test_index_account"),
126+
}
127+
_, err = coll.Indexes().CreateOne(ctx, indexModel)
128+
require.NoError(t, err)
129+
130+
indexModel = mongo.IndexModel{
131+
Keys: bson.D{{Key: "count", Value: 1}},
132+
Options: options.Index().SetName("test_index_count"),
133+
}
134+
_, err = coll.Indexes().CreateOne(ctx, indexModel)
135+
require.NoError(t, err)
136+
137+
ti := labelsGetterMock{}
138+
139+
collection := []string{"testdb.test_collection_account"}
140+
logger := promslog.New(&promslog.Config{})
141+
c := newCollectionStatsCollector(ctx, client, logger, false, ti, collection, false)
142+
143+
expected := strings.NewReader(`
144+
# HELP mongodb_collstats_storageStats_indexSizes collstats.storageStats.indexSizes
145+
# TYPE mongodb_collstats_storageStats_indexSizes untyped
146+
mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="_id_"} 4096
147+
mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_account"} 20480
148+
mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_count"} 20480
149+
`)
150+
151+
filter := []string{
152+
"mongodb_collstats_storageStats_indexSizes",
153+
}
154+
err = testutil.CollectAndCompare(c, expected, filter...)
155+
require.NoError(t, err)
156+
}

exporter/metrics.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package exporter
1717

1818
import (
1919
"regexp"
20+
"slices"
2021
"strings"
2122
"sync"
2223
"time"
@@ -220,7 +221,10 @@ func makeRawMetric(prefix, name string, value interface{}, labels map[string]str
220221
fqName, label := nameAndLabel(prefix, name)
221222

222223
metricType := prometheus.UntypedValue
223-
if strings.HasSuffix(strings.ToLower(name), "count") {
224+
225+
// reservedPrefixes are used for metrics that might include the word ‘count’ in their name but are not actual counters.
226+
reservedPrefixes := []string{"collstats.storageStats.indexSizes."}
227+
if !slices.Contains(reservedPrefixes, prefix) && strings.HasSuffix(strings.ToLower(name), "count") {
224228
metricType = prometheus.CounterValue
225229
}
226230

0 commit comments

Comments
 (0)