Skip to content

Commit

Permalink
Merge pull request #6 from esigler/add_mute
Browse files Browse the repository at this point in the history
Add ability to mute and unmute hosts in DataDog
  • Loading branch information
esigler committed Mar 3, 2016
2 parents 0722a31 + f097691 commit 73bff83
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
*.swp
.DS_Store
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ to:"<time description>"

Time descriptions are parsed by https://github.com/mojombo/chronic

### Muting

Muting a host:

```
Lita dd mute <hostname> [message:"Some reason"]
```

Unmuting a host:

```
Lita dd unmute <hostname>
```

## License

[MIT](http://opensource.org/licenses/MIT)
39 changes: 39 additions & 0 deletions lib/lita/handlers/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,49 @@ class Datadog < Handler
}
)

route(
/^dd\smute\s(?<hostname>\S*)(\smessage:"(?<message>.*)")?$/,
:mute,
command: true,
help: {
t('help.mute.syntax') => t('help.mute.desc')
}
)

route(
/^dd\sunmute\s(?<hostname>\S*)$/,
:unmute,
command: true,
help: {
t('help.unmute.syntax') => t('help.unmute.desc')
}
)

def graph(response)
content = snapshot(parse_arguments(response.match_data['args']))
response.reply(content)
end

def mute(response)
hostname = response.match_data['hostname']
message = response.match_data['message']
args = {}
args['message'] = message unless message.nil?
if mute_host(hostname, args)
response.reply(t('mute.success', host: hostname))
else
response.reply(t('errors.request'))
end
end

def unmute(response)
hostname = response.match_data['hostname']
if unmute_host(hostname)
response.reply(t('unmute.success', host: hostname))
else
response.reply(t('errors.request'))
end
end
end

Lita.register_handler(Datadog)
Expand Down
28 changes: 28 additions & 0 deletions lib/lita/helpers/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@ module Lita
module Helpers
# Helpers to go alongside fetching & parsing things
module Utilities
def mute_host(hostname, args)
client = Dogapi::Client.new(config.api_key, config.application_key)
return false unless client

return_code, contents = client.mute_host(hostname, args)

if return_code.to_s != '200'
log.warning("URL (#{return_code}): #{contents['errors'].join('\n')}")
return false
end

true
end

def unmute_host(hostname)
client = Dogapi::Client.new(config.api_key, config.application_key)
return false unless client

return_code, contents = client.unmute_host(hostname)

if return_code.to_s != '200'
log.warning("URL (#{return_code}): #{contents['errors'].join('\n')}")
return false
end

true
end

def get_graph_url(metric_query, start_ts, end_ts, event_query)
client = Dogapi::Client.new(config.api_key, config.application_key)

Expand Down
4 changes: 2 additions & 2 deletions lita-datadog.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Gem::Specification.new do |spec|
spec.name = 'lita-datadog'
spec.version = '0.9.0'
spec.version = '0.10.0'
spec.authors = ['Eric Sigler']
spec.email = ['me@esigler.com']
spec.description = 'A Datadog plugin for Lita'
spec.summary = 'A Datadog plugin for Lita'
spec.summary = spec.description
spec.homepage = 'http://github.com/esigler/lita-datadog'
spec.license = 'MIT'
spec.metadata = { 'lita_plugin_type' => 'handler' }
Expand Down
12 changes: 11 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ en:
handlers:
datadog:
errors:
request: Error requesting Datadog graph
request: Error making DataDog request
help:
graph:
syntax: 'graph metric:"simple.metric.1{*},simple.metric.5{*}'
desc: 'Graph those metrics, for the default time range'
mute:
syntax: 'dd mute <hostname> [message:"Some reason"]'
desc: 'Mute a host in DataDog, optionally with a message'
unmute:
syntax: 'dd unmute <hostname>'
desc: 'Unmute a host in DataDog'
mute:
success: "Host %{host} muted"
unmute:
success: "Host %{host} unmuted"
64 changes: 46 additions & 18 deletions spec/lita/handlers/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

describe Lita::Handlers::Datadog, lita_handler: true do
EXAMPLE_IMAGE_URL = 'http://www.example.com/path/that/ends/in.png'.freeze
EXAMPLE_ERROR_MSG = 'Error requesting Datadog graph'.freeze
EXAMPLE_ERROR_MSG = 'Error making DataDog request'.freeze

let(:success) do
client = double
allow(client).to receive(:graph_snapshot) {
[200, { 'snapshot_url' => EXAMPLE_IMAGE_URL }]
}
allow(client).to receive(:mute_host) {
[200, { 'hostname' => 'host01' }]
}
allow(client).to receive(:unmute_host) {
[200, { 'hostname' => 'host01' }]
}
client
end

let(:error) do
client = double
allow(client).to receive(:graph_snapshot) { [500, { 'errors' => ['foo'] }] }
allow(client).to receive(:mute_host) { [500, { 'errors' => ['foo'] }] }
allow(client).to receive(:unmute_host) { [500, { 'errors' => ['foo'] }] }
client
end

Expand All @@ -31,24 +39,10 @@
is_expected.to route_command(
'graph metric:"system.load.1{*}" event:"sources:something"')
.to(:graph)
end

describe '.default_config' do
it 'sets the api_key to nil' do
expect(Lita.config.handlers.datadog.api_key).to be_nil
end

it 'sets the application_key to nil' do
expect(Lita.config.handlers.datadog.application_key).to be_nil
end

it 'sets the timerange to 3600' do
expect(Lita.config.handlers.datadog.timerange).to eq(3600)
end

it 'sets the waittime to 0' do
expect(Lita.config.handlers.datadog.waittime).to eq(0)
end
is_expected.to route_command('dd mute host01').to(:mute)
is_expected.to route_command('dd mute host01 message:"Foo Bar"').to(:mute)
is_expected.to route_command('dd unmute host01')
end

describe '#graph' do
Expand Down Expand Up @@ -82,4 +76,38 @@
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end
end

describe '#mute' do
it 'mutes a hostname' do
expect(Dogapi::Client).to receive(:new) { success }
send_command('dd mute host01')
expect(replies.last).to eq('Host host01 muted')
end

it 'mutes a hostname with a message' do
expect(Dogapi::Client).to receive(:new) { success }
send_command('dd mute host01 message:"Foo Bar"')
expect(replies.last).to eq('Host host01 muted')
end

it 'reports an error if there was a problem with the request' do
expect(Dogapi::Client).to receive(:new) { error }
send_command('dd mute host01')
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end
end

describe '#unmute' do
it 'unmutes a hostname' do
expect(Dogapi::Client).to receive(:new) { success }
send_command('dd unmute host01')
expect(replies.last).to eq('Host host01 unmuted')
end

it 'reports an error if there was a problem with the request' do
expect(Dogapi::Client).to receive(:new) { error }
send_command('dd unmute host01')
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end
end
end

0 comments on commit 73bff83

Please sign in to comment.