From 683868e1b4848ba9f0517e37b3643bbeb72d6ddc Mon Sep 17 00:00:00 2001 From: ericmustin Date: Sun, 5 May 2019 16:16:55 +0200 Subject: [PATCH] add endpoints and spec for hosts logs custom_metrics traces synthetics fargate hourly usage --- .rubocop_todo.yml | 1 + lib/dogapi/facade.rb | 36 +++++++++++++ lib/dogapi/v1.rb | 1 + lib/dogapi/v1/usage.rb | 95 ++++++++++++++++++++++++++++++++++ spec/integration/usage_spec.rb | 44 ++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 lib/dogapi/v1/usage.rb create mode 100644 spec/integration/usage_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1dd1240e..99b55afb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -340,6 +340,7 @@ Style/Documentation: - 'lib/dogapi/v1/snapshot.rb' - 'lib/dogapi/v1/user.rb' - 'lib/dogapi/v1/integration.rb' + - 'lib/dogapi/v1/usage.rb' # Offense count: 1 # Cop supports --auto-correct. diff --git a/lib/dogapi/facade.rb b/lib/dogapi/facade.rb index 89bd8c2a..aecc1ccb 100644 --- a/lib/dogapi/facade.rb +++ b/lib/dogapi/facade.rb @@ -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 # @@ -621,6 +622,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= {}) diff --git a/lib/dogapi/v1.rb b/lib/dogapi/v1.rb index 3b542860..50fdcbf5 100644 --- a/lib/dogapi/v1.rb +++ b/lib/dogapi/v1.rb @@ -16,3 +16,4 @@ require 'dogapi/v1/user' require 'dogapi/v1/hosts' require 'dogapi/v1/integration' +require 'dogapi/v1/usage' diff --git a/lib/dogapi/v1/usage.rb b/lib/dogapi/v1/usage.rb new file mode 100644 index 00000000..2532eda9 --- /dev/null +++ b/lib/dogapi/v1/usage.rb @@ -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 diff --git a/spec/integration/usage_spec.rb b/spec/integration/usage_spec.rb new file mode 100644 index 00000000..9f1b0eb9 --- /dev/null +++ b/spec/integration/usage_spec.rb @@ -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