Skip to content

Commit

Permalink
Added: #site and #api_key configuration settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jul 11, 2020
1 parent 46dbab3 commit b201d18
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ build-iPhoneSimulator/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
# Ignore local variables
.envrc

# lock files
Gemfile.lock
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ services:
- bundle-jruby-9.2:/usr/local/bundle
- gemfiles-jruby-9.2:/app/gemfiles
ddagent:
image: datadog/docker-dd-agent
image: datadog/agent
environment:
- DD_APM_ENABLED=true
- DD_BIND_HOST=0.0.0.0
- DD_API_KEY=invalid_key_but_this_is_fine
- DD_API_KEY=00000000000000000000000000000000
expose:
- "8125/udp"
- "8126"
Expand Down
10 changes: 10 additions & 0 deletions lib/ddtrace/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Settings
end
end

option :api_key do |o|
o.default { ENV.fetch(Ext::Environment::ENV_API_KEY, nil) }
o.lazy
end

settings :diagnostics do
option :debug, default: false

Expand Down Expand Up @@ -146,6 +151,11 @@ def runtime_metrics(options = nil)
o.lazy
end

option :site do |o|
o.default { ENV.fetch(Ext::Environment::ENV_SITE, nil) }
o.lazy
end

option :tags do |o|
o.default do
tags = {}
Expand Down
6 changes: 5 additions & 1 deletion lib/ddtrace/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module Environment
# Defines helper methods for environment
module Helpers
def env_to_bool(var, default = nil)
ENV.key?(var) ? ENV[var].to_s.downcase == 'true' : default
ENV.key?(var) ? ENV[var].to_s.strip.downcase == 'true' : default
end

def env_to_int(var, default = nil)
ENV.key?(var) ? ENV[var].to_i : default
end

def env_to_float(var, default = nil)
Expand Down
2 changes: 2 additions & 0 deletions lib/ddtrace/ext/environment.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Datadog
module Ext
module Environment
ENV_API_KEY = 'DD_API_KEY'.freeze
ENV_ENVIRONMENT = 'DD_ENV'.freeze
ENV_SERVICE = 'DD_SERVICE'.freeze
ENV_SITE = 'DD_SITE'.freeze
ENV_TAGS = 'DD_TAGS'.freeze
ENV_VERSION = 'DD_VERSION'.freeze

Expand Down
65 changes: 65 additions & 0 deletions spec/ddtrace/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'securerandom'

require 'ddtrace'
require 'ddtrace/configuration/settings'
Expand Down Expand Up @@ -59,6 +60,38 @@
end
end

describe '#api_key' do
subject(:api_key) { settings.api_key }

context "when #{Datadog::Ext::Environment::ENV_API_KEY}" do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_API_KEY => api_key_env) do
example.run
end
end

context 'is not defined' do
let(:api_key_env) { nil }
it { is_expected.to be nil }
end

context 'is defined' do
let(:api_key_env) { SecureRandom.uuid.delete('-') }
it { is_expected.to eq(api_key_env) }
end
end
end

describe '#api_key=' do
subject(:set_api_key) { settings.api_key = api_key }

context 'when given a value' do
let(:api_key) { SecureRandom.uuid.delete('-') }
before { set_api_key }
it { expect(settings.api_key).to eq(api_key) }
end
end

describe '#diagnostics' do
describe '#debug' do
subject(:debug) { settings.diagnostics.debug }
Expand Down Expand Up @@ -484,6 +517,38 @@
end
end

describe '#site' do
subject(:site) { settings.site }

context "when #{Datadog::Ext::Environment::ENV_SITE}" do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_SITE => site_env) do
example.run
end
end

context 'is not defined' do
let(:site_env) { nil }
it { is_expected.to be nil }
end

context 'is defined' do
let(:site_env) { 'datadoghq.com' }
it { is_expected.to eq(site_env) }
end
end
end

describe '#site=' do
subject(:set_site) { settings.site = site }

context 'when given a value' do
let(:site) { 'datadoghq.com' }
before { set_site }
it { expect(settings.site).to eq(site) }
end
end

describe '#tags' do
subject(:tags) { settings.tags }

Expand Down
185 changes: 185 additions & 0 deletions spec/ddtrace/environment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
require 'spec_helper'

require 'ddtrace/environment'

RSpec.describe Datadog::Environment do
let(:var) { 'TEST_VAR' }

shared_context 'env var' do
around do |example|
ClimateControl.modify(var => env_value) do
example.run
end
end
end

describe '::env_to_bool' do
subject(:env_to_bool) { described_class.env_to_bool(var) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to be nil }
end

context 'and default is defined' do
subject(:env_to_bool) { described_class.env_to_bool(var, default) }
let(:default) { double('default') }
it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

# True values
%w[
true
TRUE
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
it { is_expected.to be true }
end
end

# False values
[
'',
'false',
'FALSE',
0,
1
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
it { is_expected.to be false }
end
end
end
end

describe '::env_to_int' do
subject(:env_to_int) { described_class.env_to_int(var) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to be nil }
end

context 'and default is defined' do
subject(:env_to_int) { described_class.env_to_int(var, default) }
let(:default) { double('default') }
it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

context '0' do
let(:env_value) { '0' }
it { is_expected.to eq 0 }
end

context '1' do
let(:env_value) { '1' }
it { is_expected.to eq 1 }
end

context '1.5' do
let(:env_value) { '1.5' }
it { is_expected.to eq 1 }
end

context 'test' do
let(:env_value) { 'test' }
it { is_expected.to eq 0 }
end
end
end

describe '::env_to_float' do
subject(:env_to_float) { described_class.env_to_float(var) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to be nil }
end

context 'and default is defined' do
subject(:env_to_float) { described_class.env_to_float(var, default) }
let(:default) { double('default') }
it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

context '0' do
let(:env_value) { '0' }
it { is_expected.to eq 0.0 }
end

context '1' do
let(:env_value) { '1' }
it { is_expected.to eq 1.0 }
end

context '1.5' do
let(:env_value) { '1.5' }
it { is_expected.to eq 1.5 }
end

context 'test' do
let(:env_value) { 'test' }
it { is_expected.to eq 0.0 }
end
end
end

describe '::env_to_list' do
subject(:env_to_list) { described_class.env_to_list(var) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to eq([]) }
end

context 'and default is defined' do
subject(:env_to_list) { described_class.env_to_list(var, default) }
let(:default) { double('default') }
it { is_expected.to be default }
end
end

context 'when env var is set as' do
include_context 'env var'

context '\'\'' do
let(:env_value) { '' }
it { is_expected.to eq([]) }
end

context ',' do
let(:env_value) { ',' }
it { is_expected.to eq([]) }
end

context '1' do
let(:env_value) { '1' }
it { is_expected.to eq(['1']) }
end

context '1,2' do
let(:env_value) { '1,2' }
it { is_expected.to eq(%w[1 2]) }
end

context ' 1 , 2 , 3 ' do
let(:env_value) { ' 1 , 2 , 3 ' }
it { is_expected.to eq(%w[1 2 3]) }
end
end
end
end

0 comments on commit b201d18

Please sign in to comment.