Skip to content

Commit 2ac37cb

Browse files
committed
Fix: Skip the non-exist namespaces when there are multiple mongo instance metrics to scrape
Signed-off-by: LinPr <[email protected]>
1 parent b70a24b commit 2ac37cb

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

exporter/collstats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (d *collstatsCollector) collect(ch chan<- prometheus.Metric) {
7878
collections = fromMapToSlice(onlyCollectionsNamespaces)
7979
} else {
8080
var err error
81-
collections, err = checkNamespacesForViews(d.ctx, client, d.collections)
81+
collections, err = checkNamespacesForViewsOrNonExist(d.ctx, client, d.collections, logger)
8282
if err != nil {
8383
logger.Error("cannot list collections", "error", err.Error())
8484
return

exporter/common.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package exporter
1818
import (
1919
"context"
2020
"fmt"
21+
"log/slog"
2122
"sort"
2223
"strings"
2324

@@ -163,7 +164,7 @@ func unique(slice []string) []string {
163164
return list
164165
}
165166

166-
func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collections []string) ([]string, error) {
167+
func checkNamespacesForViewsOrNonExist(ctx context.Context, client *mongo.Client, collections []string, logger *slog.Logger) ([]string, error) {
167168
onlyCollectionsNamespaces, err := listAllCollections(ctx, client, collections, nil, true)
168169
if err != nil {
169170
return nil, err
@@ -183,6 +184,9 @@ func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collecti
183184
}
184185

185186
if _, ok := namespaces[collection]; !ok {
187+
if logger != nil {
188+
logger.Warn("namespace is a view or does not exist, cannot be used for collstats/indexstats", "namespace", collection)
189+
}
186190
continue
187191
}
188192

exporter/common_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ func TestCheckNamespacesForViews(t *testing.T) {
198198
setupDB(ctx, t, client)
199199
defer cleanupDB(ctx, client)
200200

201-
t.Run("Views in provided collection list (should fail)", func(t *testing.T) {
202-
filtered, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.view01"})
201+
t.Run("Views or non-exist namespace in provided collection list (should fail)", func(t *testing.T) {
202+
filtered, err := checkNamespacesForViewsOrNonExist(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.non_existent"}, nil)
203203
assert.NoError(t, err)
204-
assert.Equal(t, []string{"testdb01.col01", "testdb01.system.views"}, filtered)
204+
assert.Equal(t, []string{"testdb01.col01"}, filtered)
205205
})
206206

207207
t.Run("No Views in provided collection list", func(t *testing.T) {
208-
filtered, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views"})
208+
filtered, err := checkNamespacesForViewsOrNonExist(ctx, client, []string{"testdb01.col01", "testdb01.system.views"}, nil)
209209
assert.NoError(t, err)
210210
assert.Equal(t, []string{"testdb01.col01", "testdb01.system.views"}, filtered)
211211
})

exporter/indexstats_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (d *indexstatsCollector) collect(ch chan<- prometheus.Metric) {
7777
collections = fromMapToSlice(onlyCollectionsNamespaces)
7878
} else {
7979
var err error
80-
collections, err = checkNamespacesForViews(d.ctx, client, d.collections)
80+
collections, err = checkNamespacesForViewsOrNonExist(d.ctx, client, d.collections, logger)
8181
if err != nil {
8282
logger.Error("cannot list collections", "error", err.Error())
8383

0 commit comments

Comments
 (0)