Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_active_metrics and resolve_monitors methods for missing api endpoints #172

Merged
merged 4 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 96
Exclude:
- 'spec/integration/monitor_spec.rb'

# Offense count: 2
Metrics/CyclomaticComplexity:
Expand Down Expand Up @@ -340,6 +342,7 @@ Style/Documentation:
- 'lib/dogapi/v1/snapshot.rb'
- 'lib/dogapi/v1/user.rb'
- 'lib/dogapi/v1/integration.rb'
- 'lib/dogapi/v1/metric.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand Down
11 changes: 11 additions & 0 deletions lib/dogapi/facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ def batch_metrics()
end
end

# Get a list of active metrics since a given time
# +from+ The seconds since the unix epoch <tt>[Time, Integer]</tt>
#
def get_active_metrics(from)
@metric_svc.get_active_metrics(from)
end

#
# EVENTS

Expand Down Expand Up @@ -507,6 +514,10 @@ def unmute_monitor(monitor_id, options= {})
@monitor_svc.unmute_monitor(monitor_id, options)
end

def resolve_monitors(monitor_groups = [], options = {}, version = nil)
@monitor_svc.resolve_monitors(monitor_groups, options, version)
end

def search_monitors(options = {})
@monitor_svc.search_monitors(options)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/dogapi/v1/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ def make_metric_payload(metric, points, scope, options)
suppress_error_if_silent e
end
end
end

def get_active_metrics(from)
params = {
from: from.to_i
}

request(Net::HTTP::Get, '/api/' + API_VERSION + '/metrics', params, nil, false)
end
end
end
end
12 changes: 11 additions & 1 deletion lib/dogapi/v1/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def unmute_monitor(monitor_id, options = {})
request(Net::HTTP::Post, "/api/#{API_VERSION}/monitor/#{monitor_id}/unmute", nil, options, true)
end

def resolve_monitors(monitor_groups = [], options = {}, version = nil)
body = {
'resolve' => monitor_groups
}.merge options

# Currently not part of v1 at this time but adding future compatibility option
endpoint = version.nil? ? '/api/monitor/bulk_resolve' : "/api/#{version}/monitor/bulk_resolve"

request(Net::HTTP::Post, endpoint, nil, body, true)
end

def search_monitors(options = {})
request(Net::HTTP::Get, "/api/#{API_VERSION}/monitor/search", options, nil, false)
end
Expand Down Expand Up @@ -133,6 +144,5 @@ def unmute_host(hostname)
request(Net::HTTP::Post, "/api/#{API_VERSION}/host/#{hostname}/unmute", nil, {}, true)
end
end

end
end
7 changes: 7 additions & 0 deletions spec/integration/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
}.freeze

METRIC_PARAMS = { query: METRIC_QUERY, from: FROM.to_i, to: TO.to_i }.freeze
ACTIVE_METRICS_PARAMS = { from: FROM.to_i }.freeze

describe '#emit_point' do
it 'queries the api' do
Expand Down Expand Up @@ -93,4 +94,10 @@
)
end
end

describe '#get_active_metrics' do
it_behaves_like 'an api method with params',
:get_active_metrics, [],
:get, '/metrics', ACTIVE_METRICS_PARAMS
end
end
7 changes: 7 additions & 0 deletions spec/integration/monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
DOWNTIME_SCOPE = 'host:vagrant-ubuntu-trusty-64'.freeze
DOWNTIME_ID = 424_242_424_242
MUTE_HOSTNAME = 'vagrant-ubuntu-trusty-32'.freeze
MONITOR_GROUPS = [{ 'check_a' => 'group_x' }, { 'check_a' => 'group_y' }, { 'check_b' => 'ALL_GROUPS' }].freeze

describe '#monitor' do
it_behaves_like 'an api method with options',
Expand Down Expand Up @@ -68,6 +69,12 @@
:post, "/monitor/#{MONITOR_ID}/unmute", {}
end

describe '#resolve_monitors' do
it_behaves_like 'an api method with options',
:resolve_monitors, [MONITOR_GROUPS],
:post, '/monitor/bulk_resolve', 'resolve' => MONITOR_GROUPS
end

describe '#schedule_downtime' do
it_behaves_like 'an api method with options',
:schedule_downtime, [DOWNTIME_SCOPE],
Expand Down
11 changes: 7 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ module SpecDog
let(:app_key) { 'APP_KEY' }
let(:dog) { Dogapi::Client.new(api_key, app_key, 'data.dog', nil, false) }
let(:api_url) { "#{DATADOG_HOST}/api/v1" }
let(:old_api_url) { "#{DATADOG_HOST}/api" }
let(:default_query) { { api_key: api_key, application_key: app_key } }

shared_examples 'an api method' do |command, args, request, endpoint, body|
it 'queries the api' do
url = api_url + endpoint
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
old_url = old_api_url + endpoint
stub_request(request, /#{url}|#{old_url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(command, *args)).to eq ['200', {}]

body = MultiJson.dump(body) if body

expect(WebMock).to have_requested(request, url).with(
expect(WebMock).to have_requested(request, /#{url}|#{old_url}/).with(
query: default_query,
body: body
)
Expand All @@ -42,13 +44,14 @@ module SpecDog
include_examples 'an api method', command, args, request, endpoint, body
it 'queries the api with options' do
url = api_url + endpoint
old_url = old_api_url + endpoint
options = { 'zzz' => 'aaa' }
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
stub_request(request, /#{url}|#{old_url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(command, *args, options)).to eq ['200', {}]

body = MultiJson.dump(body ? (body.merge options) : options)

expect(WebMock).to have_requested(request, url).with(
expect(WebMock).to have_requested(request, /#{url}|#{old_url}/).with(
query: default_query,
body: body
)
Expand Down