From 1651c53025ed55b66d0ec8aebc5d45a18107a238 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Mon, 1 Oct 2018 14:07:51 +0200 Subject: [PATCH 1/2] add getter/setter for datadog_host --- README.rdoc | 28 ++++++++++++++++++++++++++++ lib/dogapi/facade.rb | 1 + spec/unit/common_spec.rb | 11 +++++++++++ spec/unit/facade_spec.rb | 13 +++++++++++++ 4 files changed, 53 insertions(+) diff --git a/README.rdoc b/README.rdoc index dbab0606..d569e3e8 100644 --- a/README.rdoc +++ b/README.rdoc @@ -49,6 +49,34 @@ to take advantage of automatic tagging with the host's tags. If you want to attach events and points to a specific device on a host, simply specify the device when calling emit functions. +== Configure the Datadog API Url + + + require 'rubygems' + require 'dogapi' + + api_key = "abcdef123456" + application_key = "fedcba654321" + + # by default the API Url will be set to https://api.datadoghq.com + dog = Dogapi::Client.new(api_key, application_key) + p dog.datadog_host # prints https://api.datadoghq.com + + # API Url can be passed to the initializer... + dog = Dogapi::Client.new(api_key, application_key, nil, nil, nil, nil, 'https://myproxy.local') + p dog.datadog_host # prints https://myproxy.local + + # ...or set on the client instance directly + dog = Dogapi::Client.new(api_key, application_key) + dog.datadog_host = 'https://myproxy.local' + p dog.datadog_host # prints https://myproxy.local + + # in any case, contents of the DATADOG_HOST env var take precedence + ENV['DATADOG_HOST'] = https://myproxy.local + dog = Dogapi::Client.new(api_key, application_key) + p dog.datadog_host # prints https://myproxy.local + + == Submit an event to Datadog diff --git a/lib/dogapi/facade.rb b/lib/dogapi/facade.rb index 6fe211e3..5ca1d603 100644 --- a/lib/dogapi/facade.rb +++ b/lib/dogapi/facade.rb @@ -12,6 +12,7 @@ module Dogapi # information about the JSON object structure is available in the HTTP API # documentation, here[https://github.com/DataDog/dogapi/wiki]. class Client # rubocop:disable Metrics/ClassLength + attr_accessor :datadog_host def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true, timeout=nil, endpoint=nil) diff --git a/spec/unit/common_spec.rb b/spec/unit/common_spec.rb index f2b6ebb5..c30f1b12 100644 --- a/spec/unit/common_spec.rb +++ b/spec/unit/common_spec.rb @@ -1,6 +1,17 @@ require_relative '../spec_helper' describe 'Common' do + describe Dogapi::find_datadog_host do + it "gives precedence to DATADOG_HOST env var" do + allow(ENV).to receive(:[]).with("DATADOG_HOST").and_return('example.com') + expect(Dogapi::find_datadog_host).to eq 'example.com' + end + it "falls back to default url when DATADOG_HOST env var is not set" do + allow(ENV).to receive(:[]).with("DATADOG_HOST").and_return(nil) + expect(Dogapi::find_datadog_host).to eq 'https://api.datadoghq.com' + end + end + context 'Scope' do it 'validates the Scope class' do obj = Dogapi::Scope.new('somehost', 'somedevice') diff --git a/spec/unit/facade_spec.rb b/spec/unit/facade_spec.rb index baf722f7..3ded7683 100644 --- a/spec/unit/facade_spec.rb +++ b/spec/unit/facade_spec.rb @@ -11,6 +11,19 @@ def @service.upload(payload) end end + describe 'Datadog API url' do + it 'can be set in initializer' do + client = Dogapi::Client.new(api_key, app_key, nil, nil, nil, nil, 'example.com') + expect(client.datadog_host).to eq 'example.com' + end + + it 'can be set with instance var' do + client = Dogapi::Client.new(api_key, app_key) + client.datadog_host = 'example.com' + expect(client.datadog_host).to eq 'example.com' + end + end + describe '#emit_point' do it 'passes data' do @dogmock.emit_point('metric.name', 0, host: 'myhost') From b343028c603a52f562ec9299c445943b60d3d9f7 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Mon, 1 Oct 2018 14:19:18 +0200 Subject: [PATCH 2/2] make rubocop happy --- spec/unit/common_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/unit/common_spec.rb b/spec/unit/common_spec.rb index c30f1b12..f40a0225 100644 --- a/spec/unit/common_spec.rb +++ b/spec/unit/common_spec.rb @@ -1,14 +1,14 @@ require_relative '../spec_helper' describe 'Common' do - describe Dogapi::find_datadog_host do - it "gives precedence to DATADOG_HOST env var" do - allow(ENV).to receive(:[]).with("DATADOG_HOST").and_return('example.com') - expect(Dogapi::find_datadog_host).to eq 'example.com' + describe Dogapi.find_datadog_host do + it 'gives precedence to DATADOG_HOST env var' do + allow(ENV).to receive(:[]).with('DATADOG_HOST').and_return('example.com') + expect(Dogapi.find_datadog_host).to eq 'example.com' end - it "falls back to default url when DATADOG_HOST env var is not set" do - allow(ENV).to receive(:[]).with("DATADOG_HOST").and_return(nil) - expect(Dogapi::find_datadog_host).to eq 'https://api.datadoghq.com' + it 'falls back to default url when DATADOG_HOST env var is not set' do + allow(ENV).to receive(:[]).with('DATADOG_HOST').and_return(nil) + expect(Dogapi.find_datadog_host).to eq 'https://api.datadoghq.com' end end