Skip to content

Commit

Permalink
Add direct connection to mongod plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
denispershin authored and Denis Pershin committed Nov 11, 2021
1 parent 488568c commit 6a6dab6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ All MongoDB server versions from 2.6 and higher are supported.
## List of db where collections stats are collected
## If empty, all db are concerned
# col_stats_dbs = ["local"]

## Direct connect to mongod node that included in replcaset and collect stats exactly from this node.
## If you disable this option, telegraf will collect stats only from primary node, even if you specify secondary node in "servers" section.
## Default value true, if you want to disable direct_connect option, you have to set this option to false
# direct_connect = true

## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
Expand Down
25 changes: 25 additions & 0 deletions plugins/inputs/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type MongoDB struct {
GatherColStats bool
GatherTopStat bool
ColStatsDbs []string
DirectConnect bool
tlsint.ClientConfig

Log telegraf.Logger `toml:"-"`
Expand Down Expand Up @@ -65,6 +66,11 @@ var sampleConfig = `
## If empty, all db are concerned
# col_stats_dbs = ["local"]
## Direct connect to mongod node that included in replcaset and collect stats exactly from this node.
## If you disable this option, telegraf will collect stats only from primary node, even if you specify secondary node in "servers" section.
## Default value is true, if you want to disable direct_connect option, you need set this option to false
# direct_connect = true
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
Expand Down Expand Up @@ -124,6 +130,24 @@ func (m *MongoDB) Init() error {
return fmt.Errorf("unable to parse connection URL: %q", err)
}

if m.DirectConnect {
if strings.Contains(connURL, "replicaSet") {
m.Log.Warn("DirectConnection is set true but connection string contains replicaSet parameter")
} else if strings.Contains(connURL, "directConnection") || strings.Contains(connURL, "connect=direct") {
m.Log.Warn("DirectConnection is set to true but it seems you already use direct connection option in your connection string")
} else if len(strings.Split(u.Host, ",")) > 1 {
m.Log.Warn("DirectConnection is set to true but a direct connection cannot be made if multiple hosts are specified")
} else {
if u.Path == "" {
u.Path = "/"
}
query, _ := url.ParseQuery(u.RawQuery)
query.Add("directConnection", "true")
u.RawQuery = query.Encode()
connURL = u.String()
}
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() //nolint:revive

Expand Down Expand Up @@ -183,6 +207,7 @@ func init() {
GatherColStats: false,
GatherTopStat: false,
ColStatsDbs: []string{"local"},
DirectConnect: true,
}
})
}

0 comments on commit 6a6dab6

Please sign in to comment.