From 4912727e31b560814f7075ba0ac44443f866b88e Mon Sep 17 00:00:00 2001 From: Dmitry Anoshin Date: Thu, 20 Jun 2024 18:02:56 -0700 Subject: [PATCH] [chore] [receiver/mongodb] Remove redundant resource grouping The produced metrics are unnecessarily grouped by empty resources. This change removes the excessive grouping to keep only unique resources --- receiver/mongodbreceiver/scraper.go | 16 +-- .../testdata/integration/expected.4_0.yaml | 133 +++++++----------- .../testdata/integration/expected.4_4lpu.yaml | 61 +++----- .../testdata/integration/expected.5_0.yaml | 133 +++++++----------- .../testdata/scraper/expected.yaml | 110 +++++---------- 5 files changed, 163 insertions(+), 290 deletions(-) diff --git a/receiver/mongodbreceiver/scraper.go b/receiver/mongodbreceiver/scraper.go index 2e7772b41866..adf8df2bb4c4 100644 --- a/receiver/mongodbreceiver/scraper.go +++ b/receiver/mongodbreceiver/scraper.go @@ -98,7 +98,9 @@ func (s *mongodbScraper) collectMetrics(ctx context.Context, errs *scrapererror. s.mb.RecordMongodbDatabaseCountDataPoint(now, int64(len(dbNames))) s.collectAdminDatabase(ctx, now, errs) s.collectTopStats(ctx, now, errs) + s.mb.EmitForResource() + // Collect metrics for each database for _, dbName := range dbNames { s.collectDatabase(ctx, now, dbName, errs) collectionNames, err := s.client.ListCollectionNames(ctx, dbName) @@ -110,6 +112,10 @@ func (s *mongodbScraper) collectMetrics(ctx context.Context, errs *scrapererror. for _, collectionName := range collectionNames { s.collectIndexStats(ctx, now, dbName, collectionName, errs) } + + rb := s.mb.NewResourceBuilder() + rb.SetDatabase(dbName) + s.mb.EmitForResource(metadata.WithResource(rb.Emit())) } } @@ -127,10 +133,6 @@ func (s *mongodbScraper) collectDatabase(ctx context.Context, now pcommon.Timest return } s.recordNormalServerStats(now, serverStatus, databaseName, errs) - - rb := s.mb.NewResourceBuilder() - rb.SetDatabase(databaseName) - s.mb.EmitForResource(metadata.WithResource(rb.Emit())) } func (s *mongodbScraper) collectAdminDatabase(ctx context.Context, now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) { @@ -140,7 +142,6 @@ func (s *mongodbScraper) collectAdminDatabase(ctx context.Context, now pcommon.T return } s.recordAdminStats(now, serverStatus, errs) - s.mb.EmitForResource() } func (s *mongodbScraper) collectTopStats(ctx context.Context, now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) { @@ -150,7 +151,6 @@ func (s *mongodbScraper) collectTopStats(ctx context.Context, now pcommon.Timest return } s.recordOperationTime(now, topStats, errs) - s.mb.EmitForResource() } func (s *mongodbScraper) collectIndexStats(ctx context.Context, now pcommon.Timestamp, databaseName string, collectionName string, errs *scrapererror.ScrapeErrors) { @@ -163,10 +163,6 @@ func (s *mongodbScraper) collectIndexStats(ctx context.Context, now pcommon.Time return } s.recordIndexStats(now, indexStats, databaseName, collectionName, errs) - - rb := s.mb.NewResourceBuilder() - rb.SetDatabase(databaseName) - s.mb.EmitForResource(metadata.WithResource(rb.Emit())) } func (s *mongodbScraper) recordDBStats(now pcommon.Timestamp, doc bson.M, dbName string, errs *scrapererror.ScrapeErrors) { diff --git a/receiver/mongodbreceiver/testdata/integration/expected.4_0.yaml b/receiver/mongodbreceiver/testdata/integration/expected.4_0.yaml index aa0f3d2a42a1..45b3c8b4aa05 100644 --- a/receiver/mongodbreceiver/testdata/integration/expected.4_0.yaml +++ b/receiver/mongodbreceiver/testdata/integration/expected.4_0.yaml @@ -137,22 +137,6 @@ resourceMetrics: timeUnixNano: "1682363210513475000" isMonotonic: true unit: '{operations}' - - description: The total number of active sessions. - name: mongodb.session.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "19" - startTimeUnixNano: "1682363190502544000" - timeUnixNano: "1682363210513475000" - unit: '{sessions}' - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: - attributes: [] - scopeMetrics: - - metrics: - description: The total time spent performing operations. name: mongodb.operation.time sum: @@ -202,6 +186,15 @@ resourceMetrics: timeUnixNano: "1682363210513475000" isMonotonic: true unit: ms + - description: The total number of active sessions. + name: mongodb.session.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "19" + startTimeUnixNano: "1682363190502544000" + timeUnixNano: "1682363210513475000" + unit: '{sessions}' scope: name: otelcol/mongodbreceiver version: latest @@ -293,6 +286,19 @@ resourceMetrics: startTimeUnixNano: "1682363190502544000" timeUnixNano: "1682363210513475000" unit: '{extents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: system.version + startTimeUnixNano: "1682363190502544000" + timeUnixNano: "1682363210513475000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -353,29 +359,6 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: admin - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: system.version - startTimeUnixNano: "1682363190502544000" - timeUnixNano: "1682363210513475000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest - resource: attributes: - key: database @@ -464,6 +447,19 @@ resourceMetrics: startTimeUnixNano: "1682363190502544000" timeUnixNano: "1682363210513475000" unit: '{extents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: system.sessions + startTimeUnixNano: "1682363190502544000" + timeUnixNano: "1682363210513475000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -524,29 +520,6 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: config - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: system.sessions - startTimeUnixNano: "1682363190502544000" - timeUnixNano: "1682363210513475000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest - resource: attributes: - key: database @@ -783,6 +756,19 @@ resourceMetrics: startTimeUnixNano: "1682363190502544000" timeUnixNano: "1682363210513475000" unit: '{extents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: orders + startTimeUnixNano: "1682363190502544000" + timeUnixNano: "1682363210513475000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -843,26 +829,3 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: testdb - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: orders - startTimeUnixNano: "1682363190502544000" - timeUnixNano: "1682363210513475000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest diff --git a/receiver/mongodbreceiver/testdata/integration/expected.4_4lpu.yaml b/receiver/mongodbreceiver/testdata/integration/expected.4_4lpu.yaml index b6c3274c4698..bad4f2caccd3 100644 --- a/receiver/mongodbreceiver/testdata/integration/expected.4_4lpu.yaml +++ b/receiver/mongodbreceiver/testdata/integration/expected.4_4lpu.yaml @@ -137,22 +137,6 @@ resourceMetrics: timeUnixNano: "1682363222253814000" isMonotonic: true unit: '{operations}' - - description: The total number of active sessions. - name: mongodb.session.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "15" - startTimeUnixNano: "1682363202250964000" - timeUnixNano: "1682363222253814000" - unit: '{sessions}' - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: - attributes: [] - scopeMetrics: - - metrics: - description: The total time spent performing operations. name: mongodb.operation.time sum: @@ -202,6 +186,15 @@ resourceMetrics: timeUnixNano: "1682363222253814000" isMonotonic: true unit: ms + - description: The total number of active sessions. + name: mongodb.session.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "15" + startTimeUnixNano: "1682363202250964000" + timeUnixNano: "1682363222253814000" + unit: '{sessions}' scope: name: otelcol/mongodbreceiver version: latest @@ -701,6 +694,19 @@ resourceMetrics: startTimeUnixNano: "1682363202250964000" timeUnixNano: "1682363222253814000" unit: '{documents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: orders + startTimeUnixNano: "1682363202250964000" + timeUnixNano: "1682363222253814000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -761,26 +767,3 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: testdb - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: orders - startTimeUnixNano: "1682363202250964000" - timeUnixNano: "1682363222253814000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest diff --git a/receiver/mongodbreceiver/testdata/integration/expected.5_0.yaml b/receiver/mongodbreceiver/testdata/integration/expected.5_0.yaml index 5d1841f9c625..b9688d129705 100644 --- a/receiver/mongodbreceiver/testdata/integration/expected.5_0.yaml +++ b/receiver/mongodbreceiver/testdata/integration/expected.5_0.yaml @@ -137,22 +137,6 @@ resourceMetrics: timeUnixNano: "1682363210542990000" isMonotonic: true unit: '{operations}' - - description: The total number of active sessions. - name: mongodb.session.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "15" - startTimeUnixNano: "1682363190539043000" - timeUnixNano: "1682363210542990000" - unit: '{sessions}' - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: - attributes: [] - scopeMetrics: - - metrics: - description: The total time spent performing operations. name: mongodb.operation.time sum: @@ -202,6 +186,15 @@ resourceMetrics: timeUnixNano: "1682363210542990000" isMonotonic: true unit: ms + - description: The total number of active sessions. + name: mongodb.session.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "15" + startTimeUnixNano: "1682363190539043000" + timeUnixNano: "1682363210542990000" + unit: '{sessions}' scope: name: otelcol/mongodbreceiver version: latest @@ -284,6 +277,19 @@ resourceMetrics: startTimeUnixNano: "1682363190539043000" timeUnixNano: "1682363210542990000" unit: '{documents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: system.version + startTimeUnixNano: "1682363190539043000" + timeUnixNano: "1682363210542990000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -344,29 +350,6 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: admin - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: system.version - startTimeUnixNano: "1682363190539043000" - timeUnixNano: "1682363210542990000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest - resource: attributes: - key: database @@ -446,6 +429,19 @@ resourceMetrics: startTimeUnixNano: "1682363190539043000" timeUnixNano: "1682363210542990000" unit: '{documents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: system.sessions + startTimeUnixNano: "1682363190539043000" + timeUnixNano: "1682363210542990000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -506,29 +502,6 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: config - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: system.sessions - startTimeUnixNano: "1682363190539043000" - timeUnixNano: "1682363210542990000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest - resource: attributes: - key: database @@ -747,6 +720,19 @@ resourceMetrics: startTimeUnixNano: "1682363190539043000" timeUnixNano: "1682363210542990000" unit: '{documents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: orders + startTimeUnixNano: "1682363190539043000" + timeUnixNano: "1682363210542990000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -807,26 +793,3 @@ resourceMetrics: scope: name: otelcol/mongodbreceiver version: latest - - resource: - attributes: - - key: database - value: - stringValue: testdb - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: orders - startTimeUnixNano: "1682363190539043000" - timeUnixNano: "1682363210542990000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest diff --git a/receiver/mongodbreceiver/testdata/scraper/expected.yaml b/receiver/mongodbreceiver/testdata/scraper/expected.yaml index 0d60072a858a..811f67dbe7aa 100644 --- a/receiver/mongodbreceiver/testdata/scraper/expected.yaml +++ b/receiver/mongodbreceiver/testdata/scraper/expected.yaml @@ -219,31 +219,6 @@ resourceMetrics: timeUnixNano: "2000000" isMonotonic: true unit: '{operations}' - - description: The total number of active sessions. - name: mongodb.session.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "19" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - unit: '{sessions}' - - description: The amount of time that the server has been running. - name: mongodb.uptime - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "58965" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - isMonotonic: true - unit: ms - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: {} - scopeMetrics: - - metrics: - description: The total time spent performing operations. name: mongodb.operation.time sum: @@ -293,6 +268,25 @@ resourceMetrics: timeUnixNano: "2000000" isMonotonic: true unit: ms + - description: The total number of active sessions. + name: mongodb.session.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "19" + startTimeUnixNano: "1000000" + timeUnixNano: "2000000" + unit: '{sessions}' + - description: The amount of time that the server has been running. + name: mongodb.uptime + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "58965" + startTimeUnixNano: "1000000" + timeUnixNano: "2000000" + isMonotonic: true + unit: ms scope: name: otelcol/mongodbreceiver version: latest @@ -384,6 +378,26 @@ resourceMetrics: startTimeUnixNano: "1000000" timeUnixNano: "2000000" unit: '{extents}' + - description: The number of times an index has been accessed. + name: mongodb.index.access.count + sum: + aggregationTemporality: 2 + dataPoints: + - asInt: "0" + attributes: + - key: collection + value: + stringValue: products + startTimeUnixNano: "1000000" + timeUnixNano: "2000000" + - asInt: "2" + attributes: + - key: collection + value: + stringValue: orders + startTimeUnixNano: "1000000" + timeUnixNano: "2000000" + unit: '{accesses}' - description: The number of indexes. name: mongodb.index.count sum: @@ -441,52 +455,6 @@ resourceMetrics: timeUnixNano: "2000000" isMonotonic: true unit: By - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: - attributes: - - key: database - value: - stringValue: fakedatabase - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "0" - attributes: - - key: collection - value: - stringValue: products - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - unit: '{accesses}' - scope: - name: otelcol/mongodbreceiver - version: latest - - resource: - attributes: - - key: database - value: - stringValue: fakedatabase - scopeMetrics: - - metrics: - - description: The number of times an index has been accessed. - name: mongodb.index.access.count - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "2" - attributes: - - key: collection - value: - stringValue: orders - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - unit: '{accesses}' scope: name: otelcol/mongodbreceiver version: latest \ No newline at end of file