Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion exporter/collstats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions exporter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package exporter
import (
"context"
"fmt"
"log/slog"
"sort"
"strings"

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's log that we are skipping this collection

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good idea

}

filteredCollections = append(filteredCollections, collection)
Expand Down
9 changes: 5 additions & 4 deletions exporter/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion exporter/indexstats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down