Sha256: 7b2fee81d1050d8ec977f23b23d8b694cd4abb9881f7f9ea59d5ba32d2ac068c

Contents?: true

Size: 1.76 KB

Versions: 32

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Datadog
  module Core
    # Namespace for handling application environment
    module Environment
      # Defines helper methods for environment
      # @public_api
      module VariableHelpers
        extend self

        # Reads an environment variable as a Boolean.
        #
        # @param [String] var environment variable
        # @param [Array<String>] var list of environment variables
        # @param [Boolean] 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 [Boolean] if the environment value is the string `true` or `1`
        # @return [default] if the environment value is not found
        def env_to_bool(var, default = nil, deprecation_warning: true)
          var = decode_array(var, deprecation_warning)
          if var && ENV.key?(var)
            value = ENV[var].to_s.strip
            value.downcase!
            value == 'true' || value == '1' # rubocop:disable Style/MultipleComparison
          else
            default
          end
        end

        private

        def decode_array(var, deprecation_warning)
          if var.is_a?(Array)
            var.find.with_index do |env_var, i|
              found = ENV.key?(env_var)

              # Check if we are using a non-preferred environment variable
              if deprecation_warning && found && i != 0
                Datadog::Core.log_deprecation { "#{env_var} environment variable is deprecated, use #{var.first} instead." }
              end

              found
            end
          else
            var
          end
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
datadog-2.12.2 lib/datadog/core/environment/variable_helpers.rb
datadog-2.12.1 lib/datadog/core/environment/variable_helpers.rb
datadog-2.12.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.11.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.10.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.9.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.8.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.7.1 lib/datadog/core/environment/variable_helpers.rb
datadog-2.7.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.6.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.5.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.4.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.3.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.2.0 lib/datadog/core/environment/variable_helpers.rb
ddtrace-1.23.3 lib/datadog/core/environment/variable_helpers.rb
ddtrace-1.23.2 lib/datadog/core/environment/variable_helpers.rb
datadog-2.1.0 lib/datadog/core/environment/variable_helpers.rb
datadog-2.0.0 lib/datadog/core/environment/variable_helpers.rb
ddtrace-1.23.1 lib/datadog/core/environment/variable_helpers.rb
datadog-2.0.0.beta2 lib/datadog/core/environment/variable_helpers.rb