From cccd404ed5cccc5d68d25d09690614bbdc9a6a09 Mon Sep 17 00:00:00 2001 From: David Elner Date: Tue, 17 Mar 2020 16:14:24 -0400 Subject: [PATCH 1/4] Added: Datadog::Ext::Environment --- lib/ddtrace/ext/environment.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lib/ddtrace/ext/environment.rb diff --git a/lib/ddtrace/ext/environment.rb b/lib/ddtrace/ext/environment.rb new file mode 100644 index 00000000000..6a3f2d7c5eb --- /dev/null +++ b/lib/ddtrace/ext/environment.rb @@ -0,0 +1,7 @@ +module Datadog + module Ext + module Environment + ENV_ENVIRONMENT = 'DD_ENV'.freeze + end + end +end From 44cdf7d5e4c71ee3292c043b5a463b0ea215cd88 Mon Sep 17 00:00:00 2001 From: David Elner Date: Tue, 17 Mar 2020 16:15:15 -0400 Subject: [PATCH 2/4] Added: DD_ENV to default tags for Datadog::Tracer. --- lib/ddtrace/tracer.rb | 6 +++++- spec/ddtrace/tracer_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/ddtrace/tracer.rb b/lib/ddtrace/tracer.rb index 46b8ff1d39b..c98790c612d 100644 --- a/lib/ddtrace/tracer.rb +++ b/lib/ddtrace/tracer.rb @@ -3,6 +3,8 @@ require 'logger' require 'pathname' +require 'ddtrace/ext/environment' + require 'ddtrace/span' require 'ddtrace/context' require 'ddtrace/logger' @@ -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) diff --git a/spec/ddtrace/tracer_spec.rb b/spec/ddtrace/tracer_spec.rb index 12824db443f..34de71d1096 100644 --- a/spec/ddtrace/tracer_spec.rb +++ b/spec/ddtrace/tracer_spec.rb @@ -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) { {} } From 97f8cfa064d5ee8b0a617d982d9caf9a575221f0 Mon Sep 17 00:00:00 2001 From: David Elner Date: Tue, 17 Mar 2020 16:15:54 -0400 Subject: [PATCH 3/4] Added: DD_ENV to default tags for Datadog::Metrics. --- lib/ddtrace/metrics.rb | 2 ++ spec/ddtrace/metrics_spec.rb | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/ddtrace/metrics.rb b/lib/ddtrace/metrics.rb index d7484fa1290..ac3d43e0270 100644 --- a/lib/ddtrace/metrics.rb +++ b/lib/ddtrace/metrics.rb @@ -1,4 +1,5 @@ require 'ddtrace/ext/metrics' +require 'ddtrace/ext/environment' require 'set' require 'logger' @@ -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 diff --git a/spec/ddtrace/metrics_spec.rb b/spec/ddtrace/metrics_spec.rb index 9f5162ef221..f29d085b9cc 100644 --- a/spec/ddtrace/metrics_spec.rb +++ b/spec/ddtrace/metrics_spec.rb @@ -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) } From 4c897a962fe9c45f5a5262de8ecaed165bca783c Mon Sep 17 00:00:00 2001 From: David Elner Date: Tue, 17 Mar 2020 16:16:23 -0400 Subject: [PATCH 4/4] Added: DD_ENV to Datadog::Correlation. --- lib/ddtrace/correlation.rb | 9 +++++++-- spec/ddtrace/correlation_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/ddtrace/correlation.rb b/lib/ddtrace/correlation.rb index 2bddc4676ed..86ec24eafcc 100644 --- a/lib/ddtrace/correlation.rb +++ b/lib/ddtrace/correlation.rb @@ -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 diff --git a/spec/ddtrace/correlation_spec.rb b/spec/ddtrace/correlation_spec.rb index de2fbe7cb6a..a8192c8b66b 100644 --- a/spec/ddtrace/correlation_spec.rb +++ b/spec/ddtrace/correlation_spec.rb @@ -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