Skip to content

Commit

Permalink
Fix issue with DirectConnect (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
masaruhoshi authored Mar 25, 2021
1 parent 5f229a8 commit 1b59991
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Exporter struct {
type Opts struct {
CompatibleMode bool
GlobalConnPool bool
DirectConnect bool
URI string
Path string
WebListenAddress string
Expand Down Expand Up @@ -79,7 +80,7 @@ func New(opts *Opts) (*Exporter, error) {
}
if opts.GlobalConnPool {
var err error
exp.client, err = connect(ctx, opts.URI)
exp.client, err = connect(ctx, opts.URI, opts.DirectConnect)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,7 +162,7 @@ func (e *Exporter) handler() http.Handler {
// Use per-request connection.
if !e.opts.GlobalConnPool {
var err error
client, err = connect(ctx, e.opts.URI)
client, err = connect(ctx, e.opts.URI, e.opts.DirectConnect)
if err != nil {
e.logger.Errorf("Cannot connect to MongoDB: %v", err)
http.Error(
Expand Down Expand Up @@ -214,9 +215,9 @@ func (e *Exporter) Run() {
exporter_shared.RunServer("MongoDB", e.webListenAddress, e.path, handler)
}

func connect(ctx context.Context, dsn string) (*mongo.Client, error) {
func connect(ctx context.Context, dsn string, directConnect bool) (*mongo.Client, error) {
clientOpts := options.Client().ApplyURI(dsn)
clientOpts.SetDirect(true)
clientOpts.SetDirect(directConnect)
clientOpts.SetAppName("mongodb_exporter")

client, err := mongo.Connect(ctx, clientOpts)
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestConnect(t *testing.T) {
t.Run("Connect without SSL", func(t *testing.T) {
for name, port := range ports {
dsn := fmt.Sprintf("mongodb://%s:%s/admin", hostname, port)
client, err := connect(ctx, dsn)
client, err := connect(ctx, dsn, true)
assert.NoError(t, err, name)
err = client.Disconnect(ctx)
assert.NoError(t, err, name)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type GlobalFlags struct {
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"`
GlobalConnPool bool `name:"mongodb.global-conn-pool" help:"Use global connection pool instead of creating new pool for each http request."`
DirectConnect bool `name:"mongodb.direct-connect" help:"Whether or not a direct connect should be made. Direct connections are not valid if multiple hosts are specified or an SRV URI is used." default:"false"`
WebListenAddress string `name:"web.listen-address" help:"Address to listen on for web interface and telemetry" default:":9216"`
WebTelemetryPath string `name:"web.telemetry-path" help:"Metrics expose path" default:"/metrics"`
LogLevel string `name:"log.level" help:"Only log messages with the given severuty or above. Valid levels: [debug, info, warn, error, fatal]" enum:"debug,info,warn,error,fatal" default:"error"`
Expand Down Expand Up @@ -113,6 +114,7 @@ func buildExporter(opts GlobalFlags) (*exporter.Exporter, error) {
WebListenAddress: opts.WebListenAddress,
DisableDiagnosticData: opts.DisableDiagnosticData,
DisableReplicasetStatus: opts.DisableReplicasetStatus,
DirectConnect: opts.DirectConnect,
}

e, err := exporter.New(exporterOpts)
Expand Down

0 comments on commit 1b59991

Please sign in to comment.