Skip to content

Exporter not initiating connection to mongo when Unauthorized #1011

@dudigit

Description

@dudigit

Describe the bug
When Exporter works with mongodb + Authentication, when Unauthorizes, it dose not renew session to mongodb and keep error about authentcation

To Reproduce
Steps to reproduce the behavior:

  1. Run mongodb with authentication
  2. Run exporter with the following cmd line
/bin/mongodb_exporter --collector.diagnosticdata --collector.replicasetstatus
      --compatible-mode --mongodb.direct-connect --mongodb.global-conn-pool --web.listen-address
      \":9216\" --mongodb.uri \"mongodb://$MONGODB_ROOT_USER:$(echo $MONGODB_ROOT_PASSWORD
      | sed -r \"s/@/%40/g;s/:/%3A/g;s/#/%23/g;s/\\\\$/%24/g\")@localhost:27017/admin?
  1. This issue happens sometime - not always - timing issue
    When exporter is running before the mongodb
    See many Unauthorized errors like:
    level=error msg="cannot decode getCmdLineOtpions: cannot execute getCmdLineOpts command: (Unauthorized) Command getCmdLineOpts requires authentication"

Expected behavior
When exporter is run with wrong credential is exited
I expect that the exporter will initiate it's session to mongo in case one of it's collector error with authentication
or at least exit

Logs
level=error msg="cannot decode getCmdLineOtpions: cannot execute getCmdLineOpts command: (Unauthorized) Command getCmdLineOpts requires authentication"

Environment

  • OS - Limux
  • environment - k8s
  • MongoDB 7

Additional context
I would offer to add some logic at each collector something like

res := client.Database("admin").RunCommand(d.ctx, cmd)
var r primitive.M
if err := res.Decode(&r); err != nil {

    if e, ok := err.(mongo.CommandError); ok {

        if e.Code == Unauthorized { // Unauthorized = 13 

            logger.Errorf("unauthorized to run currtop: %s", err)

            os.Exit(1)
            // Can handle with reinitiate the connection as well 

        }

    }

}

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