Skip to content

Commit

Permalink
Adding support for custom MySQL queries
Browse files Browse the repository at this point in the history
Already supported by the agent, but not exposed in the Chef cookbook

Adding a kitchen test on MySQL as well
  • Loading branch information
wk8 committed Dec 5, 2015
1 parent 4e59a32 commit 204d209
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,32 @@ suites:
- host: localhost
port: 27017

- name: datadog_mysql
run_list:
- recipe[datadog::mysql]
attributes:
datadog:
<<: *DATADOG
mysql:
instances:
- server: 1.1.1.1
port: 3307
user: my_username
pass: my_password
sock: /path/to/mysql.sock
tags: ['prod', 'my_app']
options:
- 'replication: 0'
queries:
- type: gauge
field: users_count
metric: my_app.my_users.count
query: SELECT COUNT(1) AS users_count FROM users
- type: gauge
field: max_query_time
metric: mysql.performance.max_query_time
query: SELECT IFNULL(MAX(TIME), 0) AS max_query_time FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND != 'Sleep'

- name: datadog_ntp
run_list:
- recipe[datadog::ntp]
Expand Down
8 changes: 8 additions & 0 deletions recipes/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
# 'options' => [
# "replication: 0",
# "galera_cluster: 1"
# ],
# 'queries' => [
# {
# 'type' => 'gauge',
# 'field' => 'users_count'
# 'metric' => 'my_app.my_users.count',
# 'query' => 'SELECT COUNT(1) AS users_count FROM users'
# },
# ]
# },
# ]
Expand Down
9 changes: 9 additions & 0 deletions templates/default/mysql.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ instances:
<%= o %>
<% end -%>
<% end -%>
<% if i.key?('queries') -%>
queries:
<% i['queries'].each do |q| -%>
- type: <%= q['type'] %>
field: <%= q['field'] %>
metric: <%= q['metric'] %>
query: <%= q['query'] %>
<% end -%>
<% end -%>
<% end -%>

# Nothing to configure here
Expand Down
1 change: 1 addition & 0 deletions test/integration/datadog_mysql/serverspec/Gemfile
54 changes: 54 additions & 0 deletions test/integration/datadog_mysql/serverspec/mysql_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Encoding: utf-8
require 'json_spec'
require 'serverspec'
require 'yaml'

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

AGENT_CONFIG = '/etc/dd-agent/conf.d/mysql.yaml'

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

describe file(AGENT_CONFIG) do
it { should be_a_file }

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

expected = {
'instances' => [
{
'server' => '1.1.1.1',
'port' => 3307,
'user' => 'my_username',
'pass' => 'my_password',
'sock' => '/path/to/mysql.sock',
'tags' => ['prod', 'my_app'],
'options' => {
'replication' => 0
},
'queries' => [
{
'type' => 'gauge',
'field' => 'users_count',
'metric' => 'my_app.my_users.count',
'query' => 'SELECT COUNT(1) AS users_count FROM users'
},
{
'type' => 'gauge',
'field' => 'max_query_time',
'metric' => 'mysql.performance.max_query_time',
'query' => "SELECT IFNULL(MAX(TIME), 0) AS max_query_time FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND != 'Sleep'"
}
]
}
],
'init_config' => nil
}

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

0 comments on commit 204d209

Please sign in to comment.