Skip to content

[9.3](backport #48638) metricbeat/module/mongodb/collstats: add $collStats aggregation support with fallback - #51469

Merged
shmsr merged 1 commit into
9.3from
mergify/bp/9.3/pr-48638
Jun 25, 2026
Merged

shmsr merged 1 commit into
9.3from
mergify/bp/9.3/pr-48638

Conversation

@mergify

@mergify mergify Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

This PR is a recreation of #46681. The original PR was automatically closed by GitHub when I deleted my fork (shmsr/beats) and created a new one. Since GitHub cannot reopen PRs from deleted forks (the source branch no longer exists), this new PR was created with the same commits and content. All previous review discussions and context are preserved in #46681 for reference.

Proposed commit message

Replaces the deprecated collstats command with the recommended aggregate pipeline using the $collStats stage. The collstats command was deprecated in MongoDB 6.2, and this change ensures compatibility with current and future MongoDB versions while providing enhanced collection statistics. Now it transparently uses either the deprecated collStats database command (legacy) or the $collStats aggregation stage (MongoDB 6.2+) depending on server version and feature availability to collect collection-level operational and storage statistics from MongoDB.

The new approach provides additional metrics that were not available in the old command. A few of them have been enabled, while the rest are currently disabled, pending further development to add them when a need arises.

Implementation wise, these are the following things added:

  • Version detection: Added MongoDB version detection via buildInfo command
  • Smart routing: Implemented version-aware method selection:
    • MongoDB 6.2+: Uses $collStats aggregation with fallback to legacy command
    • MongoDB <6.2: Uses legacy collStats command
  • Aggregation pipeline
    • Constructs $collStats pipeline with storageStats and count options
    • Handles scale parameter correctly
    • Supports sharded collections with result merging
  • Result flattening: Flattens nested storageStats to maintain compatibility with existing event mapping
  • Sharded collection support: Merges statistics from multiple shards with proper aggregation logic

References

Note: This is a recreation of #46681 after fork recreation.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Testing

Comprehensive testing performed across the MongoDB 6.2 deprecation boundary:

✅ MongoDB 5.0 (Pre-deprecation)

  • Backward compatibility verified
  • All core statistics captured correctly
  • New fields added (because of changed fields.yml)
  • No breaking changes to existing metrics

✅ MongoDB 7.0 (Post-deprecation)

  • Forward compatibility verified
  • Version-specific field numOrphanDocs correctly captured
  • Enhanced metrics all present
  • New fields correctly populated
  • Aggregation pipeline works as expected

✅ Cross-version Validation

  • Consistent behavior across MongoDB versions
  • Version-aware field handling (e.g., numOrphanDocs only in 6.0+)
  • Smart fallback mechanism tested
  • Sharded collection merging logic validated

Even scripts for future testing added — standalone, replica set and sharded setups. The same standalone setup is being used by the newly written integration tests that has tests against both 5.0 and 7.0.

Related issues

@mergify
mergify Bot requested a review from a team as a code owner June 23, 2026 19:55
@mergify mergify Bot added the backport label Jun 23, 2026
@mergify
mergify Bot requested a review from a team as a code owner June 23, 2026 19:55
@mergify mergify Bot assigned shmsr Jun 23, 2026
@botelastic botelastic Bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jun 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

@github-actions github-actions Bot added the Team:Obs-InfraObs Label for the Observability Infrastructure Monitoring team label Jun 23, 2026
@botelastic botelastic Bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Jun 23, 2026
@mergify

mergify Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@shmsr
shmsr merged commit f07b748 into 9.3 Jun 25, 2026
44 of 45 checks passed
@shmsr
shmsr deleted the mergify/bp/9.3/pr-48638 branch June 25, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Team:Obs-InfraObs Label for the Observability Infrastructure Monitoring team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants