Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MB mongodb module: connect on fetch, not on init #5120

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Fix a memory allocation issue where more memory was allocated than needed in the windows-perfmon metricset. {issue}5035[5035]
- Don't start metricbeat if external modules config is wrong and reload is disabled {pull}5053[5053]
- Fix kubernetes events module to be able to index time fields properly. {issue}5093[5093]
- The MongoDB module now connects on each fetch, to avoid stopping the whole Metricbeat instance if MongoDB is not up when starting. {pull}5120[5120]

*Packetbeat*

Expand Down
20 changes: 10 additions & 10 deletions metricbeat/module/mongodb/dbstats/dbstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
mongoSession *mgo.Session
dialInfo *mgo.DialInfo
}

// New creates a new instance of the MetricSet
Expand All @@ -43,15 +43,9 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}
dialInfo.Timeout = base.Module().Config().Timeout

// instantiate direct connections to each of the configured Mongo hosts
mongoSession, err := mongodb.NewDirectSession(dialInfo)
if err != nil {
return nil, err
}

return &MetricSet{
BaseMetricSet: base,
mongoSession: mongoSession,
dialInfo: dialInfo,
}, nil
}

Expand All @@ -62,16 +56,22 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {
// events is the list of events collected from each of the databases.
var events []common.MapStr

// instantiate direct connections to each of the configured Mongo hosts
mongoSession, err := mongodb.NewDirectSession(m.dialInfo)
if err != nil {
return nil, err
}

// Get the list of databases names, which we'll use to call db.stats() on each
dbNames, err := m.mongoSession.DatabaseNames()
dbNames, err := mongoSession.DatabaseNames()
if err != nil {
logp.Err("Error retrieving database names from Mongo instance")
return events, err
}

// for each database, call db.stats() and append to events
for _, dbName := range dbNames {
db := m.mongoSession.DB(dbName)
db := mongoSession.DB(dbName)

result := common.MapStr{}

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewDirectSession(dialInfo *mgo.DialInfo) (*mgo.Session, error) {
nodeDialInfo.Direct = true
nodeDialInfo.FailFast = true

logp.Info("Connecting to MongoDB node at %v", nodeDialInfo.Addrs)
logp.Debug("mongodb", "Connecting to MongoDB node at %v", nodeDialInfo.Addrs)

session, err := mgo.DialWithInfo(&nodeDialInfo)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions metricbeat/module/mongodb/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
mongoSession *mgo.Session
dialInfo *mgo.DialInfo
}

// New creates a new instance of the MetricSet
Expand All @@ -43,24 +43,25 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}
dialInfo.Timeout = base.Module().Config().Timeout

// instantiate direct connections to Mongo host
mongoSession, err := mongodb.NewDirectSession(dialInfo)
if err != nil {
return nil, err
}

return &MetricSet{
BaseMetricSet: base,
mongoSession: mongoSession,
dialInfo: dialInfo,
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch() (common.MapStr, error) {

// instantiate direct connections to each of the configured Mongo hosts
mongoSession, err := mongodb.NewDirectSession(m.dialInfo)
if err != nil {
return nil, err
}

result := map[string]interface{}{}
if err := m.mongoSession.DB("admin").Run(bson.D{{Name: "serverStatus", Value: 1}}, &result); err != nil {
if err := mongoSession.DB("admin").Run(bson.D{{Name: "serverStatus", Value: 1}}, &result); err != nil {
return nil, err
}

Expand Down