Skip to content

Commit

Permalink
Changed: Use from env var to Datadog::Environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Mar 20, 2020
1 parent cc426ae commit a22c211
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 62 deletions.
4 changes: 2 additions & 2 deletions lib/ddtrace/correlation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'ddtrace/ext/environment'
require 'ddtrace/environment'

module Datadog
# Contains behavior for managing correlations with tracing
Expand All @@ -10,7 +10,7 @@ def initialize(*args)
super
self.trace_id = trace_id || 0
self.span_id = span_id || 0
self.env = env || ENV[Datadog::Ext::Environment::ENV_ENVIRONMENT]
self.env = env || Datadog::Environment.env
end

def to_s
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/metrics.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'ddtrace/ext/metrics'
require 'ddtrace/ext/environment'

require 'set'
require 'logger'
require 'ddtrace/environment'
require 'ddtrace/utils/time'
require 'ddtrace/runtime/identity'

Expand Down Expand Up @@ -152,7 +152,7 @@ def default_metric_options
# and defaults are unfrozen for mutation in Statsd.
DEFAULT.dup.tap do |options|
options[:tags] = options[:tags].dup
options[:tags] << "env:#{ENV[Ext::Environment::ENV_ENVIRONMENT]}" if ENV.key?(Ext::Environment::ENV_ENVIRONMENT)
options[:tags] << "env:#{Datadog::Environment.env}" unless Datadog::Environment.env.nil?
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require 'logger'
require 'pathname'

require 'ddtrace/ext/environment'

require 'ddtrace/environment'
require 'ddtrace/span'
require 'ddtrace/context'
require 'ddtrace/logger'
Expand Down Expand Up @@ -85,9 +84,7 @@ def initialize(options = {})
end

@mutex = Mutex.new
@tags = {}.tap do |tags|
tags[:env] = ENV[Ext::Environment::ENV_ENVIRONMENT] if ENV.key?(Ext::Environment::ENV_ENVIRONMENT)
end
@tags = Datadog::Environment.tags

# Enable priority sampling by default
activate_priority_sampling!(@sampler)
Expand Down
18 changes: 4 additions & 14 deletions spec/ddtrace/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,17 @@
expect(correlation_ids.to_s).to eq("dd.trace_id=#{trace_id} dd.span_id=#{span_id}")
end

context "when #{Datadog::Ext::Environment::ENV_ENVIRONMENT}" do
context 'is not defined' do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => nil) do
example.run
end
end
context 'when Datadog::Environment.env' do
before { allow(Datadog::Environment).to receive(:env).and_return(environment) }

context 'is not defined' do
let(:environment) { nil }
it { expect(correlation_ids.env).to be nil }
it { expect(correlation_ids.to_s).to eq("dd.trace_id=#{trace_id} dd.span_id=#{span_id}") }
end

context 'is defined' do
let(:environment) { 'my-env' }

around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => environment) do
example.run
end
end

it { expect(correlation_ids.env).to eq environment }
it { expect(correlation_ids.to_s).to eq("dd.trace_id=#{trace_id} dd.span_id=#{span_id} dd.env=#{environment}") }
end
Expand Down
18 changes: 4 additions & 14 deletions spec/ddtrace/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,26 +638,16 @@
)
end

context "when #{Datadog::Ext::Environment::ENV_ENVIRONMENT}" do
context 'is not defined' do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => nil) do
example.run
end
end
context 'when Datadog::Environment.env' do
before { allow(Datadog::Environment).to receive(:env).and_return(environment) }

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

context 'is defined' do
let(:environment) { 'my-env' }

around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => environment) do
example.run
end
end

it { is_expected.to include("env:#{environment}") }
end
end
Expand Down
31 changes: 6 additions & 25 deletions spec/ddtrace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@

describe '#tags' do
subject(:tags) { tracer.tags }
let(:env_tags) { {} }

it { is_expected.to be_a_kind_of(Hash) }
before { allow(Datadog::Environment).to receive(:tags).and_return(env_tags) }

context 'by default' do
it { is_expected.to be env_tags }
end

context 'when equivalent String and Symbols are added' do
shared_examples 'equivalent tags' do
Expand Down Expand Up @@ -52,30 +57,6 @@
end
end
end

context "when #{Datadog::Ext::Environment::ENV_ENVIRONMENT}" do
context 'is not defined' do
around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => nil) do
example.run
end
end

it { is_expected.to_not include(:env) }
end

context 'is defined' do
let(:environment) { 'my-env' }

around do |example|
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => environment) do
example.run
end
end

it { is_expected.to include(env: environment) }
end
end
end

describe '#start_span' do
Expand Down

0 comments on commit a22c211

Please sign in to comment.