diff --git a/lib/datadog/core/configuration/base.rb b/lib/datadog/core/configuration/base.rb index 4bd70f4ec0e..aa4666260ea 100644 --- a/lib/datadog/core/configuration/base.rb +++ b/lib/datadog/core/configuration/base.rb @@ -1,4 +1,3 @@ -require_relative '../environment/variable_helpers' require_relative 'options' module Datadog @@ -8,8 +7,6 @@ module Configuration # @public_api module Base def self.included(base) - base.extend(Core::Environment::VariableHelpers) - base.include(Core::Environment::VariableHelpers) base.include(Options) base.extend(ClassMethods) diff --git a/lib/datadog/core/environment/variable_helpers.rb b/lib/datadog/core/environment/variable_helpers.rb index ac56e7b3dc3..65e2ddf1c4a 100644 --- a/lib/datadog/core/environment/variable_helpers.rb +++ b/lib/datadog/core/environment/variable_helpers.rb @@ -29,75 +29,6 @@ def env_to_bool(var, default = nil, deprecation_warning: true) end end - # Reads an environment variable as an Integer. - # - # @param [String] var environment variable - # @param [Array] var list of environment variables - # @param [Integer] default the default value if the keys in `var` are not present in the environment - # @param [Boolean] deprecation_warning when `var` is a list, record a deprecation log when - # the first key in `var` is not used. - # @return [Integer] if the environment value is a valid Integer - # @return [default] if the environment value is not found - def env_to_int(var, default = nil, deprecation_warning: true) - var = decode_array(var, deprecation_warning) - var && ENV.key?(var) ? ENV[var].to_i : default - end - - # Reads an environment variable as a Float. - # - # @param [String] var environment variable - # @param [Array] var list of environment variables - # @param [Float] default the default value if the keys in `var` are not present in the environment - # @param [Boolean] deprecation_warning when `var` is a list, record a deprecation log when - # the first key in `var` is not used. - # @return [Float] if the environment value is a valid Float - # @return [default] if the environment value is not found - def env_to_float(var, default = nil, deprecation_warning: true) - var = decode_array(var, deprecation_warning) - var && ENV.key?(var) ? ENV[var].to_f : default - end - - # Parses comma- or space-separated lists. - # - # If a comma is present, then the list is considered comma-separated. - # Otherwise, it is considered space-separated. - # - # After the entries are separated, commas and whitespaces that are - # either trailing or leading are trimmed. - # - # Empty entries, after trimmed, are also removed from the result. - # - # @param [String] var environment variable - # @param [Array] var list of environment variables - # @param [Array] default the default value if the keys in `var` are not present in the environment - # @param [Boolean] deprecation_warning when `var` is a list, record a deprecation log when - # the first key in `var` is not used. - # @return [Array] if the environment value is a valid list - # @return [default] if the environment value is not found - def env_to_list(var, default = [], comma_separated_only:, deprecation_warning: true) - var = decode_array(var, deprecation_warning) - if var && ENV.key?(var) - value = ENV[var] - - values = if value.include?(',') || comma_separated_only - value.split(',') - else - value.split(' ') # rubocop:disable Style/RedundantArgument - end - - values.map! do |v| - v.gsub!(/\A[\s,]*|[\s,]*\Z/, '') - - v.empty? ? nil : v - end - - values.compact! - values - else - default - end - end - private def decode_array(var, deprecation_warning) diff --git a/lib/datadog/tracing/configuration/settings.rb b/lib/datadog/tracing/configuration/settings.rb index 8270e0271f6..b934792997e 100644 --- a/lib/datadog/tracing/configuration/settings.rb +++ b/lib/datadog/tracing/configuration/settings.rb @@ -1,4 +1,5 @@ require_relative '../../tracing/configuration/ext' +require_relative '../../core/environment/variable_helpers' require_relative 'http' module Datadog @@ -425,7 +426,7 @@ def self.extended(base) option :enabled do |o| o.type :bool o.default do - disabled = env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_DISABLED) + disabled = Core::Environment::VariableHelpers.env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_DISABLED) enabled = if disabled.nil? false @@ -438,7 +439,7 @@ def self.extended(base) end # ENABLED env var takes precedence over deprecated DISABLED - env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_ENABLED, enabled) + Core::Environment::VariableHelpers.env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_ENABLED, enabled) end end diff --git a/sig/datadog/appsec/configuration/settings.rbs b/sig/datadog/appsec/configuration/settings.rbs index 0d069ec4de6..41f594921ff 100644 --- a/sig/datadog/appsec/configuration/settings.rbs +++ b/sig/datadog/appsec/configuration/settings.rbs @@ -3,7 +3,6 @@ module Datadog module Configuration # Settings module Settings - extend Datadog::Core::Environment::VariableHelpers extend Datadog::Core::Configuration::Base::ClassMethods include Datadog::Core::Configuration::Base::InstanceMethods extend Datadog::Core::Configuration::Options::ClassMethods diff --git a/sig/datadog/core/environment/variable_helpers.rbs b/sig/datadog/core/environment/variable_helpers.rbs index 1bbf6bcb8ed..6c7ff2607f7 100644 --- a/sig/datadog/core/environment/variable_helpers.rbs +++ b/sig/datadog/core/environment/variable_helpers.rbs @@ -6,12 +6,6 @@ module Datadog def env_to_bool: (untyped var, ?untyped? default) -> untyped - def env_to_int: (untyped var, ?untyped? default) -> untyped - - def env_to_float: (untyped var, ?untyped? default) -> untyped - - def env_to_list: (untyped var, ?untyped default) -> untyped - private def decode_array: (untyped var) -> untyped diff --git a/spec/datadog/core/configuration/base_spec.rb b/spec/datadog/core/configuration/base_spec.rb index ca535dfb75c..75c1c8d04e7 100644 --- a/spec/datadog/core/configuration/base_spec.rb +++ b/spec/datadog/core/configuration/base_spec.rb @@ -51,8 +51,6 @@ describe 'instance behavior' do subject(:base_object) { base_class.new } - it { is_expected.to be_a_kind_of(Datadog::Core::Environment::VariableHelpers) } - describe '#initialize' do subject(:base_object) { base_class.new(options) } diff --git a/spec/datadog/core/environment/variable_helpers_spec.rb b/spec/datadog/core/environment/variable_helpers_spec.rb index c5612543ef1..06403c6af76 100644 --- a/spec/datadog/core/environment/variable_helpers_spec.rb +++ b/spec/datadog/core/environment/variable_helpers_spec.rb @@ -100,188 +100,4 @@ include_context 'with deprecated options' end end - - describe '::env_to_int' do - subject(:env_to_int) { variable_helpers.env_to_int(var, **options) } - - context 'when env var is not defined' do - context 'and default is not defined' do - it { is_expected.to be nil } - end - - context 'and default is defined' do - subject(:env_to_int) { variable_helpers.env_to_int(var, default) } - - let(:default) { double } - - it { is_expected.to be default } - end - end - - context 'when env var is set as' do - include_context 'env var' - - context '0' do - let(:env_value) { '0' } - - it { is_expected.to eq 0 } - end - - context '1' do - let(:env_value) { '1' } - - it { is_expected.to eq 1 } - end - - context '1.5' do - let(:env_value) { '1.5' } - - it { is_expected.to eq 1 } - end - - context 'test' do - let(:env_value) { 'test' } - - it { is_expected.to eq 0 } - end - - include_context 'with deprecated options' - end - end - - describe '::env_to_float' do - subject(:env_to_float) { variable_helpers.env_to_float(var, **options) } - - context 'when env var is not defined' do - context 'and default is not defined' do - it { is_expected.to be nil } - end - - context 'and default is defined' do - subject(:env_to_float) { variable_helpers.env_to_float(var, default) } - - let(:default) { double } - - it { is_expected.to be default } - end - end - - context 'when env var is set as' do - include_context 'env var' - - context '0' do - let(:env_value) { '0' } - - it { is_expected.to eq 0.0 } - end - - context '1' do - let(:env_value) { '1' } - - it { is_expected.to eq 1.0 } - end - - context '1.5' do - let(:env_value) { '1.5' } - - it { is_expected.to eq 1.5 } - end - - context 'test' do - let(:env_value) { 'test' } - - it { is_expected.to eq 0.0 } - end - - include_context 'with deprecated options' - end - end - - describe '::env_to_list' do - subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: false, **options) } - - context 'when env var is not defined' do - context 'and default is not defined' do - it { is_expected.to eq([]) } - end - - context 'and default is defined' do - subject(:env_to_list) { variable_helpers.env_to_list(var, default, comma_separated_only: false) } - - let(:default) { double } - - it { is_expected.to be default } - end - end - - context 'when env var is set' do - include_context 'env var' - - context '\'\'' do - let(:env_value) { '' } - - it { is_expected.to eq([]) } - end - - context ',' do - let(:env_value) { ',' } - - it { is_expected.to eq([]) } - end - - context '1' do - let(:env_value) { '1' } - - it { is_expected.to eq(['1']) } - end - - context '1,2' do - let(:env_value) { '1,2' } - - it { is_expected.to eq(%w[1 2]) } - end - - context ' 1 , 2 , 3 ' do - let(:env_value) { ' 1 , 2 , 3 ' } - - it { is_expected.to eq(%w[1 2 3]) } - end - - context '1 2 3' do - let(:env_value) { '1 2 3' } - - it { is_expected.to eq(%w[1 2 3]) } - end - - context '1,2 3' do - let(:env_value) { '1,2 3' } - - it { is_expected.to eq(['1', '2 3']) } - end - - context ' 1 2 3 ' do - let(:env_value) { ' 1 2 3 ' } - - it { is_expected.to eq(%w[1 2 3]) } - end - - context '1,, ,2,3,' do - let(:env_value) { '1,, ,2,3,' } - - it { is_expected.to eq(%w[1 2 3]) } - end - - context 'and comma_separated_only is set' do - subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: true) } - - context 'value with space' do - let(:env_value) { 'value with space' } - - it { is_expected.to eq(['value with space']) } - end - end - - include_context 'with deprecated options' - end - end end