From 23c3c41e74b919e5b8c81b5eb85dee6e25727db7 Mon Sep 17 00:00:00 2001 From: LinPr <314573849@qq.com> Date: Wed, 13 Aug 2025 20:49:22 +0800 Subject: [PATCH 1/2] Fix: Skip the non-exist namespaces when there are multiple mongo instance metrics to scrape Signed-off-by: LinPr <314573849@qq.com> --- exporter/common.go | 4 ++-- exporter/common_test.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/exporter/common.go b/exporter/common.go index eeb47cfd4..1ad72ef11 100644 --- a/exporter/common.go +++ b/exporter/common.go @@ -164,7 +164,7 @@ func unique(slice []string) []string { } func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collections []string) ([]string, error) { - onlyCollectionsNamespaces, err := listAllCollections(ctx, client, nil, nil, true) + onlyCollectionsNamespaces, err := listAllCollections(ctx, client, collections, nil, true) if err != nil { return nil, err } @@ -183,7 +183,7 @@ func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collecti } if _, ok := namespaces[collection]; !ok { - return nil, errors.Errorf("namespace %s is a view and cannot be used for collstats/indexstats", collection) + continue } filteredCollections = append(filteredCollections, collection) diff --git a/exporter/common_test.go b/exporter/common_test.go index eacd6469a..fd1fe633e 100644 --- a/exporter/common_test.go +++ b/exporter/common_test.go @@ -199,8 +199,9 @@ func TestCheckNamespacesForViews(t *testing.T) { defer cleanupDB(ctx, client) t.Run("Views in provided collection list (should fail)", func(t *testing.T) { - _, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.view01"}) - assert.EqualError(t, err, "namespace testdb01.view01 is a view and cannot be used for collstats/indexstats") + filtered, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.view01"}) + assert.NoError(t, err) + assert.Equal(t, []string{"testdb01.col01", "testdb01.system.views"}, filtered) }) t.Run("No Views in provided collection list", func(t *testing.T) { From 2ac37cbef17957c0e3c5a64812eafca8b8533a59 Mon Sep 17 00:00:00 2001 From: LinPr <314573849@qq.com> Date: Tue, 26 Aug 2025 22:18:05 +0800 Subject: [PATCH 2/2] Fix: Skip the non-exist namespaces when there are multiple mongo instance metrics to scrape Signed-off-by: LinPr <314573849@qq.com> --- exporter/collstats_collector.go | 2 +- exporter/common.go | 6 +++++- exporter/common_test.go | 8 ++++---- exporter/indexstats_collector.go | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/exporter/collstats_collector.go b/exporter/collstats_collector.go index 1636f8961..f8321de5e 100644 --- a/exporter/collstats_collector.go +++ b/exporter/collstats_collector.go @@ -78,7 +78,7 @@ func (d *collstatsCollector) collect(ch chan<- prometheus.Metric) { collections = fromMapToSlice(onlyCollectionsNamespaces) } else { var err error - collections, err = checkNamespacesForViews(d.ctx, client, d.collections) + collections, err = checkNamespacesForViewsOrNonExist(d.ctx, client, d.collections, logger) if err != nil { logger.Error("cannot list collections", "error", err.Error()) return diff --git a/exporter/common.go b/exporter/common.go index 1ad72ef11..039de2aa3 100644 --- a/exporter/common.go +++ b/exporter/common.go @@ -18,6 +18,7 @@ package exporter import ( "context" "fmt" + "log/slog" "sort" "strings" @@ -163,7 +164,7 @@ func unique(slice []string) []string { return list } -func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collections []string) ([]string, error) { +func checkNamespacesForViewsOrNonExist(ctx context.Context, client *mongo.Client, collections []string, logger *slog.Logger) ([]string, error) { onlyCollectionsNamespaces, err := listAllCollections(ctx, client, collections, nil, true) if err != nil { return nil, err @@ -183,6 +184,9 @@ func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collecti } if _, ok := namespaces[collection]; !ok { + if logger != nil { + logger.Warn("namespace is a view or does not exist, cannot be used for collstats/indexstats", "namespace", collection) + } continue } diff --git a/exporter/common_test.go b/exporter/common_test.go index fd1fe633e..3c9dbda5a 100644 --- a/exporter/common_test.go +++ b/exporter/common_test.go @@ -198,14 +198,14 @@ func TestCheckNamespacesForViews(t *testing.T) { setupDB(ctx, t, client) defer cleanupDB(ctx, client) - t.Run("Views in provided collection list (should fail)", func(t *testing.T) { - filtered, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.view01"}) + t.Run("Views or non-exist namespace in provided collection list (should fail)", func(t *testing.T) { + filtered, err := checkNamespacesForViewsOrNonExist(ctx, client, []string{"testdb01.col01", "testdb01.system.views", "testdb01.non_existent"}, nil) assert.NoError(t, err) - assert.Equal(t, []string{"testdb01.col01", "testdb01.system.views"}, filtered) + assert.Equal(t, []string{"testdb01.col01"}, filtered) }) t.Run("No Views in provided collection list", func(t *testing.T) { - filtered, err := checkNamespacesForViews(ctx, client, []string{"testdb01.col01", "testdb01.system.views"}) + filtered, err := checkNamespacesForViewsOrNonExist(ctx, client, []string{"testdb01.col01", "testdb01.system.views"}, nil) assert.NoError(t, err) assert.Equal(t, []string{"testdb01.col01", "testdb01.system.views"}, filtered) }) diff --git a/exporter/indexstats_collector.go b/exporter/indexstats_collector.go index e2e6f0abc..2cd4a05d6 100644 --- a/exporter/indexstats_collector.go +++ b/exporter/indexstats_collector.go @@ -77,7 +77,7 @@ func (d *indexstatsCollector) collect(ch chan<- prometheus.Metric) { collections = fromMapToSlice(onlyCollectionsNamespaces) } else { var err error - collections, err = checkNamespacesForViews(d.ctx, client, d.collections) + collections, err = checkNamespacesForViewsOrNonExist(d.ctx, client, d.collections, logger) if err != nil { logger.Error("cannot list collections", "error", err.Error())