Skip to content

Commit

Permalink
improve compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
naughtyGitCat committed Dec 12, 2022
1 parent 5419d8e commit b793c84
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var (

// GlobalFlags has command line flags to configure the exporter.
type GlobalFlags struct {
User string `name:"mongodb.user" help:"monitor user, need clusterMonitor role in admin db and read role in local db" env:"MONGODB_USER" placeholder:"monitor" default:"monitor"`
Password string `name:"mongodb.password" help:"monitor user password" env:"MONGODB_PASSWORD" placeholder:"123456"`
User string `name:"mongodb.user" help:"monitor user, need clusterMonitor role in admin db and read role in local db" env:"MONGODB_USER" placeholder:"monitorUser"`
Password string `name:"mongodb.password" help:"monitor user password" env:"MONGODB_PASSWORD" placeholder:"monitorPassword"`
CollStatsNamespaces string `name:"mongodb.collstats-colls" help:"List of comma separared databases.collections to get $collStats" placeholder:"db1,db2.col2"`
IndexStatsCollections string `name:"mongodb.indexstats-colls" help:"List of comma separared databases.collections to get $indexStats" placeholder:"db1.col1,db2.col2"`
URI string `name:"mongodb.uri" help:"MongoDB connection URI" env:"MONGODB_URI" placeholder:"mongodb://user:[email protected]:27017/admin?ssl=true"`
Expand Down Expand Up @@ -105,16 +105,17 @@ func buildExporter(opts GlobalFlags) *exporter.Exporter {

log.Debugf("Compatible mode: %v", opts.CompatibleMode)

if strings.HasPrefix(opts.URI, "mongodb") {
// trim mongodb:// prefix to add user and pass
// IF user@pass not contained in uri AND custom user and pass supplied in arguments
// DO concat a new uri with user and pass arguments value
if !strings.Contains(opts.URI, "@") && opts.User != "" && opts.Password != "" {
// trim mongodb:// prefix to handle user and pass logic
opts.URI = strings.TrimPrefix(opts.URI, "mongodb://")
}

if !strings.Contains(opts.URI, "@") {
log.Debugf("add user and pass to the uri")
if !strings.HasPrefix(opts.URI, "mongodb") {
opts.URI = fmt.Sprintf("mongodb://%s:%s@%s", opts.User, opts.Password, opts.URI)
}
opts.URI = fmt.Sprintf("%s:%s@%s", opts.User, opts.Password, opts.URI)

// add back mongodb://
opts.URI = "mongodb://" + opts.URI
}

log.Debugf("Connection URI: %s", opts.URI)
Expand Down

0 comments on commit b793c84

Please sign in to comment.