Skip to content

Commit

Permalink
Render jmx.yaml template based on data input
Browse files Browse the repository at this point in the history
After multiple passes at attempting to cover all cases of inclusion of instance
details into a given YAML file, this lays the foundation for leaving the details
up to the end-user to implement the instance hash, instead of trying to capture
each possible flag.
This should help future-proof the recipe and template, so any new flags can be
appended as data.

See #88, #93 and fixes #116, #121.

Tests and examples updated.
  • Loading branch information
miketheman committed Dec 30, 2014
1 parent fcfbe9d commit c2e0dbf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 63 deletions.
27 changes: 22 additions & 5 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,30 @@ suites:
application_key: alsonotnil
jmx:
instances:
- server: localhost
- host: localhost
port: 9999
extra_key: extra_val
include:
- domain: domain0
- domain: domain1
exclude: []
conf:
- include:
domain: domain0
type: ThreadPool
bean: tomcat_bean
attributes:
maxThreads:
alias: tomcat.threads.max
metric_type: gauge
bytesReceived:
alias: tomcat.bytes_rcvd
metric_type: counter
include:
domain: org.apache.cassandra.db
attributes:
- BloomFilterDiskSpaceUsed
- Capacity
bean_name: com.datadoghq.test:type=BeanType,tag1=my_bean_name
foo: bar
exclude:
- domain: evil_domain

- name: dd-agent-mongo
run_list:
Expand Down
17 changes: 17 additions & 0 deletions recipes/jmx.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
include_recipe 'datadog::dd-agent'

# Build a data structure with configuration.
# @see https://github.com/DataDog/dd-agent/blob/master/conf.d/jmx.yaml.example JMX Example
# @example
# node.override['datadog']['jmx']['instances'] = [
# {
# 'host' => 'localhost',
# 'port' => '7199',
# 'name' 'prod_jmx_app',
# 'conf' => [
# 'include' => {
# 'attributes' => ['Capacity', 'Used'],
# 'bean_name' => 'com.datadoghq.test:type=BeanType,tag1=my_bean_name',
# 'domain' => 'com.datadoghq.test'
# }
# ]
# }
# ]
datadog_monitor 'jmx' do
instances node['datadog']['jmx']['instances']
end
47 changes: 2 additions & 45 deletions templates/default/jmx.yaml.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
<% excluded_keys = ["host", "server", "conf", "include", "exclude"] -%>
instances:
<% @instances.each do |i| -%>
- host: <%= i["server"] %>
<% (i.keys - excluded_keys).each do |key| -%>
<%= key %>: <%= i[key] %>
<% end -%>
<% if i.key?("include") || i.key?("exclude") -%>
conf:
<% i["include"].each do |c| -%>
- include:
<% if c.key?("domain") -%>
domain: <%= c["domain"] %>
<% end -%>
<% if c.key?("bean") -%>
bean: <%= c["bean"] %>
<% end -%>
<% if c.key?("attributes") -%>
attribute:
<% c["attributes"].each do |a| -%>
attribute<%= c["attributes"].index(a) %>:
metric_type: <%= a["metric_type"] %>
alias: <%= a["alias"] %>
<% end -%>
<% end -%>
<% end -%>
<% i["exclude"].each do |c| -%>
- exclude:
<% if c.key?("domain") -%>
domain: <%= c["domain"] %>
<% end -%>
<% if c.key?("bean") -%>
bean: <%= c["bean"] %>
<% end -%>
<% if c.key?("attributes") -%>
attribute:
<% c["attributes"].each do |a| -%>
attribute<%= c["attributes"].index(a) %>:
metric_type: <%= a["metric_type"] %>
alias: <%= a["alias"] %>
<% end -%>
<% end -%>
<% end -%>
<% end -%>
<% end -%>
<%# Sanitize the compiled Mash to standard Array/Hash objects by way of JSON -%>
<%= JSON.parse(({ 'instances' => @instances }).to_json).to_yaml %>

init_config:
# Nothing to configure here
13 changes: 0 additions & 13 deletions test/integration/dd-agent-jmx/bats/jmx_config.bats

This file was deleted.

3 changes: 3 additions & 0 deletions test/integration/dd-agent-jmx/serverspec/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'json_spec', '~> 1.1'
61 changes: 61 additions & 0 deletions test/integration/dd-agent-jmx/serverspec/dd-agent-jmx_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Encoding: utf-8
require 'json_spec'
require 'serverspec'
require 'yaml'

set :backend, :exec
set :path, '/sbin:/usr/local/sbin:$PATH'

JMX_CONFIG = '/etc/dd-agent/conf.d/jmx.yaml'

describe service('datadog-agent') do
it { should be_running }
end

describe file(JMX_CONFIG) do
it { should be_a_file }

it 'is valid yaml matching input values' do
generated = YAML.load_file(JMX_CONFIG)

expected = {
'init_config' => nil,
'instances' => [
{
'conf' => [
{
'exclude' => [
{
'domain' => 'evil_domain'
}
],
'include' => {
'attribute' => {
'bytesReceived' => { 'metric_type' => 'counter', 'alias' => 'tomcat.bytes_rcvd' },
'maxThreads' => { 'metric_type' => 'gauge', 'alias' => 'tomcat.threads.max' }
},
'bean' => 'tomcat_bean',
'domain' => 'domain0',
'type' => 'ThreadPool'
},
'include' => {
'attributes' => [
'BloomFilterDiskSpaceUsed',
'Capacity'
],
'bean_name' => 'com.datadoghq.test:type=BeanType,tag1=my_bean_name',
'domain' => 'org.apache.cassandra.db',
'foo' => 'bar'
}
}
],
'extra_key' => 'extra_val',
'host' => 'localhost',
'port' => 9999
}
]
}

expect(generated.to_json).to be_json_eql expected.to_json
end
end

0 comments on commit c2e0dbf

Please sign in to comment.