Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update mongo configuration template #838

Merged
merged 1 commit into from
Feb 26, 2025
Merged
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
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'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Suggested change
'hosts' => ['localhost:27017'],
'hosts' => %w[localhost:27017],
Consider using the %w syntax instead (...read more)

The rule "Prefer %w to the literal array syntax" is a Ruby style guideline that encourages the use of %w notation instead of the traditional array syntax when defining arrays of strings. This rule is part of the Ruby community's efforts to promote readability and simplicity in Ruby code.

This rule is important because it helps to keep the code concise and easy to read. The %w notation allows you to define an array of strings without having to use quotes and commas. This can make the code cleaner and easier to understand, especially when dealing with large arrays.

To follow this rule, replace the traditional array syntax with the %w notation. For example, instead of writing ['foo', 'bar', 'baz'], you should write %w[foo bar baz]. This will create the same array, but in a more readable and concise way. By following this rule, you can help to make your Ruby code cleaner and easier to understand.

View in Datadog  Leave us feedback  Documentation

'username' => 'user',
'password' => 'pass',
'dbm' => true,
'database_autodiscovery' => { 'enabled' => true },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Consider using symbols instead of string hash keys (...read more)

In Ruby, it is a best practice to use symbols instead of strings as hash keys. This rule emphasizes that it's more efficient and idiomatic to use symbols for this purpose. Symbols are immutable and unique, which makes them ideal for identifying things, whereas strings are mutable and can create multiple objects for the same sequence of characters.

The importance of this rule lies in the performance and memory usage of your Ruby application. Using symbols as hash keys reduces memory usage because they are stored in memory only once during a Ruby process. This can make a significant difference in the efficiency of your application, especially when dealing with large data sets.

To ensure you're following good coding practices, always use symbols for hash keys unless there's a specific reason to use a string. A simple refactoring from values = { 'foo' => 42, 'bar' => 99, 'baz' => 123 } to values = { foo: 42, bar: 99, baz: 123 } will make your code compliant with this rule. This not only improves your code's performance but also makes it more readable and consistent with Ruby's conventions.

View in Datadog  Leave us feedback  Documentation

'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 -%>
Loading