Skip to content

Commit

Permalink
Merge pull request #160 from DataDog/massi/dd_host
Browse files Browse the repository at this point in the history
Add getter/setter for datadog_host
  • Loading branch information
masci authored Oct 1, 2018
2 parents 341f4dd + b343028 commit 2696fc0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions lib/dogapi/facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/facade_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2696fc0

Please sign in to comment.