Skip to content

Commit

Permalink
fix(): limit the number of collected collections refs MONITOR-5648
Browse files Browse the repository at this point in the history
  • Loading branch information
kestrelcjx committed Apr 15, 2023
1 parent 48ee29d commit 9d72572
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions metricbeat/module/mongodb/collstats/collstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error {
if err != nil {
return fmt.Errorf("ListDatabaseNames failed: %s", err)
}
collectedCollectionNum := 0
OutLoop:
for _, databaseName := range databaseNames {
database := client.Database(databaseName)
collectionNames, err := database.ListCollectionNames(context.Background(), bson.D{})
Expand Down Expand Up @@ -156,6 +158,10 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error {
reporter.Event(mb.Event{
MetricSetFields: event,
})
collectedCollectionNum += 1
if collectedCollectionNum >= m.Config.MaxCollectionNum {
break OutLoop
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions metricbeat/module/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ type ModuleConfig struct {

Database string `config:"database"`

Username string `config:"username"`
Password string `config:"password"`
Username string `config:"username"`
Password string `config:"password"`
MaxCollectionNum int `config:"max_collection_num"`

Credentials struct {
AuthMechanism string `config:"auth_mechanism"`
Expand Down Expand Up @@ -80,6 +81,9 @@ func NewMetricset(base mb.BaseMetricSet) (*Metricset, error) {
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, fmt.Errorf("could not read config: %w", err)
}
if config.MaxCollectionNum <= 0 {
config.MaxCollectionNum = 1000
}

return &Metricset{Config: config, BaseMetricSet: base}, nil
}
Expand Down

0 comments on commit 9d72572

Please sign in to comment.