From 61d482208a650ee8b44e0bff8232d7948e1610fc Mon Sep 17 00:00:00 2001 From: zhangruizhi Date: Fri, 1 Aug 2025 21:23:41 +0800 Subject: [PATCH 1/2] disable coll chunk info when CollStatsLimit happens --- exporter/exporter.go | 2 +- exporter/shards_collector.go | 19 ++++++++++++------- main.go | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/exporter/exporter.go b/exporter/exporter.go index d69ddff0a..eb11cba8c 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -249,7 +249,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol registry.MustRegister(rsgsc) } if e.opts.EnableShards && nodeType == typeMongos && requestOpts.EnableShards { - sc := newShardsCollector(ctx, client, e.opts.Logger, e.opts.CompatibleMode) + sc := newShardsCollector(ctx, client, e.opts.Logger, e.opts.CompatibleMode, limitsOk) registry.MustRegister(sc) } diff --git a/exporter/shards_collector.go b/exporter/shards_collector.go index e204baa2f..f450034bd 100644 --- a/exporter/shards_collector.go +++ b/exporter/shards_collector.go @@ -29,17 +29,19 @@ import ( ) type shardsCollector struct { - ctx context.Context - base *baseCollector - compatible bool + ctx context.Context + base *baseCollector + compatible bool + scanEachCollChunkOk bool } // newShardsCollector creates collector collecting metrics about chunks for shards Mongo. -func newShardsCollector(ctx context.Context, client *mongo.Client, logger *slog.Logger, compatibleMode bool) *shardsCollector { +func newShardsCollector(ctx context.Context, client *mongo.Client, logger *slog.Logger, compatibleMode bool, scanEachCollChunkOk bool) *shardsCollector { return &shardsCollector{ - ctx: ctx, - base: newBaseCollector(client, logger.With("collector", "shards")), - compatible: compatibleMode, + ctx: ctx, + base: newBaseCollector(client, logger.With("collector", "shards")), + compatible: compatibleMode, + scanEachCollChunkOk: scanEachCollChunkOk, } } @@ -82,6 +84,9 @@ func (d *shardsCollector) collect(ch chan<- prometheus.Metric) { if err != nil { logger.Error("cannot get database names", "error", err) } + if !d.scanEachCollChunkOk { + return + } for _, database := range databaseNames { collections := d.getCollectionsForDBName(database) for _, row := range collections { diff --git a/main.go b/main.go index 4eae7774a..708b3f61e 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ type GlobalFlags struct { CollectAll bool `name:"collect-all" help:"Enable all collectors. Same as specifying all --collector."` - CollStatsLimit int `name:"collector.collstats-limit" help:"Disable collstats, dbstats, topmetrics and indexstats collector if there are more than collections. 0=No limit" default:"0"` + CollStatsLimit int `name:"collector.collstats-limit" help:"Disable collstats, dbstats, topmetrics, indexstats collector and coll chunk info if there are more than collections. 0=No limit" default:"0"` CollStatsEnableDetails bool `name:"collector.collstats-enable-details" help:"Enable collecting index details and wired tiger metrics from $collStats" default:"false"` ProfileTimeTS int `name:"collector.profile-time-ts" help:"Set time for scrape slow queries." default:"30"` From 8712438c39c4072c701964d6148233e539471be9 Mon Sep 17 00:00:00 2001 From: zhangruizhi Date: Mon, 4 Aug 2025 11:02:58 +0800 Subject: [PATCH 2/2] fix check logic, disable coll chunk scrape before `ListDatabaseNames`, add arg in `TestShardsCollector` to fit ctor args change --- exporter/shards_collector.go | 7 ++++--- exporter/shards_collector_test.go | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/exporter/shards_collector.go b/exporter/shards_collector.go index f450034bd..728ef572e 100644 --- a/exporter/shards_collector.go +++ b/exporter/shards_collector.go @@ -80,13 +80,14 @@ func (d *shardsCollector) collect(ch chan<- prometheus.Metric) { ch <- metric } + if !d.scanEachCollChunkOk { + return + } + databaseNames, err := client.ListDatabaseNames(d.ctx, bson.D{}) if err != nil { logger.Error("cannot get database names", "error", err) } - if !d.scanEachCollChunkOk { - return - } for _, database := range databaseNames { collections := d.getCollectionsForDBName(database) for _, row := range collections { diff --git a/exporter/shards_collector_test.go b/exporter/shards_collector_test.go index 4900b4ded..53c2213f2 100644 --- a/exporter/shards_collector_test.go +++ b/exporter/shards_collector_test.go @@ -35,7 +35,7 @@ func TestShardsCollector(t *testing.T) { defer cancel() client := tu.DefaultTestClientMongoS(ctx, t) - c := newShardsCollector(ctx, client, promslog.New(&promslog.Config{}), false) + c := newShardsCollector(ctx, client, promslog.New(&promslog.Config{}), false, true) reg := prometheus.NewPedanticRegistry() if err := reg.Register(c); err != nil {