Skip to content

Commit

Permalink
Refactored: Moved #tags, #env, #service, #version into Configuration:…
Browse files Browse the repository at this point in the history
…:Settings.
  • Loading branch information
delner committed Mar 27, 2020
1 parent aa0551c commit 203c19c
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 216 deletions.
5 changes: 4 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,10 @@ These values can also be overridden at the tracer level:

```ruby
Datadog.configure do |c|
c.tracer env: 'test', tags: { 'team' => 'qa' }
c.service = 'billing-api'
c.env = 'test'
c.tags = { 'team' => 'qa' }
c.version = '1.3-alpha'
end
```

Expand Down
35 changes: 35 additions & 0 deletions lib/ddtrace/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ class Settings
#
# Configuration options
#
option :service do |o|
o.default { ENV.fetch(Ext::Environment::ENV_SERVICE, nil) }
o.lazy
end

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

option :tags do |o|
o.default do
tags = {}

# Parse tags from environment
env_to_list(Ext::Environment::ENV_TAGS).each do |tag|
pair = tag.split(':')
tags[pair.first] = pair.last if pair.length == 2
end

# Override tags if defined
tags[Ext::Environment::TAG_ENV] = env unless env.nil?
tags[Ext::Environment::TAG_VERSION] = version unless version.nil?

tags
end

o.lazy
end

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

option :analytics_enabled do |o|
o.default { env_to_bool(Ext::Analytics::ENV_TRACE_ANALYTICS_ENABLED, nil) }
o.lazy
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/correlation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def initialize(*args)
super
self.trace_id = trace_id || 0
self.span_id = span_id || 0
self.env = env || Datadog::Environment.env
self.version = version || Datadog::Environment.version
self.env = env || Datadog.configuration.env
self.version = version || Datadog.configuration.version
end

def to_s
Expand Down
29 changes: 0 additions & 29 deletions lib/ddtrace/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,6 @@
module Datadog
# Namespace for handling application environment
module Environment
# TODO: Extract to Datadog::Configuration::Settings
def self.env
ENV[Ext::Environment::ENV_ENVIRONMENT]
end

# TODO: Extract to Datadog::Configuration::Settings
def self.service
ENV[Ext::Environment::ENV_SERVICE]
end

# TODO: Extract to Datadog::Configuration::Settings
def self.tags
tags = {}

env_to_list(Ext::Environment::ENV_TAGS).each do |tag|
pair = tag.split(':')
tags[pair.first] = pair.last if pair.length == 2
end

tags[Ext::Environment::TAG_ENV] = env unless env.nil?
tags[Ext::Environment::TAG_VERSION] = version unless version.nil?

tags
end

def self.version
ENV[Ext::Environment::ENV_VERSION]
end

# Defines helper methods for environment
module Helpers
def env_to_bool(var, default = nil)
Expand Down
9 changes: 6 additions & 3 deletions lib/ddtrace/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ def default_metric_options
# and defaults are unfrozen for mutation in Statsd.
DEFAULT.dup.tap do |options|
options[:tags] = options[:tags].dup
# rubocop:disable Metrics/LineLength
options[:tags] << "#{Datadog::Ext::Environment::TAG_ENV}:#{Datadog::Environment.env}" unless Datadog::Environment.env.nil?
options[:tags] << "#{Datadog::Ext::Environment::TAG_VERSION}:#{Datadog::Environment.version}" unless Datadog::Environment.version.nil?

env = Datadog.configuration.env
options[:tags] << "#{Datadog::Ext::Environment::TAG_ENV}:#{env}" unless env.nil?

version = Datadog.configuration.version
options[:tags] << "#{Datadog::Ext::Environment::TAG_VERSION}:#{version}" unless version.nil?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def finish(finish_time = nil)
# spans without a service would be dropped, so here we provide a default.
# This should really never happen with integrations in contrib, as a default
# service is always set. It's only for custom instrumentation.
@service ||= (Datadog::Environment.service || (@tracer && @tracer.default_service))
@service ||= (Datadog.configuration.service || (@tracer && @tracer.default_service))

begin
@context.close_span(self)
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def initialize(options = {})
end

@mutex = Mutex.new
@tags = Datadog::Environment.tags
@tags = options.fetch(:tags, Datadog.configuration.tags)

# Enable priority sampling by default
activate_priority_sampling!(@sampler)
Expand Down Expand Up @@ -156,7 +156,7 @@ def default_service
# tracer.set_tags('env' => 'prod', 'component' => 'core')
def set_tags(tags)
string_tags = Hash[tags.collect { |k, v| [k.to_s, v] }]
@tags.update(string_tags)
@tags = @tags.merge(string_tags)
end

# Guess context and parent from child_of entry.
Expand Down
161 changes: 161 additions & 0 deletions spec/ddtrace/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,167 @@
RSpec.describe Datadog::Configuration::Settings do
subject(:settings) { described_class.new }

describe '#env' do
subject(:env) { settings.env }
context "when #{Datadog::Ext::Environment::ENV_ENVIRONMENT}" do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => environment) do
example.run
end
end

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

context 'is defined' do
let(:environment) { 'env-value' }
it { is_expected.to eq(environment) }
end
end
end

describe '#service' do
subject(:service) { settings.service }
context "when #{Datadog::Ext::Environment::ENV_SERVICE}" do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_SERVICE => service) do
example.run
end
end

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

context 'is defined' do
let(:service) { 'service-value' }
it { is_expected.to eq(service) }
end
end
end

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

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

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

context 'is defined' do
let(:env_tags) { 'a:1,b:2' }

it { is_expected.to include('a' => '1', 'b' => '2') }

context 'with an invalid tag' do
context do
let(:env_tags) { '' }
it { is_expected.to eq({}) }
end

context do
let(:env_tags) { 'a' }
it { is_expected.to eq({}) }
end

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

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

context do
let(:env_tags) { 'a:' }
it { is_expected.to eq({}) }
end
end

context 'and when #env' do
before { allow(settings).to receive(:env).and_return(env) }

context 'is set' do
let(:env) { 'env-value' }
it { is_expected.to include('env' => env) }
end

context 'is not set' do
let(:env) { nil }
it { is_expected.to_not include('env') }
end
end

context 'and when #version' do
before { allow(settings).to receive(:version).and_return(version) }

context 'is set' do
let(:version) { 'version-value' }
it { is_expected.to include('version' => version) }
end

context 'is not set' do
let(:version) { nil }
it { is_expected.to_not include('version') }
end
end
end

context 'conflicts with #env' do
let(:env_tags) { "env:#{tag_env_value}" }
let(:tag_env_value) { 'tag-env-value' }
let(:env_value) { 'env-value' }

before { allow(settings).to receive(:env).and_return(env_value) }

it { is_expected.to include('env' => env_value) }
end

context 'conflicts with #version' do
let(:env_tags) { "env:#{tag_version_value}" }
let(:tag_version_value) { 'tag-version-value' }
let(:version_value) { 'version-value' }

before { allow(settings).to receive(:version).and_return(version_value) }

it { is_expected.to include('version' => version_value) }
end
end
end

describe '#version' do
subject(:version) { settings.version }
context "when #{Datadog::Ext::Environment::ENV_VERSION}" do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_VERSION => version) do
example.run
end
end

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

context 'is defined' do
let(:version) { 'version-value' }
it { is_expected.to eq(version) }
end
end
end

describe '#sampling' do
describe '#rate_limit' do
subject(:rate_limit) { settings.sampling.rate_limit }
Expand Down
8 changes: 4 additions & 4 deletions spec/ddtrace/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def have_attribute(attribute)
let(:version) { nil }

before do
allow(Datadog::Environment).to receive(:env).and_return(environment)
allow(Datadog::Environment).to receive(:version).and_return(version)
allow(Datadog.configuration).to receive(:env).and_return(environment)
allow(Datadog.configuration).to receive(:version).and_return(version)
end

context 'given nil' do
Expand Down Expand Up @@ -74,7 +74,7 @@ def have_attribute(attribute)

it_behaves_like 'a correlation identifier with basic properties'

context 'when Datadog::Environment.env' do
context 'when #env configuration setting' do
context 'is not defined' do
let(:environment) { nil }
it_behaves_like 'a correlation identifier with basic properties'
Expand All @@ -86,7 +86,7 @@ def have_attribute(attribute)
end
end

context 'when Datadog::Environment.version' do
context 'when #version configuration setting' do
context 'is not defined' do
let(:version) { nil }
it_behaves_like 'a correlation identifier with basic properties'
Expand Down
Loading

0 comments on commit 203c19c

Please sign in to comment.