Skip to content

Commit 7e7ec49

Browse files
committed
Backport MongoDB hosts array support to 3.x
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 <j.hatler@xsolla.com>
1 parent 7f93cb4 commit 7e7ec49

4 files changed

Lines changed: 81 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
# Unreleased
5+
6+
* [BUGFIX] Backport MongoDB hosts array support from main to fix broken connection strings with modern configuration format
7+
48
# 3.24.0 / 2025-02-25
59

610
* [BUGFIX] Fix incorrect SSL parameter data type for postgres integration ([#824])

manifests/integrations/mongo.pp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
#
33
# This class will install the necessary configuration for the mongo integration
44
#
5+
# See the sample mongo.d/conf.yaml for all available configuration options
6+
# https://github.com/DataDog/integrations-core/blob/master/mongo/datadog_checks/mongo/data/conf.yaml.example
7+
#
58
# NOTE: In newer versions of the Datadog Agent, the ssl parameters will be deprecated in favor the tls variants
69
#
710
# Parameters:
11+
# $hosts
12+
# Array of hosts host (and optional port number) where the mongod instance is running
13+
# $dbm
14+
# Enable the Database Monitoring feature
15+
# $database_autodiscovery
16+
# Enable the Database Autodiscovery feature
17+
# $reported_database_hostname
18+
# Optional database hostname override the mongodb hostname detected by the Agent from mongodb admin command serverStatus
819
# $additional_metrics
920
# Optional array of additional metrics
1021
# $database
1122
# Optionally specify database to query. Defaults to 'admin'
12-
# $host:
13-
# The host mongo is running on. Defaults to '127.0.0.1'
1423
# $password
1524
# Optionally specify password for connection
16-
# $port
17-
# The port mongo is running on. Defaults to 27017
1825
# $ssl
1926
# Optionally enable SSL for connection
2027
# $ssl_ca_certs
@@ -37,6 +44,12 @@
3744
# Optional array of tags
3845
# $username
3946
# Optionally specify username for connection
47+
# $host:
48+
# Deprecated use $hosts instead
49+
# The host mongo is running on. Defaults to '127.0.0.1'
50+
# $port
51+
# Deprecated use $hosts instead
52+
# The port mongo is running on. Defaults to 27017
4053
#
4154
# Sample Usage (Older Agent Versions):
4255
#
@@ -73,19 +86,20 @@
7386
# {
7487
# 'additional_metrics' => ['top'],
7588
# 'database' => 'database_name',
76-
# 'host' => 'localhost',
89+
# 'hosts' => ['localhost:27017'],
7790
# 'password' => 'mongo_password',
78-
# 'port' => '27017',
7991
# 'tls' => true,
8092
# 'tls_ca_file' => '/path/to/ca.pem',
8193
# 'tls_allow_invalid_certificates' => false,
8294
# 'tls_certificate_key_file' => '/path/to/combined.pem',
8395
# 'tags' => ['optional_tag1', 'optional_tag2'],
8496
# 'username' => 'mongo_username',
97+
# 'dbm' => true,
98+
# 'database_autodiscovery' => {'enabled' => true},
99+
# 'reported_database_hostname' => 'mymongodbhost',
85100
# },
86101
# {
87-
# 'host' => 'localhost',
88-
# 'port' => '27018',
102+
# 'hosts' => ['localhost:27017'],
89103
# 'tags' => [],
90104
# 'additional_metrics' => [],
91105
# 'collections' => [],

spec/classes/datadog_agent_integrations_mongo_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@
2727
it { is_expected.to contain_file(conf_file).without_content(%r{tags:}) }
2828
end
2929

30+
context 'with one mongo host defined in hosts array' do
31+
let(:params) do
32+
{
33+
servers: [
34+
{
35+
'hosts' => ['localhost:27017'],
36+
'username' => 'user',
37+
'password' => 'pass',
38+
'database' => 'admin',
39+
'dbm' => true,
40+
'database_autodiscovery' => { 'enabled' => true },
41+
'reported_database_hostname' => 'mongohost',
42+
},
43+
],
44+
}
45+
end
46+
47+
it { is_expected.to contain_file(conf_file).with_content(%r{- hosts:\s+- localhost:27017}) }
48+
it { is_expected.to contain_file(conf_file).with_content(%r{username: user}) }
49+
it { is_expected.to contain_file(conf_file).with_content(%r{password: pass}) }
50+
it { is_expected.to contain_file(conf_file).with_content(%r{database: admin}) }
51+
it { is_expected.to contain_file(conf_file).with_content(%r{dbm: true}) }
52+
it { is_expected.to contain_file(conf_file).with_content(%r{database_autodiscovery:\s+enabled: true}) }
53+
it { is_expected.to contain_file(conf_file).with_content(%r{reported_database_hostname: mongohost}) }
54+
end
55+
3056
context 'with one mongo' do
3157
let(:params) do
3258
{

templates/agent-conf.d/mongo.yaml.erb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ init_config:
44

55
instances:
66
<% @servers.each do |server| -%>
7+
<% if !server['hosts'].nil? && server['hosts'].any? -%>
8+
- hosts:
9+
<%- server['hosts'].each do |host| -%>
10+
- <%= host %>
11+
<%- end -%>
12+
<%- if !server['username'].nil? -%>
13+
username: <%= server['username'] %>
14+
<%- end -%>
15+
<%- if !server['password'].nil? -%>
16+
password: <%= server['password'] %>
17+
<%- end -%>
18+
<%- if !server['database'].nil? -%>
19+
database: <%= server['database'] %>
20+
<%- end -%>
21+
<%- end -%>
22+
<% if server['hosts'].nil? -%>
723
- server: mongodb://<%= server['username'] %><%= ":" unless server['password'].nil? %><%= server['password'] %><%= "@" unless server['username'].nil? %><%= server['host'] %>:<%= server['port'] %>/<%= server['database'] %>
24+
<%- end -%>
825
<%- if !server['tags'].nil? && server['tags'].any? -%>
926
tags:
1027
<%- server['tags'].each do |tag| -%>
@@ -50,4 +67,16 @@ instances:
5067
- <%= collection %>
5168
<%- end -%>
5269
<%- end -%>
70+
<%- if !server['dbm'].nil? -%>
71+
dbm: <%= server['dbm'] %>
72+
<%- end -%>
73+
<%- if !server['database_autodiscovery'].nil? -%>
74+
database_autodiscovery:
75+
<%- if !server['database_autodiscovery']['enabled'].nil? -%>
76+
enabled: <%= server['database_autodiscovery']['enabled'] %>
77+
<%- end -%>
78+
<%- end -%>
79+
<%- if !server['reported_database_hostname'].nil? -%>
80+
reported_database_hostname: <%= server['reported_database_hostname'] %>
81+
<%- end -%>
5382
<% end -%>

0 commit comments

Comments
 (0)