Skip to content

Commit

Permalink
update mongo configuration template (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda authored Feb 26, 2025
1 parent 7f93cb4 commit 9472fc7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
27 changes: 19 additions & 8 deletions manifests/integrations/mongo.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
# 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 +41,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 +83,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
24 changes: 24 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,30 @@
it { is_expected.to contain_file(conf_file).without_content(%r{tags:}) }
end

context 'with one mongo host defined in hosts' do
let(:params) do
{
servers: [
{
'hosts' => ['localhost:27017'],
'username' => 'user',
'password' => 'pass',
'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{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 -%>

0 comments on commit 9472fc7

Please sign in to comment.