From 638889d6501e853c7d5ea1e0d825dd214dd2ffaf Mon Sep 17 00:00:00 2001 From: simonswu Date: Fri, 8 Sep 2023 00:03:49 +0800 Subject: [PATCH] chore(): add threshold for MongoDB collstats metric set --- metricbeat/module/mongodb/collstats/collstats.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/metricbeat/module/mongodb/collstats/collstats.go b/metricbeat/module/mongodb/collstats/collstats.go index cf042834abd..eeda54596c2 100644 --- a/metricbeat/module/mongodb/collstats/collstats.go +++ b/metricbeat/module/mongodb/collstats/collstats.go @@ -101,12 +101,15 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error { if !ok { return errors.New("collection 'totals' are not a map") } - + collectedCollectionDataNum := 0 for group, info := range totals { if group == "note" { continue } - + // 如果已采集的collection的数量大于定义的,那么不再采集数据 + if collectedCollectionDataNum >= m.Config.MaxCollectionNum { + break + } infoMap, ok := info.(map[string]interface{}) if !ok { reporter.Error(errors.New("unexpected data returned by mongodb")) @@ -122,6 +125,7 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error { reporter.Event(mb.Event{ MetricSetFields: event, }) + collectedCollectionDataNum += 1 } databaseNames, err := client.ListDatabaseNames(context.Background(), bson.D{}) @@ -138,6 +142,9 @@ OutLoop: continue } for _, collectionName := range collectionNames { + if collectedCollectionNum >= m.Config.MaxCollectionNum { + break OutLoop + } res = db.RunCommand(context.Background(), bson.D{bson.E{Key: "collStats", Value: collectionName}}) if err = res.Err(); err != nil { m.Logger().Errorf("'collStats %s' command returned an error: %w", collectionName, err) @@ -159,9 +166,7 @@ OutLoop: MetricSetFields: event, }) collectedCollectionNum += 1 - if collectedCollectionNum >= m.Config.MaxCollectionNum { - break OutLoop - } + } }