Skip to content

Bug report of checkNamespacesForViews function #1144

@LinPr

Description

@LinPr

The checkNamespacesForViews function implimentation works well as what I expected when there are mulitiple mongo instances.

func checkNamespacesForViews(ctx context.Context, client *mongo.Client, collections []string) ([]string, error) {

If there are multiple mongo instance uri was setted, and also multiple collections setted, for exampele:

./mongodb_exporter \
	--mongodb.uri=mongodb://admin:[email protected]:27017,mongodb://admin:[email protected]:27017,mongodb://admin:[email protected]:27017 \
	--collector.dbstats=true \
	--collector.collstats=true \
	--collector.collstats-enable-details=true \
	--mongodb.collstats-colls=db1.collection1,db1.collection2,db2.collection2,db3.collection3 \
	--log.level=info \
	--split-cluster=true

which meanes I want to collect metrics from different collections in different databases..

But after the mongo_expoter start running , and try to collect metrics with curl or through prometheus, the process will be returned from the checkNamespacesForViews with a error message, just returned from here inside the function.
https://github.com/percona/mongodb_exporter/blob/ba39863aee6812fb89f5efda47ff446bff8827d1/exporter/common.go#L186C11-L186C14
The error will be triggered when one instances does not have db1.collection1 or other namespace, then the whole process will be interrupted And when you try to fetch the metrics, there is nothing returned. because the process was returned already, and you will never make the multi-instance mechanism work, because there are alway different namspaces in different instances, if only this situation exist, this error will be triggered

Well , I don't think this is the best implementation, the process should not be returned , but just need to skip the non-exist namespace. and continue to process the rest namespaces, add them to filteredCollections, those namespace metrics will be expoed to the outside world

	filteredCollections := []string{}
	for _, collection := range removeEmptyStrings(collections) {
		if len(strings.Split(collection, ".")) < 2 { //nolint:gomnd
			continue
		}

		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)
	}

Just like this, use continue instead of return nil, errors.Errorf("namespace %s is a view and cannot be used for collstats/indexstats", collection)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions