Skip to content

Commit

Permalink
Add datadog_monitor option to set the config file name
Browse files Browse the repository at this point in the history
This allows people to use the integration multiple times and place
different config files in the same integration directory.  The use case
would be many different cookbooks adding a single check they care about
without doing messy attribute overrides.
  • Loading branch information
brentm5 committed Aug 11, 2024
1 parent c8b66e3 commit c5b1c1e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,21 @@ datadog_monitor 'name' do
instances Array # default value: []
logs Array # default value: []
use_integration_template true, false # default value: false
config_name String # default value: 'conf'
action Symbol # defaults to :add
end
```
#### Properties
| Property | Description |
|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `'name'` | The name of the Agent integration to configure and enable. |
| `instances` | The fields used to fill values under the `instances` section in the integration configuration file. |
| `init_config` | The fields used to fill values under the the `init_config` section in the integration configuration file. |
| `logs` | The fields used to fill values under the the `logs` section in the integration configuration file. |
| Property | Description |
|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `'name'` | The name of the Agent integration to configure and enable. |
| `instances` | The fields used to fill values under the `instances` section in the integration configuration file. |
| `init_config` | The fields used to fill values under the the `init_config` section in the integration configuration file. |
| `logs` | The fields used to fill values under the the `logs` section in the integration configuration file. |
| `use_integration_template` | Set to `true` (recommended) to use the default template, which writes the values of `instances`, `init_config`, and `logs` in the YAML under their respective keys. This defaults to `false` for backward compatibility, but may default to `true` in a future major version of the cookbook. |
| `config_name` | The filename used when creating an integrations configuration file. Overriding this property allows the creation of multiple configuration files for a single integration. This defaults to `conf`, which creates a configuration file named `conf.yaml`. |
#### Example
Expand Down
5 changes: 3 additions & 2 deletions resources/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
property :use_integration_template, [TrueClass, FalseClass], required: false, default: false
property :is_jmx, [TrueClass, FalseClass], required: false, default: false
property :logs, [Array, nil], required: false, default: []
property :config_name, [String, nil], required: false, default: 'conf'

action :add do
Chef::Log.debug("Adding monitoring for #{new_resource.name}")
Expand All @@ -49,7 +50,7 @@
mode '755'
end
end
yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.d", 'conf.yaml')
yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.d", "#{new_resource.config_name}.yaml")
else
yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.yaml")
end
Expand Down Expand Up @@ -101,7 +102,7 @@

action :remove do
yaml_file = if Chef::Datadog.agent_major_version(node) != 5
::File.join(yaml_dir, "#{new_resource.name}.d", 'conf.yaml')
::File.join(yaml_dir, "#{new_resource.name}.d", "#{new_resource.config_name}.yaml")
else
::File.join(yaml_dir, "#{new_resource.name}.yaml")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/dd-agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ def set_env_var(name, value)
end
it 'creates the config file at A7\'s path' do
expect(chef_run).to render_file('/etc/datadog-agent/conf.d/potato.d/conf.yaml')
expect(chef_run).to render_file('/etc/datadog-agent/conf.d/potato.d/potato-one.yaml')
end
end
end
Expand Down Expand Up @@ -1541,6 +1542,7 @@ def set_env_var(name, value)
end
it 'creates the config file at A7\'s path' do
expect(chef_run).to delete_file('/etc/datadog-agent/conf.d/potato.d/conf.yaml')
expect(chef_run).to delete_file('/etc/datadog-agent/conf.d/potato.d/potato-one.yaml')
end
end
end
5 changes: 5 additions & 0 deletions test/cookbooks/test/recipes/monitor_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
datadog_monitor 'potato' do
action :add
end

datadog_monitor 'potato' do
action :add
config_name 'potato-one'
end
5 changes: 5 additions & 0 deletions test/cookbooks/test/recipes/monitor_remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
datadog_monitor 'potato' do
action :remove
end

datadog_monitor 'potato' do
action :remove
config_name 'potato-one'
end

0 comments on commit c5b1c1e

Please sign in to comment.