lita-datadog 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 674d744f8af8eef3c2124caf7fe6f330edfed54d
4
- data.tar.gz: dd9e912e13bb4ce44fd3ba42991301c68b2c8790
3
+ metadata.gz: 9e93b235ba1458b7bafd84b79fc69a0dacd5b4da
4
+ data.tar.gz: fe1a7a633fab19e2720c502c781ac26b5e7e12d8
5
5
  SHA512:
6
- metadata.gz: 1e331d216b2083724b8ee65a9c939a7be060200b8a86720362b653175315a424aae3698e292830b3c3d8a1248969f1f22d867a9647a4babd959a23c9fefc000f
7
- data.tar.gz: fe96cea3e2fd8a0040f81e0be28da1a0da91ec463be3890eb137ce8ac6784c5d68f08b1f7095293f26baa29d33318f1736d5ff2de816fc6e50ec1c61060b7bb6
6
+ metadata.gz: f7f2bfd6595bee7e7684856418e1bd8d5e7f2a98125c21068c88c9cb5e64b10938593964a7b87ab12a791ab23c496a99e2771e2807e7a1ec881821248e9de162
7
+ data.tar.gz: d029ea15a656fd46da382aee1cbd008b10ccf63bee0d3600d1d60f16b25828d8d4716209214bef96b31ecbcc6fef9e3009c6306eb582cc8a43209463b49c1fb2
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  *.swp
19
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --profile 5
@@ -1,8 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1
4
- script: bundle exec rake
5
- before_install:
6
- - gem update --system
4
+ - 2.2
7
5
  services:
8
6
  - redis-server
7
+ sudo: false
8
+ cache: bundler
data/README.md CHANGED
@@ -81,6 +81,20 @@ to:"<time description>"
81
81
 
82
82
  Time descriptions are parsed by https://github.com/mojombo/chronic
83
83
 
84
+ ### Muting
85
+
86
+ Muting a host:
87
+
88
+ ```
89
+ Lita dd mute <hostname> [message:"Some reason"]
90
+ ```
91
+
92
+ Unmuting a host:
93
+
94
+ ```
95
+ Lita dd unmute <hostname>
96
+ ```
97
+
84
98
  ## License
85
99
 
86
100
  [MIT](http://opensource.org/licenses/MIT)
@@ -4,6 +4,8 @@ Lita.load_locales Dir[File.expand_path(
4
4
  File.join('..', '..', 'locales', '*.yml'), __FILE__
5
5
  )]
6
6
 
7
+ require 'lita/helpers/utilities'
8
+ require 'lita/helpers/graphs'
7
9
  require 'lita/handlers/datadog'
8
10
 
9
11
  require 'dogapi'
@@ -6,68 +6,60 @@ module Lita
6
6
  config :timerange, default: 3600
7
7
  config :waittime, default: 0
8
8
 
9
+ include Lita::Helpers::Utilities
10
+ include Lita::Helpers::Graphs
11
+
9
12
  route(
10
- /^graph\s(.*)$/,
13
+ /^graph\s(?<args>.*)$/,
11
14
  :graph,
12
15
  command: true,
13
- help: { t('help.graph.syntax') => t('help.graph.desc') }
16
+ help: {
17
+ t('help.graph.syntax') => t('help.graph.desc')
18
+ }
14
19
  )
15
20
 
16
- def graph(response)
17
- args = parse_arguments(response.matches[0][0])
18
- response.reply(get_response(args))
19
- end
21
+ route(
22
+ /^dd\smute\s(?<hostname>\S*)(\smessage:"(?<message>.*)")?$/,
23
+ :mute,
24
+ command: true,
25
+ help: {
26
+ t('help.mute.syntax') => t('help.mute.desc')
27
+ }
28
+ )
20
29
 
21
- private
30
+ route(
31
+ /^dd\sunmute\s(?<hostname>\S*)$/,
32
+ :unmute,
33
+ command: true,
34
+ help: {
35
+ t('help.unmute.syntax') => t('help.unmute.desc')
36
+ }
37
+ )
22
38
 
23
- def get_response(args)
24
- return_code, snapshot = get_graph_url(args[:metric],
25
- args[:start],
26
- args[:end],
27
- args[:event])
39
+ def graph(response)
40
+ content = snapshot(parse_arguments(response.match_data['args']))
41
+ response.reply(content)
42
+ end
28
43
 
29
- if return_code.to_s == '200'
30
- sleep config.waittime
31
- return snapshot['snapshot_url']
44
+ def mute(response)
45
+ hostname = response.match_data['hostname']
46
+ message = response.match_data['message']
47
+ args = {}
48
+ args['message'] = message unless message.nil?
49
+ if mute_host(hostname, args)
50
+ response.reply(t('mute.success', host: hostname))
32
51
  else
33
- t('errors.request')
52
+ response.reply(t('errors.request'))
34
53
  end
35
54
  end
36
55
 
37
- def get_graph_url(metric_query, start_ts, end_ts, event_query)
38
- client = Dogapi::Client.new(config.api_key, config.application_key)
39
-
40
- return nil unless client
41
-
42
- client.graph_snapshot(metric_query, start_ts, end_ts, event_query)
43
- end
44
-
45
- def parse_arguments(arg_string)
46
- end_ts = parse_end(arg_string)
47
- start_ts = parse_start(arg_string, end_ts)
48
- metric = parse_metric(arg_string)
49
- event = parse_event(arg_string)
50
- { metric: metric, start: start_ts, end: end_ts, event: event }
51
- end
52
-
53
- def parse_end(string)
54
- found = /(to|end):"(.+?)"/.match(string)
55
- found ? Chronic.parse(found[2]).to_i : Time.now.to_i
56
- end
57
-
58
- def parse_start(string, end_ts)
59
- found = /(from|start):"(.+?)"/.match(string)
60
- found ? Chronic.parse(found[2]).to_i : end_ts - config.timerange
61
- end
62
-
63
- def parse_metric(string)
64
- found = /metric:"(.+?)"/.match(string)
65
- found ? found[1] : 'system.load.1{*}'
66
- end
67
-
68
- def parse_event(string)
69
- found = /event:"(.+?)"/.match(string)
70
- found ? found[1] : ''
56
+ def unmute(response)
57
+ hostname = response.match_data['hostname']
58
+ if unmute_host(hostname)
59
+ response.reply(t('unmute.success', host: hostname))
60
+ else
61
+ response.reply(t('errors.request'))
62
+ end
71
63
  end
72
64
  end
73
65
 
@@ -0,0 +1,18 @@
1
+ module Lita
2
+ module Helpers
3
+ # Helpers for different ways to generate a graph
4
+ module Graphs
5
+ def snapshot(args)
6
+ url = get_graph_url(args[:metric],
7
+ args[:start],
8
+ args[:end],
9
+ args[:event])
10
+
11
+ return t('errors.request') if url.nil?
12
+ # NOTE: Is this still needed?
13
+ sleep config.waittime
14
+ url['snapshot_url']
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,78 @@
1
+ module Lita
2
+ module Helpers
3
+ # Helpers to go alongside fetching & parsing things
4
+ module Utilities
5
+ def mute_host(hostname, args)
6
+ client = Dogapi::Client.new(config.api_key, config.application_key)
7
+ return false unless client
8
+
9
+ return_code, contents = client.mute_host(hostname, args)
10
+
11
+ if return_code.to_s != '200'
12
+ log.warning("URL (#{return_code}): #{contents['errors'].join('\n')}")
13
+ return false
14
+ end
15
+
16
+ true
17
+ end
18
+
19
+ def unmute_host(hostname)
20
+ client = Dogapi::Client.new(config.api_key, config.application_key)
21
+ return false unless client
22
+
23
+ return_code, contents = client.unmute_host(hostname)
24
+
25
+ if return_code.to_s != '200'
26
+ log.warning("URL (#{return_code}): #{contents['errors'].join('\n')}")
27
+ return false
28
+ end
29
+
30
+ true
31
+ end
32
+
33
+ def get_graph_url(metric_query, start_ts, end_ts, event_query)
34
+ client = Dogapi::Client.new(config.api_key, config.application_key)
35
+
36
+ return nil unless client
37
+
38
+ return_code, contents = client.graph_snapshot(metric_query, start_ts,
39
+ end_ts, event_query)
40
+
41
+ if return_code.to_s != '200'
42
+ log.warning("URL (#{return_code}): #{contents['errors'].join('\n')}")
43
+ return nil
44
+ end
45
+
46
+ contents
47
+ end
48
+
49
+ def parse_arguments(arg_string)
50
+ end_ts = parse_end(arg_string)
51
+ start_ts = parse_start(arg_string, end_ts)
52
+ metric = parse_metric(arg_string)
53
+ event = parse_event(arg_string)
54
+ { metric: metric, start: start_ts, end: end_ts, event: event }
55
+ end
56
+
57
+ def parse_end(string)
58
+ found = /(to|end):"(.+?)"/.match(string)
59
+ found ? Chronic.parse(found[2]).to_i : Time.now.to_i
60
+ end
61
+
62
+ def parse_start(string, end_ts)
63
+ found = /(from|start):"(.+?)"/.match(string)
64
+ found ? Chronic.parse(found[2]).to_i : end_ts - config.timerange
65
+ end
66
+
67
+ def parse_metric(string)
68
+ found = /metric:"(.+?)"/.match(string)
69
+ found ? found[1] : 'system.load.1{*}'
70
+ end
71
+
72
+ def parse_event(string)
73
+ found = /event:"(.+?)"/.match(string)
74
+ found ? found[1] : ''
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,17 +1,17 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-datadog'
3
- spec.version = '0.9.0'
3
+ spec.version = '0.10.0'
4
4
  spec.authors = ['Eric Sigler']
5
5
  spec.email = ['me@esigler.com']
6
6
  spec.description = 'A Datadog plugin for Lita'
7
- spec.summary = 'A Datadog plugin for Lita'
7
+ spec.summary = spec.description
8
8
  spec.homepage = 'http://github.com/esigler/lita-datadog'
9
9
  spec.license = 'MIT'
10
10
  spec.metadata = { 'lita_plugin_type' => 'handler' }
11
11
 
12
12
  spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
13
- spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
14
- spec.test_files = spec.files.grep(/^(test|spec|features)\//)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ['lib']
16
16
 
17
17
  spec.add_runtime_dependency 'lita', '>= 4.0'
@@ -3,8 +3,18 @@ en:
3
3
  handlers:
4
4
  datadog:
5
5
  errors:
6
- request: Error requesting Datadog graph
6
+ request: Error making DataDog request
7
7
  help:
8
8
  graph:
9
9
  syntax: 'graph metric:"simple.metric.1{*},simple.metric.5{*}'
10
10
  desc: 'Graph those metrics, for the default time range'
11
+ mute:
12
+ syntax: 'dd mute <hostname> [message:"Some reason"]'
13
+ desc: 'Mute a host in DataDog, optionally with a message'
14
+ unmute:
15
+ syntax: 'dd unmute <hostname>'
16
+ desc: 'Unmute a host in DataDog'
17
+ mute:
18
+ success: "Host %{host} muted"
19
+ unmute:
20
+ success: "Host %{host} unmuted"
@@ -1,20 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Lita::Handlers::Datadog, lita_handler: true do
4
- EXAMPLE_IMAGE_URL = 'http://www.example.com/path/that/ends/in.png'
5
- EXAMPLE_ERROR_MSG = 'Error requesting Datadog graph'
4
+ EXAMPLE_IMAGE_URL = 'http://www.example.com/path/that/ends/in.png'.freeze
5
+ EXAMPLE_ERROR_MSG = 'Error making DataDog request'.freeze
6
6
 
7
7
  let(:success) do
8
8
  client = double
9
9
  allow(client).to receive(:graph_snapshot) {
10
10
  [200, { 'snapshot_url' => EXAMPLE_IMAGE_URL }]
11
11
  }
12
+ allow(client).to receive(:mute_host) {
13
+ [200, { 'hostname' => 'host01' }]
14
+ }
15
+ allow(client).to receive(:unmute_host) {
16
+ [200, { 'hostname' => 'host01' }]
17
+ }
12
18
  client
13
19
  end
14
20
 
15
21
  let(:error) do
16
22
  client = double
17
- allow(client).to receive(:graph_snapshot) { [500, {}] }
23
+ allow(client).to receive(:graph_snapshot) { [500, { 'errors' => ['foo'] }] }
24
+ allow(client).to receive(:mute_host) { [500, { 'errors' => ['foo'] }] }
25
+ allow(client).to receive(:unmute_host) { [500, { 'errors' => ['foo'] }] }
18
26
  client
19
27
  end
20
28
 
@@ -31,24 +39,10 @@ describe Lita::Handlers::Datadog, lita_handler: true do
31
39
  is_expected.to route_command(
32
40
  'graph metric:"system.load.1{*}" event:"sources:something"')
33
41
  .to(:graph)
34
- end
35
-
36
- describe '.default_config' do
37
- it 'sets the api_key to nil' do
38
- expect(Lita.config.handlers.datadog.api_key).to be_nil
39
- end
40
-
41
- it 'sets the application_key to nil' do
42
- expect(Lita.config.handlers.datadog.application_key).to be_nil
43
- end
44
42
 
45
- it 'sets the timerange to 3600' do
46
- expect(Lita.config.handlers.datadog.timerange).to eq(3600)
47
- end
48
-
49
- it 'sets the waittime to 0' do
50
- expect(Lita.config.handlers.datadog.waittime).to eq(0)
51
- end
43
+ is_expected.to route_command('dd mute host01').to(:mute)
44
+ is_expected.to route_command('dd mute host01 message:"Foo Bar"').to(:mute)
45
+ is_expected.to route_command('dd unmute host01')
52
46
  end
53
47
 
54
48
  describe '#graph' do
@@ -82,4 +76,38 @@ describe Lita::Handlers::Datadog, lita_handler: true do
82
76
  expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
83
77
  end
84
78
  end
79
+
80
+ describe '#mute' do
81
+ it 'mutes a hostname' do
82
+ expect(Dogapi::Client).to receive(:new) { success }
83
+ send_command('dd mute host01')
84
+ expect(replies.last).to eq('Host host01 muted')
85
+ end
86
+
87
+ it 'mutes a hostname with a message' do
88
+ expect(Dogapi::Client).to receive(:new) { success }
89
+ send_command('dd mute host01 message:"Foo Bar"')
90
+ expect(replies.last).to eq('Host host01 muted')
91
+ end
92
+
93
+ it 'reports an error if there was a problem with the request' do
94
+ expect(Dogapi::Client).to receive(:new) { error }
95
+ send_command('dd mute host01')
96
+ expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
97
+ end
98
+ end
99
+
100
+ describe '#unmute' do
101
+ it 'unmutes a hostname' do
102
+ expect(Dogapi::Client).to receive(:new) { success }
103
+ send_command('dd unmute host01')
104
+ expect(replies.last).to eq('Host host01 unmuted')
105
+ end
106
+
107
+ it 'reports an error if there was a problem with the request' do
108
+ expect(Dogapi::Client).to receive(:new) { error }
109
+ send_command('dd unmute host01')
110
+ expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
111
+ end
112
+ end
85
113
  end
@@ -10,3 +10,20 @@ require 'lita-datadog'
10
10
  require 'lita/rspec'
11
11
 
12
12
  Lita.version_3_compatibility_mode = false
13
+
14
+ RSpec.configure do |config|
15
+ config.expect_with :rspec do |expectations|
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ config.mock_with :rspec do |mocks|
20
+ mocks.verify_partial_doubles = true
21
+ end
22
+
23
+ config.filter_run :focus
24
+ config.run_all_when_everything_filtered = true
25
+ config.default_formatter = 'doc' if config.files_to_run.one?
26
+ config.order = :random
27
+
28
+ Kernel.srand config.seed
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-datadog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Sigler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -144,6 +144,7 @@ extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
146
  - ".gitignore"
147
+ - ".rspec"
147
148
  - ".rubocop.yml"
148
149
  - ".travis.yml"
149
150
  - CONTRIBUTING.md
@@ -153,6 +154,8 @@ files:
153
154
  - Rakefile
154
155
  - lib/lita-datadog.rb
155
156
  - lib/lita/handlers/datadog.rb
157
+ - lib/lita/helpers/graphs.rb
158
+ - lib/lita/helpers/utilities.rb
156
159
  - lita-datadog.gemspec
157
160
  - locales/en.yml
158
161
  - spec/lita/handlers/datadog_spec.rb
@@ -178,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
181
  version: '0'
179
182
  requirements: []
180
183
  rubyforge_project:
181
- rubygems_version: 2.2.2
184
+ rubygems_version: 2.5.1
182
185
  signing_key:
183
186
  specification_version: 4
184
187
  summary: A Datadog plugin for Lita