Skip to content

Commit

Permalink
Merge pull request #977 from DataDog/feature/add_environment_env_var
Browse files Browse the repository at this point in the history
Add DD_ENV environment variable
  • Loading branch information
delner authored Mar 19, 2020
2 parents c0d9be7 + 4c897a9 commit e13f861
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/ddtrace/correlation.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
require 'ddtrace/ext/environment'

module Datadog
# Contains behavior for managing correlations with tracing
# e.g. Retrieve a correlation to the current trace for logging, etc.
module Correlation
# Struct representing correlation
Identifier = Struct.new(:trace_id, :span_id) do
Identifier = Struct.new(:trace_id, :span_id, :env) do
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]
end

def to_s
"dd.trace_id=#{trace_id} dd.span_id=#{span_id}"
str = "dd.trace_id=#{trace_id} dd.span_id=#{span_id}"
str += " dd.env=#{env}" unless env.nil?
str
end
end.freeze

Expand Down
7 changes: 7 additions & 0 deletions lib/ddtrace/ext/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Datadog
module Ext
module Environment
ENV_ENVIRONMENT = 'DD_ENV'.freeze
end
end
end
2 changes: 2 additions & 0 deletions lib/ddtrace/metrics.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ddtrace/ext/metrics'
require 'ddtrace/ext/environment'

require 'set'
require 'logger'
Expand Down Expand Up @@ -151,6 +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)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'logger'
require 'pathname'

require 'ddtrace/ext/environment'

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

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

# Enable priority sampling by default
activate_priority_sampling!(@sampler)
Expand Down
26 changes: 26 additions & 0 deletions spec/ddtrace/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@
expect(correlation_ids.span_id).to eq(span_id)
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

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
end
end
end
end
53 changes: 53 additions & 0 deletions spec/ddtrace/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,59 @@
end
end

RSpec.describe Datadog::Metrics::Options do
context 'when included into a class' do
subject(:instance) { options_class.new }
let(:options_class) { stub_const('OptionsClass', Class.new { include Datadog::Metrics::Options }) }

describe '#default_metric_options' do
subject(:default_metric_options) { instance.default_metric_options }

it { is_expected.to be_a_kind_of(Hash) }
it { expect(default_metric_options.frozen?).to be false }

describe ':tags' do
subject(:default_tags) { default_metric_options[:tags] }

it { is_expected.to be_a_kind_of(Array) }
it { expect(default_tags.frozen?).to be false }
it 'includes default tags' do
is_expected.to include(
"#{Datadog::Ext::Metrics::TAG_LANG}:#{Datadog::Runtime::Identity.lang}",
"#{Datadog::Ext::Metrics::TAG_LANG_INTERPRETER}:#{Datadog::Runtime::Identity.lang_interpreter}",
"#{Datadog::Ext::Metrics::TAG_LANG_VERSION}:#{Datadog::Runtime::Identity.lang_version}",
"#{Datadog::Ext::Metrics::TAG_TRACER_VERSION}:#{Datadog::Runtime::Identity.tracer_version}"
)
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
end
end
end

RSpec.describe Datadog::Metrics::Logging::Adapter do
subject(:adapter) { described_class.new(logger) }
let(:logger) { instance_double(Logger) }
Expand Down
30 changes: 30 additions & 0 deletions spec/ddtrace/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@
end
end

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

it { is_expected.to be_a_kind_of(Hash) }

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 '#trace' do
let(:name) { 'span.name' }
let(:options) { {} }
Expand Down

0 comments on commit e13f861

Please sign in to comment.