Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes
=======

# Unreleased

* [BUGFIX] Backport MongoDB hosts array support from main to fix broken connection strings with modern configuration format

# 3.24.0 / 2025-02-25

* [BUGFIX] Fix incorrect SSL parameter data type for postgres integration ([#824])
Expand Down
30 changes: 22 additions & 8 deletions manifests/integrations/mongo.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
#
# This class will install the necessary configuration for the mongo integration
#
# See the sample mongo.d/conf.yaml for all available configuration options
# https://github.com/DataDog/integrations-core/blob/master/mongo/datadog_checks/mongo/data/conf.yaml.example
#
# NOTE: In newer versions of the Datadog Agent, the ssl parameters will be deprecated in favor the tls variants
#
# Parameters:
# $hosts
# Array of hosts host (and optional port number) where the mongod instance is running
# $dbm
# Enable the Database Monitoring feature
# $database_autodiscovery
# Enable the Database Autodiscovery feature
# $reported_database_hostname
# Optional database hostname override the mongodb hostname detected by the Agent from mongodb admin command serverStatus
# $additional_metrics
# Optional array of additional metrics
# $database
# Optionally specify database to query. Defaults to 'admin'
# $host:
# The host mongo is running on. Defaults to '127.0.0.1'
# $password
# Optionally specify password for connection
# $port
# The port mongo is running on. Defaults to 27017
# $ssl
# Optionally enable SSL for connection
# $ssl_ca_certs
Expand All @@ -37,6 +44,12 @@
# Optional array of tags
# $username
# Optionally specify username for connection
# $host:
# Deprecated use $hosts instead
# The host mongo is running on. Defaults to '127.0.0.1'
# $port
# Deprecated use $hosts instead
# The port mongo is running on. Defaults to 27017
#
# Sample Usage (Older Agent Versions):
#
Expand Down Expand Up @@ -73,19 +86,20 @@
# {
# 'additional_metrics' => ['top'],
# 'database' => 'database_name',
# 'host' => 'localhost',
# 'hosts' => ['localhost:27017'],
# 'password' => 'mongo_password',
# 'port' => '27017',
# 'tls' => true,
# 'tls_ca_file' => '/path/to/ca.pem',
# 'tls_allow_invalid_certificates' => false,
# 'tls_certificate_key_file' => '/path/to/combined.pem',
# 'tags' => ['optional_tag1', 'optional_tag2'],
# 'username' => 'mongo_username',
# 'dbm' => true,
# 'database_autodiscovery' => {'enabled' => true},
# 'reported_database_hostname' => 'mymongodbhost',
# },
# {
# 'host' => 'localhost',
# 'port' => '27018',
# 'hosts' => ['localhost:27017'],
# 'tags' => [],
# 'additional_metrics' => [],
# 'collections' => [],
Expand Down
26 changes: 26 additions & 0 deletions spec/classes/datadog_agent_integrations_mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@
it { is_expected.to contain_file(conf_file).without_content(%r{tags:}) }
end

context 'with one mongo host defined in hosts array' do
let(:params) do
{
servers: [
{
'hosts' => ['localhost:27017'],
'username' => 'user',
'password' => 'pass',
'database' => 'admin',
'dbm' => true,
'database_autodiscovery' => { 'enabled' => true },
'reported_database_hostname' => 'mongohost',
},
],
}
end

it { is_expected.to contain_file(conf_file).with_content(%r{- hosts:\s+- localhost:27017}) }
it { is_expected.to contain_file(conf_file).with_content(%r{username: user}) }
it { is_expected.to contain_file(conf_file).with_content(%r{password: pass}) }
it { is_expected.to contain_file(conf_file).with_content(%r{database: admin}) }
it { is_expected.to contain_file(conf_file).with_content(%r{dbm: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{database_autodiscovery:\s+enabled: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{reported_database_hostname: mongohost}) }
end

context 'with one mongo' do
let(:params) do
{
Expand Down
29 changes: 29 additions & 0 deletions templates/agent-conf.d/mongo.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ init_config:

instances:
<% @servers.each do |server| -%>
<% if !server['hosts'].nil? && server['hosts'].any? -%>
- hosts:
<%- server['hosts'].each do |host| -%>
- <%= host %>
<%- end -%>
<%- if !server['username'].nil? -%>
username: <%= server['username'] %>
<%- end -%>
<%- if !server['password'].nil? -%>
password: <%= server['password'] %>
<%- end -%>
<%- if !server['database'].nil? -%>
database: <%= server['database'] %>
<%- end -%>
<%- end -%>
<% if server['hosts'].nil? -%>
- server: mongodb://<%= server['username'] %><%= ":" unless server['password'].nil? %><%= server['password'] %><%= "@" unless server['username'].nil? %><%= server['host'] %>:<%= server['port'] %>/<%= server['database'] %>
<%- end -%>
<%- if !server['tags'].nil? && server['tags'].any? -%>
tags:
<%- server['tags'].each do |tag| -%>
Expand Down Expand Up @@ -50,4 +67,16 @@ instances:
- <%= collection %>
<%- end -%>
<%- end -%>
<%- if !server['dbm'].nil? -%>
dbm: <%= server['dbm'] %>
<%- end -%>
<%- if !server['database_autodiscovery'].nil? -%>
database_autodiscovery:
<%- if !server['database_autodiscovery']['enabled'].nil? -%>
enabled: <%= server['database_autodiscovery']['enabled'] %>
<%- end -%>
<%- end -%>
<%- if !server['reported_database_hostname'].nil? -%>
reported_database_hostname: <%= server['reported_database_hostname'] %>
<%- end -%>
<% end -%>