diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index c27a6c90dab..d512872e0b3 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -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 ``` diff --git a/lib/ddtrace/configuration/base.rb b/lib/ddtrace/configuration/base.rb index 1ea19789728..ed19f693dca 100644 --- a/lib/ddtrace/configuration/base.rb +++ b/lib/ddtrace/configuration/base.rb @@ -7,6 +7,7 @@ module Configuration module Base def self.included(base) base.send(:extend, Datadog::Environment::Helpers) + base.send(:include, Datadog::Environment::Helpers) base.send(:include, Options) base.send(:extend, ClassMethods) @@ -23,7 +24,7 @@ def settings(name, &block) settings_class = new_settings_class(&block) option(name) do |o| - o.default -> { settings_class.new } + o.default { settings_class.new } o.lazy o.resetter do |value| value.reset! if value.respond_to?(:reset!) diff --git a/lib/ddtrace/configuration/option.rb b/lib/ddtrace/configuration/option.rb index 227954c0cfb..8fe4d40005b 100644 --- a/lib/ddtrace/configuration/option.rb +++ b/lib/ddtrace/configuration/option.rb @@ -26,7 +26,7 @@ def get elsif definition.delegate_to context_eval(&definition.delegate_to) else - set(definition.default_value) + set(default_value) end end @@ -42,6 +42,14 @@ def reset end end + def default_value + if definition.lazy + context_eval(&definition.default) + else + definition.default + end + end + private def context_exec(*args, &block) diff --git a/lib/ddtrace/configuration/option_definition.rb b/lib/ddtrace/configuration/option_definition.rb index 1565052ebe7..fe4ec64e4c7 100644 --- a/lib/ddtrace/configuration/option_definition.rb +++ b/lib/ddtrace/configuration/option_definition.rb @@ -27,10 +27,6 @@ def initialize(name, meta = {}, &block) @setter = meta[:setter] || block || IDENTITY end - def default_value - lazy ? @default.call : @default - end - # Creates a new Option, bound to the context provided. def build(context) Option.new(self, context) diff --git a/lib/ddtrace/configuration/settings.rb b/lib/ddtrace/configuration/settings.rb index 1542e8e647b..2ada9181519 100644 --- a/lib/ddtrace/configuration/settings.rb +++ b/lib/ddtrace/configuration/settings.rb @@ -19,18 +19,21 @@ class Settings # Configuration options # option :analytics_enabled do |o| + # TODO: Raise deprecation warning o.default { env_to_bool(Ext::Analytics::ENV_TRACE_ANALYTICS_ENABLED, nil) } o.lazy end - option :report_hostname do |o| - o.default { env_to_bool(Ext::NET::ENV_REPORT_HOSTNAME, false) } - o.lazy - end + settings :diagnostics do + option :health_metrics do |o| + o.default do + Datadog::Diagnostics::Health::Metrics.new( + enabled: env_to_bool(Datadog::Ext::Diagnostics::Health::Metrics::ENV_ENABLED, false) + ) + end - option :runtime_metrics_enabled do |o| - o.default { env_to_bool(Ext::Runtime::Metrics::ENV_ENABLED, false) } - o.lazy + o.lazy + end end settings :distributed_tracing do @@ -57,6 +60,31 @@ class Settings end end + option :env do |o| + o.default { ENV.fetch(Ext::Environment::ENV_ENVIRONMENT, nil) } + o.lazy + end + + option :report_hostname do |o| + o.default { env_to_bool(Ext::NET::ENV_REPORT_HOSTNAME, false) } + o.lazy + end + + # Backwards compatibility for configuring runtime metrics e.g. `c.runtime_metrics enabled: true` + def runtime_metrics(options = nil) + runtime_metrics = get_option(:tracer).writer.runtime_metrics + return runtime_metrics if options.nil? + + # TODO: Raise deprecation warning + runtime_metrics.configure(options) + end + + option :runtime_metrics_enabled do |o| + # TODO: Raise deprecation warning + o.default { env_to_bool(Ext::Runtime::Metrics::ENV_ENABLED, false) } + o.lazy + end + settings :sampling do option :default_rate do |o| o.default { env_to_float(Ext::Sampling::ENV_SAMPLE_RATE, nil) } @@ -69,16 +97,29 @@ class Settings end end - settings :diagnostics do - option :health_metrics do |o| - o.default do - Datadog::Diagnostics::Health::Metrics.new( - enabled: env_to_bool(Datadog::Ext::Diagnostics::Health::Metrics::ENV_ENABLED, false) - ) + option :service do |o| + o.default { ENV.fetch(Ext::Environment::ENV_SERVICE, 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 - o.lazy + # 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 :tracer do |o| @@ -99,20 +140,34 @@ class Settings tracer.tap do |t| unless options.nil? t.configure(options) - Datadog::Logger.log = options[:log] if options[:log] - t.set_tags(options[:tags]) if options[:tags] - t.set_tags(env: options[:env]) if options[:env] - Datadog::Logger.debug_logging = options.fetch(:debug, false) + + if options[:log] + # TODO: Raise deprecation warning + Datadog::Logger.log = options[:log] + end + + if options[:tags] + # TODO: Raise deprecation warning + t.set_tags(options[:tags]) + end + + if options[:env] + # TODO: Raise deprecation warning + t.set_tags(env: options[:env]) + end + + if options.key?(:debug) + # TODO: Raise deprecation warning + Datadog::Logger.debug_logging = options[:debug] + end end end end end - def runtime_metrics(options = nil) - runtime_metrics = get_option(:tracer).writer.runtime_metrics - return runtime_metrics if options.nil? - - runtime_metrics.configure(options) + option :version do |o| + o.default { ENV.fetch(Ext::Environment::ENV_VERSION, nil) } + o.lazy end end end diff --git a/lib/ddtrace/contrib/presto/configuration/settings.rb b/lib/ddtrace/contrib/presto/configuration/settings.rb index ab21cd3afd3..1c24b118d4b 100644 --- a/lib/ddtrace/contrib/presto/configuration/settings.rb +++ b/lib/ddtrace/contrib/presto/configuration/settings.rb @@ -7,13 +7,15 @@ module Presto module Configuration # Custom settings for the Presto integration class Settings < Contrib::Configuration::Settings - option :analytics_enabled, - default: -> { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) }, - lazy: true + option :analytics_enabled do |o| + o.default { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) } + o.lazy + end - option :analytics_sample_rate, - default: -> { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) }, - lazy: true + option :analytics_sample_rate do |o| + o.default { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) } + o.lazy + end option :service_name, default: Ext::SERVICE_NAME end diff --git a/lib/ddtrace/correlation.rb b/lib/ddtrace/correlation.rb index 5873a58c4fd..a10afb9577f 100644 --- a/lib/ddtrace/correlation.rb +++ b/lib/ddtrace/correlation.rb @@ -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 diff --git a/lib/ddtrace/environment.rb b/lib/ddtrace/environment.rb index d0a92323f6a..806923d8787 100644 --- a/lib/ddtrace/environment.rb +++ b/lib/ddtrace/environment.rb @@ -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) diff --git a/lib/ddtrace/metrics.rb b/lib/ddtrace/metrics.rb index 83346fc15fd..4eec8e23bbb 100644 --- a/lib/ddtrace/metrics.rb +++ b/lib/ddtrace/metrics.rb @@ -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 diff --git a/lib/ddtrace/span.rb b/lib/ddtrace/span.rb index c4f11a2fd19..96473047a8e 100644 --- a/lib/ddtrace/span.rb +++ b/lib/ddtrace/span.rb @@ -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) diff --git a/lib/ddtrace/tracer.rb b/lib/ddtrace/tracer.rb index ae54373c173..0826d96466a 100644 --- a/lib/ddtrace/tracer.rb +++ b/lib/ddtrace/tracer.rb @@ -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) @@ -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. diff --git a/spec/ddtrace/configuration/base_spec.rb b/spec/ddtrace/configuration/base_spec.rb index b4a593c1f96..8511682c00d 100644 --- a/spec/ddtrace/configuration/base_spec.rb +++ b/spec/ddtrace/configuration/base_spec.rb @@ -18,13 +18,16 @@ let(:name) { :debug } let(:block) { proc { option :enabled } } - it 'adds a settings option' do - settings + describe 'defines a settings option' do + subject(:definition) { base_class.options[name] } + before { settings } - base_class.options[name].tap do |option| - expect(option).to be_a_kind_of(Datadog::Configuration::OptionDefinition) - expect(option.default_value).to be_a_kind_of(described_class) - expect(option.default_value.option_defined?(:enabled)).to be true + it { is_expected.to be_a_kind_of(Datadog::Configuration::OptionDefinition) } + + describe 'when instantiated' do + subject(:option) { Datadog::Configuration::Option.new(definition, self) } + it { expect(option.default_value).to be_a_kind_of(described_class) } + it { expect(option.default_value.option_defined?(:enabled)).to be true } end end end @@ -34,6 +37,8 @@ describe 'instance behavior' do subject(:base_object) { base_class.new } + it { is_expected.to be_a_kind_of(Datadog::Environment::Helpers) } + describe '#initialize' do subject(:base_object) { base_class.new(options) } let(:options) { { foo: :bar } } diff --git a/spec/ddtrace/configuration/option_definition_spec.rb b/spec/ddtrace/configuration/option_definition_spec.rb index 6bf07638948..68a641d174c 100644 --- a/spec/ddtrace/configuration/option_definition_spec.rb +++ b/spec/ddtrace/configuration/option_definition_spec.rb @@ -126,24 +126,6 @@ end end - describe '#default_value' do - subject(:result) { definition.default_value } - let(:meta) { { default: default } } - let(:default) { double('default') } - - context 'when lazy is true' do - let(:meta) { super().merge(lazy: true) } - let(:default_value) { double('default_value') } - before(:each) { expect(default).to receive(:call).and_return(default_value) } - it { is_expected.to be default_value } - end - - context 'when lazy is false' do - let(:meta) { super().merge(lazy: false) } - it { is_expected.to be default } - end - end - describe '#build' do subject(:build) { definition.build(context) } let(:context) { double('context') } diff --git a/spec/ddtrace/configuration/option_spec.rb b/spec/ddtrace/configuration/option_spec.rb index eeec1c91b36..158cf0d07f4 100644 --- a/spec/ddtrace/configuration/option_spec.rb +++ b/spec/ddtrace/configuration/option_spec.rb @@ -7,15 +7,17 @@ let(:definition) do instance_double( Datadog::Configuration::OptionDefinition, - default_value: default_value, + default: default, delegate_to: delegate, + lazy: lazy, on_set: nil, resetter: nil, setter: setter ) end - let(:default_value) { double('default value') } + let(:default) { double('default') } let(:delegate) { nil } + let(:lazy) { false } let(:setter) { proc { setter_value } } let(:setter_value) { double('setter_value') } let(:context) { double('configuration object') } @@ -136,24 +138,24 @@ context 'hasn\'t been called' do before do expect(context).to receive(:instance_exec) do |*args, &block| - expect(args.first).to be(default_value) + expect(args.first).to be(default) expect(block).to be setter - default_value + default end end - it { is_expected.to be(default_value) } + it { is_expected.to be(default) } context 'and #get is called twice' do before do - expect(definition).to receive(:default_value) + expect(definition).to receive(:default) .once - .and_return(default_value) + .and_return(default) end it 'keeps and re-uses the same default object' do - is_expected.to be default_value - expect(option.get).to be default_value + is_expected.to be default + expect(option.get).to be default end end end @@ -185,7 +187,7 @@ before do allow(definition).to receive(:resetter).and_return nil allow(context).to receive(:instance_exec).with(value, nil, &setter) - allow(context).to receive(:instance_exec).with(default_value, nil, &setter).and_return(default_value) + allow(context).to receive(:instance_exec).with(default, nil, &setter).and_return(default) option.set(value) end @@ -193,7 +195,7 @@ context 'then #get is invoked' do subject(:get) { option.get } before { reset } - it { is_expected.to be(default_value) } + it { is_expected.to be(default) } end end @@ -219,4 +221,29 @@ end end end + + describe '#default_value' do + subject(:default_value) { option.default_value } + let(:default) { double('default') } + + context 'when lazy is true' do + let(:lazy) { true } + let(:default) { proc {} } + let(:block_default) { double('block default') } + + before do + expect(context).to receive(:instance_eval) do |&block| + expect(block).to be default + block_default + end + end + + it { is_expected.to be block_default } + end + + context 'when lazy is false' do + let(:lazy) { false } + it { is_expected.to be default } + end + end end diff --git a/spec/ddtrace/configuration/settings_spec.rb b/spec/ddtrace/configuration/settings_spec.rb index fd3155ae53d..7c9561dc35c 100644 --- a/spec/ddtrace/configuration/settings_spec.rb +++ b/spec/ddtrace/configuration/settings_spec.rb @@ -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 } @@ -45,18 +206,56 @@ end describe '#tracer' do - let(:tracer) { Datadog::Tracer.new } - let(:debug_state) { Datadog::Logger.debug_logging } - let(:custom_log) { Logger.new(STDOUT) } + context 'given :log' do + let(:custom_log) { Logger.new(STDOUT, level: Logger::INFO) } - context 'given some settings' do - before(:each) do + before do @original_log = Datadog::Logger.log + settings.tracer(log: custom_log) + end + + after do + Datadog::Logger.log = @original_log + end + + it 'uses the logger for logging' do + expect(Datadog::Logger.log).to eq(custom_log) + end + end + + context 'given :debug' do + subject(:configure) { settings.tracer(debug: debug) } + + shared_examples_for 'debug toggle' do + before { Datadog::Logger.debug_logging = !debug } + after { Datadog::Logger.debug_logging = false } + + it do + expect { configure }.to change { Datadog::Logger.debug_logging } + .from(!debug) + .to(debug) + end + end + + context 'as true' do + it_behaves_like 'debug toggle' do + let(:debug) { true } + end + end + + context 'as false' do + it_behaves_like 'debug toggle' do + let(:debug) { false } + end + end + end + + context 'given some settings' do + let(:tracer) { Datadog::Tracer.new } + before do settings.tracer( enabled: false, - debug: !debug_state, - log: custom_log, hostname: 'tracer.host.com', port: 1234, env: :config_test, @@ -66,15 +265,12 @@ ) end - after(:each) do - Datadog::Logger.debug_logging = debug_state - Datadog::Logger.log = @original_log + after do + Datadog::Logger.debug_logging = false end it 'applies settings correctly' do expect(tracer.enabled).to be false - expect(debug_state).to be false - expect(Datadog::Logger.log).to eq(custom_log) expect(tracer.writer.transport.current_api.adapter.hostname).to eq('tracer.host.com') expect(tracer.writer.transport.current_api.adapter.port).to eq(1234) expect(tracer.tags['env']).to eq(:config_test) diff --git a/spec/ddtrace/contrib/presto/client_spec.rb b/spec/ddtrace/contrib/presto/client_spec.rb index 061b2e6f65a..08af52348bb 100644 --- a/spec/ddtrace/contrib/presto/client_spec.rb +++ b/spec/ddtrace/contrib/presto/client_spec.rb @@ -86,13 +86,6 @@ def suppress_warnings expect(span.get_tag('presto.model_version')).to eq(model_version) expect(span.get_tag('out.host')).to eq("#{host}:#{port}") end - - it_behaves_like 'analytics for integration' do - let(:analytics_enabled_var) { Datadog::Contrib::Presto::Ext::ENV_ANALYTICS_ENABLED } - let(:analytics_sample_rate_var) { Datadog::Contrib::Presto::Ext::ENV_ANALYTICS_SAMPLE_RATE } - end - - it_behaves_like 'measured span for integration', false end shared_examples_for 'a configurable Presto trace' do @@ -208,6 +201,8 @@ def suppress_warnings let(:analytics_enabled_var) { Datadog::Contrib::Presto::Ext::ENV_ANALYTICS_ENABLED } let(:analytics_sample_rate_var) { Datadog::Contrib::Presto::Ext::ENV_ANALYTICS_SAMPLE_RATE } end + + it_behaves_like 'measured span for integration', false end describe '#run operation' do diff --git a/spec/ddtrace/correlation_spec.rb b/spec/ddtrace/correlation_spec.rb index 97111e7bb6d..59ed2bfe1bc 100644 --- a/spec/ddtrace/correlation_spec.rb +++ b/spec/ddtrace/correlation_spec.rb @@ -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 @@ -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' @@ -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' diff --git a/spec/ddtrace/environment_spec.rb b/spec/ddtrace/environment_spec.rb deleted file mode 100644 index 32105020380..00000000000 --- a/spec/ddtrace/environment_spec.rb +++ /dev/null @@ -1,167 +0,0 @@ -require 'spec_helper' - -require 'ddtrace' -require 'ddtrace/environment' - -RSpec.describe Datadog::Environment do - describe '::env' do - subject(:env) { described_class.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) { described_class.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) { described_class.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(described_class).to receive(:env).and_return(env) } - - context 'is set' do - let(:env) { nil } - it { is_expected.to_not include('env') } - end - - context 'is not set' do - let(:env) { 'env-value' } - it { is_expected.to include('env' => env) } - end - end - - context 'and when ::version' do - before { allow(described_class).to receive(:version).and_return(version) } - - context 'is set' do - let(:version) { nil } - it { is_expected.to_not include('version') } - end - - context 'is not set' do - let(:version) { 'version-value' } - it { is_expected.to include('version' => 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(described_class).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(described_class).to receive(:version).and_return(version_value) } - - it { is_expected.to include('version' => version_value) } - end - end - end - - describe '::version' do - subject(:version) { described_class.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 -end diff --git a/spec/ddtrace/metrics_spec.rb b/spec/ddtrace/metrics_spec.rb index fab6cca1c6a..8229efa742f 100644 --- a/spec/ddtrace/metrics_spec.rb +++ b/spec/ddtrace/metrics_spec.rb @@ -638,8 +638,8 @@ ) end - context 'when Datadog::Environment.env' do - before { allow(Datadog::Environment).to receive(:env).and_return(environment) } + context 'when #env configuration setting' do + before { allow(Datadog.configuration).to receive(:env).and_return(environment) } context 'is not defined' do let(:environment) { nil } @@ -653,7 +653,7 @@ end context 'when Datadog::Environment.version' do - before { allow(Datadog::Environment).to receive(:version).and_return(version) } + before { allow(Datadog.configuration).to receive(:version).and_return(version) } context 'is not defined' do let(:version) { nil } diff --git a/spec/ddtrace/span_spec.rb b/spec/ddtrace/span_spec.rb index a3ba58d0259..bc34746c2c0 100644 --- a/spec/ddtrace/span_spec.rb +++ b/spec/ddtrace/span_spec.rb @@ -52,9 +52,9 @@ it { is_expected.to eq service_value } end - context 'is only defined in the environment' do + context 'is only defined in the configuration' do let(:env_service) { 'env-service' } - before { allow(Datadog::Environment).to receive(:service).and_return(env_service) } + before { allow(Datadog.configuration).to receive(:service).and_return(env_service) } it { is_expected.to eq env_service } end @@ -62,7 +62,7 @@ let(:default_service) { 'default-service' } before do - allow(Datadog::Environment).to receive(:service).and_return(nil) + allow(Datadog.configuration).to receive(:service).and_return(nil) allow(tracer).to receive(:default_service).and_return(default_service) end diff --git a/spec/ddtrace/tracer_spec.rb b/spec/ddtrace/tracer_spec.rb index 8ace05b0128..a06784d62e9 100644 --- a/spec/ddtrace/tracer_spec.rb +++ b/spec/ddtrace/tracer_spec.rb @@ -23,7 +23,7 @@ subject(:tags) { tracer.tags } let(:env_tags) { {} } - before { allow(Datadog::Environment).to receive(:tags).and_return(env_tags) } + before { allow(Datadog.configuration).to receive(:tags).and_return(env_tags) } context 'by default' do it { is_expected.to be env_tags }