Skip to content

Conversation

@jhatler
Copy link

@jhatler jhatler commented Nov 21, 2025

What does this PR do?

Backports the MongoDB hosts array support from main branch (PR #838, commit 9472fc7) to the 3.x branch to fix broken connection strings when using modern MongoDB configuration format.

This resolves the issue where users providing modern hosts array parameters get malformed MongoDB URIs like mongodb://user:pass@:/admin (missing host:port).

Motivation

Problem:
Organizations running the 3.x branch experience broken MongoDB monitoring when using modern configuration format. The template expects legacy host/port parameters but receives hosts array, causing nil values that produce malformed connection strings.

Impact:

  • MongoDB monitoring fails with invalid connection URIs
  • Users must use deprecated host/port format (inconsistent with current Datadog documentation)
  • Modern MongoDB features (DBM, database autodiscovery) unavailable on 3.x

Why backport:

  • The fix exists and is proven in main branch (merged Feb 26, 2025)
  • There are production environments still on 3.x (less than 1 year since 4.0 released)
  • Low risk, high value for 3.x users
  • Maintains backward compatibility with legacy configurations

Additional Notes

Changes included in this backport:

  1. Template (templates/agent-conf.d/mongo.yaml.erb):

    • Dual-path rendering: checks if hosts parameter exists
    • Modern path: renders hosts array format (lines 7-21)
    • Legacy path: renders connection string format (lines 22-24)
    • Adds rendering for dbm, database_autodiscovery, reported_database_hostname
  2. Manifest (manifests/integrations/mongo.pp):

    • Documents new parameters: hosts, dbm, database_autodiscovery, reported_database_hostname
    • Deprecates host and port parameters in favor of hosts array
    • Updates examples to show modern format
    • Adds reference to official Datadog integration documentation
  3. Tests (spec/classes/datadog_agent_integrations_mongo_spec.rb):

    • Adds test context for hosts array configuration
    • Verifies modern format renders correctly
    • Tests all new parameters (dbm, database_autodiscovery, reported_database_hostname)
    • Maintains backward compatibility (existing tests unchanged)
  4. CHANGELOG (CHANGELOG.md):

    • Documents backport in Unreleased section

Backward Compatibility:
✅ Fully backward compatible - legacy host/port configurations continue to work unchanged

Original implementation:

Describe your test plan

Automated Testing:

  • ✅ pre-commit checks: PASSED
  • pdk validate --parallel: PASSED (only expected warnings for undocumented classes)
  • ✅ Full RSpec suite: 4895 examples, 0 failures, 63 pending
  • ✅ Syntax validation: PASSED
  • ✅ New tests verify hosts array format renders correctly
  • ✅ Existing tests verify backward compatibility maintained

Test scenarios covered:

  1. Default parameters (legacy format) - existing test
  2. Legacy host/port format - existing tests
  3. NEW: Modern hosts array format - new test
  4. NEW: New parameters (dbm, database_autodiscovery, reported_database_hostname) - new test
  5. Multiple servers - existing tests
  6. Collections and additional_metrics - existing tests

Backport MongoDB integration fix from main branch to resolve broken
connection strings when using modern configuration format with hosts
array instead of legacy host/port parameters.

The current 3.x template generates malformed MongoDB URIs
(mongodb://user:pass@:/admin with missing host:port) when users
provide modern 'hosts' array parameters. This occurs because the
template expects old-style 'host' and 'port' keys but receives
'hosts' array from modern configurations, causing nil values in
the connection string.

Changes:
- Add dual-path template logic supporting both formats
- Modern configs with 'hosts' array use hosts format (works correctly)
- Legacy configs with 'host'/'port' use connection string (backward compatible)
- Add support for new MongoDB monitoring parameters:
  - dbm: Database Monitoring feature
  - database_autodiscovery: Auto-discovery of databases
  - reported_database_hostname: Custom hostname for metrics
- Update documentation to deprecate host/port in favor of hosts
- Add RSpec tests for modern hosts array format
- Update CHANGELOG for unreleased changes

Template changes:
- Check if server['hosts'] exists and has elements
- If yes: render modern hosts array format with fields
- If no: fall back to legacy connection string format
- Add dbm, database_autodiscovery, reported_database_hostname rendering

Documentation changes:
- Move hosts, dbm, database_autodiscovery, reported_database_hostname to top
- Mark host and port as deprecated with recommendation to use hosts
- Update examples to show modern hosts array format
- Add link to official Datadog MongoDB integration docs

Test changes:
- Add test context for hosts array configuration
- Verify hosts array renders correctly
- Verify all new parameters render correctly
- Maintain backward compatibility with existing tests

Tested:
- pdk validate: PASSED
- Full test suite: 4895 examples, 0 failures, 63 pending
- Syntax validation: PASSED

Refs: 9472fc7
Refs: DataDog#838
Signed-off-by: Jaremy Hatler <[email protected]>
@jhatler jhatler requested a review from a team as a code owner November 21, 2025 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant