diff --git a/exporter/collstats_collector.go b/exporter/collstats_collector.go index 1636f896..f8321de5 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 eeb47cfd..039de2aa 100644 --- a/exporter/common.go +++ b/exporter/common.go @@ -18,6 +18,7 @@ package exporter import ( "context" "fmt" + "log/slog" "sort" "strings" @@ -163,8 +164,8 @@ func unique(slice []string) []string { return list } -func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collections []string) ([]string, error) { - onlyCollectionsNamespaces, err := listAllCollections(ctx, client, nil, nil, true) +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,7 +184,10 @@ 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) + if logger != nil { + logger.Warn("namespace is a view or does not exist, cannot be used for collstats/indexstats", "namespace", collection) + } + continue } filteredCollections = append(filteredCollections, collection) diff --git a/exporter/common_test.go b/exporter/common_test.go index eacd6469..3c9dbda5 100644 --- a/exporter/common_test.go +++ b/exporter/common_test.go @@ -198,13 +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) { - _, 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") + 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"}, 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 e2e6f0ab..2cd4a05d 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())