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 methods for missing hourly usage api endpoints #173

Merged
merged 2 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
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ Style/Documentation:
- 'lib/dogapi/v1/snapshot.rb'
- 'lib/dogapi/v1/user.rb'
- 'lib/dogapi/v1/integration.rb'
- 'lib/dogapi/v1/usage.rb'
- 'lib/dogapi/v1/metric.rb'

# Offense count: 1
Expand Down
36 changes: 36 additions & 0 deletions lib/dogapi/facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true,
@legacy_event_svc = Dogapi::EventService.new(@datadog_host)
@hosts_svc = Dogapi::V1::HostsService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@integration_svc = Dogapi::V1::IntegrationService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@usage_svc = Dogapi::V1::UsageService.new(@api_key, @application_key, silent, timeout, @datadog_host)
end

#
Expand Down Expand Up @@ -632,6 +633,41 @@ def delete_integration(source_type_name)
@integration_svc.delete_integration(source_type_name)
end

#
# USAGE
#

# Get hourly usage information for different datadog service
# Usage data is delayed by up to 72 hours from when it was incurred. It is retained for the past 15 months.#
# format of dates is ISO-8601 UTC YYYY-MM-DDThh
# ex:
# require 'time'
# Time.now.utc.strftime('%Y-%m-%dT%H')
# => "2019-05-05T13"
def get_hosts_usage(start_hr, end_hr = nil)
@usage_svc.get_hosts_usage(start_hr, end_hr)
end

def get_logs_usage(start_hr, end_hr = nil)
@usage_svc.get_logs_usage(start_hr, end_hr)
end

def get_custom_metrics_usage(start_hr, end_hr = nil)
@usage_svc.get_custom_metrics_usage(start_hr, end_hr)
end

def get_traces_usage(start_hr, end_hr = nil)
@usage_svc.get_traces_usage(start_hr, end_hr)
end

def get_synthetics_usage(start_hr, end_hr = nil)
@usage_svc.get_synthetics_usage(start_hr, end_hr)
end

def get_fargate_usage(start_hr, end_hr = nil)
@usage_svc.get_fargate_usage(start_hr, end_hr)
end

private

def override_scope(options= {})
Expand Down
1 change: 1 addition & 0 deletions lib/dogapi/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
require 'dogapi/v1/user'
require 'dogapi/v1/hosts'
require 'dogapi/v1/integration'
require 'dogapi/v1/usage'
95 changes: 95 additions & 0 deletions lib/dogapi/v1/usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require 'dogapi'

module Dogapi
class V1 # for namespacing

class UsageService < Dogapi::APIService

API_VERSION = 'v1'

# Retrieve hourly host usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_hosts_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/hosts", params, nil, false)
end

# Retrieve hourly logs usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_logs_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/logs", params, nil, false)
end

# Retrieve hourly custom metrics usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_custom_metrics_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/timeseries", params, nil, false)
end

# Retrieve hourly trace search usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_traces_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/traces", params, nil, false)
end

# Retrieve hourly synthetics usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_synthetics_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/synthetics", params, nil, false)
end

# Retrieve hourly fargate usage information
#
# :start_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, for usage beginning at this hour
# :end_hr => String: Datetime ISO-8601 UTC YYYY-MM-DDThh, default start_hr+1d, for usage ending BEFORE this hour
def get_fargate_usage(start_hr, end_hr = nil)
params = {
start_hr: start_hr
}

params['end_hr'] = end_hr if end_hr

request(Net::HTTP::Get, "/api/#{API_VERSION}/usage/fargate", params, nil, false)
end
end
end
end
44 changes: 44 additions & 0 deletions spec/integration/usage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../spec_helper'

describe Dogapi::Client do
USAGE_PARAMS = {
start_hr: (Time.now - (3600 * 24)).utc.strftime('%Y-%m-%dT%H'),
end_hr: Time.now.utc.strftime('%Y-%m-%dT%H')
}.freeze

describe '#get_hosts_usage' do
it_behaves_like 'an api method with params',
:get_hosts_usage, [],
:get, '/usage/hosts', USAGE_PARAMS
end

describe '#get_logs_usage' do
it_behaves_like 'an api method with params',
:get_logs_usage, [],
:get, '/usage/logs', USAGE_PARAMS
end

describe '#get_custom_metrics_usage' do
it_behaves_like 'an api method with params',
:get_custom_metrics_usage, [],
:get, '/usage/timeseries', USAGE_PARAMS
end

describe '#get_traces_usage' do
it_behaves_like 'an api method with params',
:get_traces_usage, [],
:get, '/usage/traces', USAGE_PARAMS
end

describe '#get_synthetics_usage' do
it_behaves_like 'an api method with params',
:get_synthetics_usage, [],
:get, '/usage/synthetics', USAGE_PARAMS
end

describe '#get_fargate_usage' do
it_behaves_like 'an api method with params',
:get_fargate_usage, [],
:get, '/usage/fargate', USAGE_PARAMS
end
end