diff --git a/lib/datadog/appsec/configuration/settings.rb b/lib/datadog/appsec/configuration/settings.rb index 16ec128b33e..fd95b1f72bd 100644 --- a/lib/datadog/appsec/configuration/settings.rb +++ b/lib/datadog/appsec/configuration/settings.rb @@ -33,9 +33,10 @@ def self.add_settings!(base) base.class_eval do settings :appsec do option :enabled do |o| - o.default { env_to_bool('DD_APPSEC_ENABLED', DEFAULT_APPSEC_ENABLED) } - o.setter do |v| - v ? true : false + o.env_var 'DD_APPSEC_ENABLED' + o.default DEFAULT_APPSEC_ENABLED + o.setter do |value| + val_to_bool(value) end end @@ -53,56 +54,62 @@ def self.add_settings!(base) end option :ruleset do |o| - o.default { ENV.fetch('DD_APPSEC_RULES', DEFAULT_APPSEC_RULESET) } + o.env_var 'DD_APPSEC_RULES' + o.default { DEFAULT_APPSEC_RULESET } + o.lazy end option :ip_denylist do |o| o.default { [] } + o.lazy end option :user_id_denylist do |o| o.default { [] } + o.lazy end option :waf_timeout do |o| - o.default { ENV.fetch('DD_APPSEC_WAF_TIMEOUT', DEFAULT_APPSEC_WAF_TIMEOUT) } # us + o.env_var 'DD_APPSEC_WAF_TIMEOUT' + o.default { DEFAULT_APPSEC_WAF_TIMEOUT } # us o.setter do |v| Datadog::Core::Utils::Duration.call(v.to_s, base: :us) end + o.lazy end option :waf_debug do |o| - o.default { env_to_bool('DD_APPSEC_WAF_DEBUG', DEFAULT_APPSEC_WAF_DEBUG) } - o.setter do |v| - v ? true : false + o.env_var 'DD_APPSEC_WAF_DEBUG' + o.default DEFAULT_APPSEC_WAF_DEBUG + o.setter do |value| + val_to_bool(value) end end option :trace_rate_limit do |o| - o.default { env_to_int('DD_APPSEC_TRACE_RATE_LIMIT', DEFAULT_APPSEC_TRACE_RATE_LIMIT) } # trace/s + o.env_var 'DD_APPSEC_TRACE_RATE_LIMIT' + o.default DEFAULT_APPSEC_TRACE_RATE_LIMIT + o.setter do |value| + val_to_int(value) + end end option :obfuscator_key_regex do |o| - o.default { ENV.fetch('DD_APPSEC_OBFUSCATION_PARAMETER_KEY_REGEXP', DEFAULT_OBFUSCATOR_KEY_REGEX) } + o.env_var 'DD_APPSEC_OBFUSCATION_PARAMETER_KEY_REGEXP' + o.default { DEFAULT_OBFUSCATOR_KEY_REGEX } + o.lazy end option :obfuscator_value_regex do |o| - o.default do - ENV.fetch( - 'DD_APPSEC_OBFUSCATION_PARAMETER_VALUE_REGEXP', - DEFAULT_OBFUSCATOR_VALUE_REGEX - ) - end + o.env_var 'DD_APPSEC_OBFUSCATION_PARAMETER_VALUE_REGEXP' + o.default { DEFAULT_OBFUSCATOR_VALUE_REGEX } + o.lazy end settings :track_user_events do option :enabled do |o| - o.default do - ENV.fetch( - 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING', - DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_ENABLED - ) - end + o.env_var 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING' + o.default { DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_ENABLED } o.setter do |v| if v v.to_s != 'disabled' @@ -110,12 +117,12 @@ def self.add_settings!(base) false end end + o.lazy end option :mode do |o| - o.default do - ENV.fetch('DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING', DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_MODE) - end + o.env_var 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING' + o.default { DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_MODE } o.setter do |v| string_value = v.to_s if APPSEC_VALID_TRACK_USER_EVENTS_MODE.include?(string_value) @@ -129,6 +136,7 @@ def self.add_settings!(base) DEFAULT_APPSEC_AUTOMATED_TRACK_USER_EVENTS_MODE end end + o.lazy end end end diff --git a/lib/datadog/ci/configuration/settings.rb b/lib/datadog/ci/configuration/settings.rb index 2a970baaed7..16681cdbd01 100644 --- a/lib/datadog/ci/configuration/settings.rb +++ b/lib/datadog/ci/configuration/settings.rb @@ -16,7 +16,11 @@ def self.add_settings!(base) base.class_eval do settings :ci do option :enabled do |o| - o.default { env_to_bool(CI::Ext::Settings::ENV_MODE_ENABLED, false) } + o.env_var CI::Ext::Settings::ENV_MODE_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end end # DEV: Alias to Datadog::Tracing::Contrib::Extensions::Configuration::Settings#instrument. @@ -36,10 +40,12 @@ def self.add_settings!(base) option :trace_flush do |o| o.default { nil } + o.lazy end option :writer_options do |o| o.default { {} } + o.lazy end end end diff --git a/lib/datadog/ci/contrib/cucumber/configuration/settings.rb b/lib/datadog/ci/contrib/cucumber/configuration/settings.rb index 662fd0af8c7..854fb33d054 100644 --- a/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +++ b/lib/datadog/ci/contrib/cucumber/configuration/settings.rb @@ -12,15 +12,21 @@ module Configuration # TODO: mark as `@public_api` when GA class Settings < Datadog::Tracing::Contrib::Configuration::Settings option :enabled do |o| - o.default { env_to_bool(Ext::ENV_ENABLED, true) } + o.env_var Ext::ENV_ENABLED + o.default true + o.setter do |value| + val_to_bool(value) + end end option :service_name do |o| o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME } + o.lazy end option :operation_name do |o| - o.default { ENV.fetch(Ext::ENV_OPERATION_NAME, Ext::OPERATION_NAME) } + o.env_var Ext::ENV_OPERATION_NAME + o.default Ext::OPERATION_NAME end end end diff --git a/lib/datadog/ci/contrib/rspec/configuration/settings.rb b/lib/datadog/ci/contrib/rspec/configuration/settings.rb index 11e88b8532c..f75f8010746 100644 --- a/lib/datadog/ci/contrib/rspec/configuration/settings.rb +++ b/lib/datadog/ci/contrib/rspec/configuration/settings.rb @@ -12,15 +12,21 @@ module Configuration # TODO: mark as `@public_api` when GA class Settings < Datadog::Tracing::Contrib::Configuration::Settings option :enabled do |o| - o.default { env_to_bool(Ext::ENV_ENABLED, true) } + o.env_var Ext::ENV_ENABLED + o.default true + o.setter do |value| + val_to_bool(value) + end end option :service_name do |o| o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME } + o.lazy end option :operation_name do |o| - o.default { ENV.fetch(Ext::ENV_OPERATION_NAME, Ext::OPERATION_NAME) } + o.env_var Ext::ENV_OPERATION_NAME + o.default ENV[Ext::OPERATION_NAME] end end end diff --git a/lib/datadog/core/configuration/settings.rb b/lib/datadog/core/configuration/settings.rb index 3edffa85348..aca7f7ea12d 100644 --- a/lib/datadog/core/configuration/settings.rb +++ b/lib/datadog/core/configuration/settings.rb @@ -79,7 +79,9 @@ def initialize(*_) # @default `DD_API_KEY` environment variable, otherwise `nil` # @return [String,nil] option :api_key do |o| - o.default { ENV.fetch(Core::Environment::Ext::ENV_API_KEY, nil) } + o.env_var Core::Environment::Ext::ENV_API_KEY + o.default { nil } + o.lazy end # Datadog diagnostic settings. @@ -98,7 +100,11 @@ def initialize(*_) # @default `DD_TRACE_DEBUG` environment variable, otherwise `false` # @return [Boolean] option :debug do |o| - o.default { env_to_bool(Datadog::Core::Configuration::Ext::Diagnostics::ENV_DEBUG_ENABLED, false) } + o.env_var Datadog::Core::Configuration::Ext::Diagnostics::ENV_DEBUG_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end o.on_set do |enabled| # Enable rich debug print statements. # We do not need to unnecessarily load 'pp' unless in debugging mode. @@ -115,7 +121,11 @@ def initialize(*_) # @default `DD_HEALTH_METRICS_ENABLED` environment variable, otherwise `false` # @return [Boolean] option :enabled do |o| - o.default { env_to_bool(Datadog::Core::Configuration::Ext::Diagnostics::ENV_HEALTH_METRICS_ENABLED, false) } + o.env_var Datadog::Core::Configuration::Ext::Diagnostics::ENV_HEALTH_METRICS_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end end # {Datadog::Statsd} instance to collect health metrics. @@ -140,7 +150,11 @@ def initialize(*_) # @return [Boolean,nil] option :enabled do |o| # Defaults to nil as we want to know when the default value is being used - o.default { env_to_bool(Datadog::Core::Configuration::Ext::Diagnostics::ENV_STARTUP_LOGS_ENABLED, nil) } + o.env_var Datadog::Core::Configuration::Ext::Diagnostics::ENV_STARTUP_LOGS_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end end end end @@ -154,7 +168,8 @@ def initialize(*_) o.setter { |v| v.to_s if v } # NOTE: env also gets set as a side effect of tags. See the WORKAROUND note in #initialize for details. - o.default { ENV.fetch(Core::Environment::Ext::ENV_ENVIRONMENT, nil) } + o.env_var Core::Environment::Ext::ENV_ENVIRONMENT + o.default nil end # Internal `Datadog.logger` configuration. @@ -189,7 +204,11 @@ def initialize(*_) # @default `DD_PROFILING_ENABLED` environment variable, otherwise `false` # @return [Boolean] option :enabled do |o| - o.default { env_to_bool(Profiling::Ext::ENV_ENABLED, false) } + o.env_var Profiling::Ext::ENV_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end end # @public_api @@ -222,7 +241,11 @@ def initialize(*_) # # @default `DD_PROFILING_MAX_FRAMES` environment variable, otherwise 400 option :max_frames do |o| - o.default { env_to_int(Profiling::Ext::ENV_MAX_FRAMES, 400) } + o.env_var Profiling::Ext::ENV_MAX_FRAMES + o.default 400 + o.setter do |value| + val_to_int(value) + end end # @public_api @@ -234,7 +257,11 @@ def initialize(*_) # @default `DD_PROFILING_ENDPOINT_COLLECTION_ENABLED` environment variable, otherwise `true` # @return [Boolean] option :enabled do |o| - o.default { env_to_bool(Profiling::Ext::ENV_ENDPOINT_COLLECTION_ENABLED, true) } + o.env_var Profiling::Ext::ENV_ENDPOINT_COLLECTION_ENABLED + o.default true + o.setter do |value| + val_to_bool(value) + end end end end @@ -276,7 +303,11 @@ def initialize(*_) # # @default `DD_PROFILING_FORCE_ENABLE_LEGACY` environment variable, otherwise `false` option :force_enable_legacy_profiler do |o| - o.default { env_to_bool('DD_PROFILING_FORCE_ENABLE_LEGACY', false) } + o.env_var 'DD_PROFILING_FORCE_ENABLE_LEGACY' + o.default false + o.setter do |value| + val_to_bool(value) + end o.on_set do |value| if value Datadog.logger.warn( @@ -308,7 +339,10 @@ def initialize(*_) # # @default `DD_PROFILING_FORCE_ENABLE_GC` environment variable, otherwise `false` option :force_enable_gc_profiling do |o| - o.default { env_to_bool('DD_PROFILING_FORCE_ENABLE_GC', false) } + o.env_var 'DD_PROFILING_FORCE_ENABLE_GC' + o.setter do |value| + val_to_bool(value) + end end # Can be used to enable/disable the Datadog::Profiling.allocation_count feature. @@ -329,7 +363,11 @@ def initialize(*_) # # @default `DD_PROFILING_SKIP_MYSQL2_CHECK` environment variable, otherwise `false` option :skip_mysql2_check do |o| - o.default { env_to_bool('DD_PROFILING_SKIP_MYSQL2_CHECK', false) } + o.env_var 'DD_PROFILING_SKIP_MYSQL2_CHECK' + o.default false + o.setter do |value| + val_to_bool(value) + end end # The profiler gathers data by sending `SIGPROF` unix signals to Ruby application threads. @@ -352,14 +390,15 @@ def initialize(*_) # # @default `DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED` environment variable as a boolean, otherwise `:auto` option :no_signals_workaround_enabled do |o| - o.default { env_to_bool('DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED', :auto) } - end - - # Enables data collection for the timeline feature. This is still experimental and not recommended yet. - # - # @default `DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED` environment variable as a boolean, otherwise `false` - option :experimental_timeline_enabled do |o| - o.default { env_to_bool('DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED', false) } + o.env_var 'DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED' + o.default :auto + o.setter do |value| + if value != :auto + val_to_bool(value) + else + value + end + end end end @@ -369,8 +408,11 @@ def initialize(*_) # # @default `DD_PROFILING_UPLOAD_TIMEOUT` environment variable, otherwise `30.0` option :timeout_seconds do |o| - o.setter { |value| value.nil? ? 30.0 : value.to_f } - o.default { env_to_float(Profiling::Ext::ENV_UPLOAD_TIMEOUT, 30.0) } + o.env_var Profiling::Ext::ENV_UPLOAD_TIMEOUT + o.default 30.0 + o.setter do |value| + value ? val_to_float(value) : 30.0 + end end end end @@ -383,10 +425,14 @@ def initialize(*_) # @default `DD_RUNTIME_METRICS_ENABLED` environment variable, otherwise `false` # @return [Boolean] option :enabled do |o| - o.default { env_to_bool(Core::Runtime::Ext::Metrics::ENV_ENABLED, false) } + o.env_var Core::Runtime::Ext::Metrics::ENV_ENABLED + o.default false + o.setter do |value| + val_to_bool(value) + end end - option :opts, default: ->(_i) { {} } + option :opts, default: ->(_i) { {} }, lazy: true option :statsd end @@ -399,7 +445,9 @@ def initialize(*_) o.setter { |v| v.to_s if v } # NOTE: service also gets set as a side effect of tags. See the WORKAROUND note in #initialize for details. - o.default { ENV.fetch(Core::Environment::Ext::ENV_SERVICE, Core::Environment::Ext::FALLBACK_SERVICE_NAME) } + o.env_var Core::Environment::Ext::ENV_SERVICE + o.default { Core::Environment::Ext::FALLBACK_SERVICE_NAME } + o.lazy # There's a few cases where we don't want to use the fallback service name, so this helper allows us to get a # nil instead so that one can do @@ -421,7 +469,9 @@ def initialize(*_) # @default `DD_SITE` environment variable, otherwise `nil` which sends data to `app.datadoghq.com` # @return [String,nil] option :site do |o| - o.default { ENV.fetch(Core::Environment::Ext::ENV_SITE, nil) } + o.env_var Core::Environment::Ext::ENV_SITE + o.default { nil } + o.lazy end # Default tags @@ -431,42 +481,46 @@ def initialize(*_) # @default `DD_TAGS` environment variable (in the format `'tag1:value1,tag2:value2'`), otherwise `{}` # @return [Hash] option :tags do |o| - o.default do - tags = {} - - # Parse tags from environment - env_to_list(Core::Environment::Ext::ENV_TAGS, comma_separated_only: false).each do |tag| - key, value = tag.split(':', 2) - tags[key] = value if value && !value.empty? - end + o.env_var Core::Environment::Ext::ENV_TAGS + o.default { {} } + o.setter do |new_value, old_value| + tag_list = if new_value && new_value.is_a?(String) + val_to_list(new_value, comma_separated_only: false).each_with_object({}) do |tag, tags| + key, value = tag.split(':', 2) + tags[key] = value if value && !value.empty? + end + else + new_value + end + + env_value = env + version_value = version + service_name = service_without_fallback # Override tags if defined - tags[Core::Environment::Ext::TAG_ENV] = env unless env.nil? - tags[Core::Environment::Ext::TAG_VERSION] = version unless version.nil? + tag_list[Core::Environment::Ext::TAG_ENV] = env_value unless env_value.nil? + tag_list[Core::Environment::Ext::TAG_VERSION] = version_value unless version_value.nil? - tags - end - - o.setter do |new_value, old_value| # Coerce keys to strings - string_tags = new_value.collect { |k, v| [k.to_s, v] }.to_h + string_tags = tag_list.collect { |k, v| [k.to_s, v] }.to_h # Cross-populate tag values with other settings if env.nil? && string_tags.key?(Core::Environment::Ext::TAG_ENV) self.env = string_tags[Core::Environment::Ext::TAG_ENV] end - if version.nil? && string_tags.key?(Core::Environment::Ext::TAG_VERSION) + if version_value.nil? && string_tags.key?(Core::Environment::Ext::TAG_VERSION) self.version = string_tags[Core::Environment::Ext::TAG_VERSION] end - if service_without_fallback.nil? && string_tags.key?(Core::Environment::Ext::TAG_SERVICE) + if service_name.nil? && string_tags.key?(Core::Environment::Ext::TAG_SERVICE) self.service = string_tags[Core::Environment::Ext::TAG_SERVICE] end # Merge with previous tags (old_value || {}).merge(string_tags) end + o.lazy end # The time provider used by Datadog. It must respect the interface of [Time](https://ruby-doc.org/core-3.0.1/Time.html). @@ -479,9 +533,8 @@ def initialize(*_) # @default `->{ Time.now }` # @return [Proc