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 getter/setter for datadog_host #160

Merged
merged 2 commits into from
Oct 1, 2018
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
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