Skip to content

Commit

Permalink
feat(mongodb): add skip_ping_at_init option
Browse files Browse the repository at this point in the history
  • Loading branch information
observeralone committed Jun 17, 2022
1 parent 579ae4b commit f315c61
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions plugins/inputs/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ All MongoDB server versions from 2.6 and higher are supported.
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

## Skip Ping at initialization
## If you are not sure that your mongodb is already running, skipping the ping operation will allow you to run Telegraf normally and mongodb will try to reconnect by itself
# skip_ping_at_init = false
```

### Permissions
Expand Down
8 changes: 5 additions & 3 deletions plugins/inputs/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type MongoDB struct {
GatherPerdbStats bool
GatherColStats bool
GatherTopStat bool
SkipPingAtInit bool
ColStatsDbs []string
tlsint.ClientConfig

Expand Down Expand Up @@ -140,9 +141,10 @@ func (m *MongoDB) Init() error {
return fmt.Errorf("unable to connect to MongoDB: %q", err)
}

err = client.Ping(ctx, opts.ReadPreference)
if err != nil {
return fmt.Errorf("unable to connect to MongoDB: %s", err)
if m.SkipPingAtInit {
m.Log.Infof("skip ping at initialization to MongoDB: %q", connURL)
} else if err := client.Ping(ctx, opts.ReadPreference); err != nil {
return fmt.Errorf("unable to ping to MongoDB: %s", err)
}

server := &Server{
Expand Down
20 changes: 20 additions & 0 deletions plugins/inputs/mongodb/mongodb_skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mongodb

import (
"testing"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)

func TestMongodb_SkipPingAtInit(t *testing.T) {
m := &MongoDB{
Log: testutil.Logger{},
Servers: []string{"mongodb://aaa:[email protected]:37017/adminaaa"},
}
err := m.Init()
assert.Error(t, err)
m.SkipPingAtInit = true
err = m.Init()
assert.NoError(t, err)
}
37 changes: 37 additions & 0 deletions plugins/inputs/mongodb/sample.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Read metrics from one or many MongoDB servers
[[inputs.mongodb]]
## An array of URLs of the form:
## "mongodb://" [user ":" pass "@"] host [ ":" port]
## For example:
## mongodb://user:[email protected]:27017,
## mongodb://10.10.3.33:18832,
servers = ["mongodb://127.0.0.1:27017/?connect=direct"]

## When true, collect cluster status.
## Note that the query that counts jumbo chunks triggers a COLLSCAN, which
## may have an impact on performance.
# gather_cluster_status = true

## When true, collect per database stats
# gather_perdb_stats = false

## When true, collect per collection stats
# gather_col_stats = false

## When true, collect usage statistics for each collection
## (insert, update, queries, remove, getmore, commands etc...).
# gather_top_stat = false

## List of db where collections stats are collected
## If empty, all db are concerned
# col_stats_dbs = ["local"]

## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

## Skip Ping at initialization
# skip_ping_at_init = true

0 comments on commit f315c61

Please sign in to comment.