From b35cef943830ff340774c6f78ac14ad22b45d9a6 Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Fri, 14 Jul 2023 12:51:18 +0200 Subject: [PATCH] Update core configuration to use env and type options --- lib/datadog/ci/configuration/settings.rb | 11 +- .../cucumber/configuration/settings.rb | 9 +- .../minitest/configuration/settings.rb | 9 +- .../contrib/rspec/configuration/settings.rb | 9 +- lib/datadog/core/configuration/settings.rb | 156 ++++++++++++------ .../core/diagnostics/environment_logger.rb | 9 +- lib/datadog/tracing/configuration/settings.rb | 137 ++++++++------- .../action_cable/configuration/settings.rb | 12 +- .../action_mailer/configuration/settings.rb | 12 +- .../action_pack/configuration/settings.rb | 11 +- .../action_view/configuration/settings.rb | 12 +- .../active_job/configuration/settings.rb | 17 +- .../configuration/settings.rb | 12 +- .../active_record/configuration/settings.rb | 14 +- .../active_support/configuration/settings.rb | 12 +- .../contrib/aws/configuration/settings.rb | 12 +- .../concurrent_ruby/configuration/settings.rb | 4 +- .../contrib/dalli/configuration/settings.rb | 12 +- .../delayed_job/configuration/settings.rb | 17 +- .../elasticsearch/configuration/settings.rb | 12 +- .../contrib/ethon/configuration/settings.rb | 16 +- .../contrib/excon/configuration/settings.rb | 20 ++- .../contrib/faraday/configuration/settings.rb | 21 ++- .../contrib/grape/configuration/settings.rb | 11 +- .../contrib/graphql/configuration/settings.rb | 11 +- .../contrib/grpc/configuration/settings.rb | 19 ++- .../contrib/hanami/configuration/settings.rb | 4 +- .../contrib/http/configuration/settings.rb | 34 +++- .../httpclient/configuration/settings.rb | 34 +++- .../contrib/httprb/configuration/settings.rb | 34 +++- .../contrib/kafka/configuration/settings.rb | 12 +- .../contrib/lograge/configuration/settings.rb | 4 +- .../contrib/mongodb/configuration/settings.rb | 12 +- .../contrib/mysql2/configuration/settings.rb | 21 ++- .../opensearch/configuration/settings.rb | 16 +- .../contrib/pg/configuration/settings.rb | 21 ++- .../contrib/presto/configuration/settings.rb | 12 +- .../contrib/qless/configuration/settings.rb | 16 +- .../contrib/que/configuration/settings.rb | 28 +++- .../contrib/racecar/configuration/settings.rb | 12 +- .../contrib/rack/configuration/settings.rb | 25 ++- .../contrib/rails/configuration/settings.rb | 26 +-- .../contrib/rake/configuration/settings.rb | 17 +- .../contrib/redis/configuration/settings.rb | 16 +- .../contrib/resque/configuration/settings.rb | 17 +- .../rest_client/configuration/settings.rb | 16 +- .../contrib/roda/configuration/settings.rb | 12 +- .../semantic_logger/configuration/settings.rb | 4 +- .../contrib/sequel/configuration/settings.rb | 12 +- .../shoryuken/configuration/settings.rb | 19 ++- .../contrib/sidekiq/configuration/settings.rb | 25 ++- .../contrib/sinatra/configuration/settings.rb | 17 +- .../sneakers/configuration/settings.rb | 19 ++- .../contrib/stripe/configuration/settings.rb | 12 +- .../sucker_punch/configuration/settings.rb | 12 +- .../datadog/ci/configuration/settings_spec.rb | 4 + .../core/configuration/settings_spec.rb | 22 +-- .../diagnostics/environment_logger_spec.rb | 11 +- spec/datadog/core/telemetry/collector_spec.rb | 1 + .../tracing/configuration/settings_spec.rb | 6 +- 60 files changed, 789 insertions(+), 361 deletions(-) diff --git a/lib/datadog/ci/configuration/settings.rb b/lib/datadog/ci/configuration/settings.rb index 2a970baaed7..156309c81f4 100644 --- a/lib/datadog/ci/configuration/settings.rb +++ b/lib/datadog/ci/configuration/settings.rb @@ -16,7 +16,9 @@ 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.type :bool + o.env CI::Ext::Settings::ENV_MODE_ENABLED + o.default false end # DEV: Alias to Datadog::Tracing::Contrib::Extensions::Configuration::Settings#instrument. @@ -34,12 +36,11 @@ def self.add_settings!(base) # TODO: Deprecate in the next major version, as `instrument` better describes this method's purpose alias_method :use, :instrument - option :trace_flush do |o| - o.default { nil } - end + option :trace_flush option :writer_options do |o| - o.default { {} } + o.type :hash + o.default({}) 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..7ce9f9c9622 100644 --- a/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +++ b/lib/datadog/ci/contrib/cucumber/configuration/settings.rb @@ -12,15 +12,20 @@ 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.type :bool + o.env Ext::ENV_ENABLED + o.default true end option :service_name do |o| + o.type :string o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME } end option :operation_name do |o| - o.default { ENV.fetch(Ext::ENV_OPERATION_NAME, Ext::OPERATION_NAME) } + o.type :string + o.env Ext::ENV_OPERATION_NAME + o.default Ext::OPERATION_NAME end end end diff --git a/lib/datadog/ci/contrib/minitest/configuration/settings.rb b/lib/datadog/ci/contrib/minitest/configuration/settings.rb index 52970f2799d..eb315690a7d 100644 --- a/lib/datadog/ci/contrib/minitest/configuration/settings.rb +++ b/lib/datadog/ci/contrib/minitest/configuration/settings.rb @@ -12,15 +12,20 @@ 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.type :bool + o.env Ext::ENV_ENABLED + o.default true end option :service_name do |o| + o.type :string o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME } end option :operation_name do |o| - o.default { ENV.key?(Ext::ENV_OPERATION_NAME) ? ENV[Ext::ENV_OPERATION_NAME] : Ext::OPERATION_NAME } + o.type :string + o.env 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..5c9946b06fa 100644 --- a/lib/datadog/ci/contrib/rspec/configuration/settings.rb +++ b/lib/datadog/ci/contrib/rspec/configuration/settings.rb @@ -12,15 +12,20 @@ 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.type :bool + o.env Ext::ENV_ENABLED + o.default true end option :service_name do |o| + o.type :string o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME } end option :operation_name do |o| - o.default { ENV.fetch(Ext::ENV_OPERATION_NAME, Ext::OPERATION_NAME) } + o.type :string + o.env Ext::ENV_OPERATION_NAME + o.default Ext::OPERATION_NAME end end end diff --git a/lib/datadog/core/configuration/settings.rb b/lib/datadog/core/configuration/settings.rb index b1eb701a0f5..38e4ce7286b 100644 --- a/lib/datadog/core/configuration/settings.rb +++ b/lib/datadog/core/configuration/settings.rb @@ -79,7 +79,8 @@ 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.type :string, nilable: true + o.env Core::Environment::Ext::ENV_API_KEY end # Datadog diagnostic settings. @@ -98,7 +99,9 @@ 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 Datadog::Core::Configuration::Ext::Diagnostics::ENV_DEBUG_ENABLED + o.default false + o.type :bool o.on_set do |enabled| # Enable rich debug print statements. # We do not need to unnecessarily load 'pp' unless in debugging mode. @@ -115,7 +118,9 @@ 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 Datadog::Core::Configuration::Ext::Diagnostics::ENV_HEALTH_METRICS_ENABLED + o.default false + o.type :bool end # {Datadog::Statsd} instance to collect health metrics. @@ -136,11 +141,12 @@ def initialize(*_) # If `nil`, defaults to logging startup logs when `ddtrace` detects that the application # is *not* running in a development environment. # - # @default `DD_TRACE_STARTUP_LOGS` environment variable, otherwise `nil` - # @return [Boolean,nil] + # @default `DD_TRACE_STARTUP_LOGS` environment variable, otherwise `false` + # @return [Boolean] 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 Datadog::Core::Configuration::Ext::Diagnostics::ENV_STARTUP_LOGS_ENABLED + o.default false + o.type :bool end end end @@ -154,7 +160,7 @@ 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 Core::Environment::Ext::ENV_ENVIRONMENT end # Internal `Datadog.logger` configuration. @@ -189,7 +195,9 @@ 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 Profiling::Ext::ENV_ENABLED + o.default false + o.type :bool end # @public_api @@ -222,7 +230,9 @@ 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.type :int + o.env Profiling::Ext::ENV_MAX_FRAMES + o.default 400 end # @public_api @@ -234,7 +244,9 @@ 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 Profiling::Ext::ENV_ENDPOINT_COLLECTION_ENABLED + o.default true + o.type :bool end end end @@ -276,7 +288,9 @@ 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 'DD_PROFILING_FORCE_ENABLE_LEGACY' + o.default false + o.type :bool o.on_set do |value| if value Datadog.logger.warn( @@ -308,7 +322,9 @@ 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 'DD_PROFILING_FORCE_ENABLE_GC' + o.type :bool + o.default false end # Can be used to enable/disable the Datadog::Profiling.allocation_count feature. @@ -329,7 +345,18 @@ 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.type :bool + o.env 'DD_PROFILING_SKIP_MYSQL2_CHECK' + o.default false + 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.type :bool + o.env 'DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED' + o.default false end # The profiler gathers data by sending `SIGPROF` unix signals to Ruby application threads. @@ -352,14 +379,14 @@ 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 'DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED' + o.default :auto + o.env_parser do |value| + if value + value = value.strip.downcase + ['true', '1'].include?(value) + end + end end end @@ -369,8 +396,9 @@ 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.type :float + o.env Profiling::Ext::ENV_UPLOAD_TIMEOUT + o.default 30.0 end end end @@ -383,10 +411,12 @@ 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 Core::Runtime::Ext::Metrics::ENV_ENABLED + o.default false + o.type :bool end - option :opts, default: ->(_i) { {} } + option :opts, default: {}, type: :hash option :statsd end @@ -399,7 +429,8 @@ 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 Core::Environment::Ext::ENV_SERVICE + o.default Core::Environment::Ext::FALLBACK_SERVICE_NAME # 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 +452,8 @@ 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.type :string, nilable: true + o.env Core::Environment::Ext::ENV_SITE end # Default tags @@ -431,36 +463,51 @@ 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 = {} + o.type :hash, nilable: true + o.env Core::Environment::Ext::ENV_TAGS + o.env_parser do |env_value| + values = if env_value.include?(',') + env_value.split(',') + else + env_value.split(' ') # rubocop:disable Style/RedundantArgument + end + + values.map! do |v| + v.gsub!(/\A[\s,]*|[\s,]*\Z/, '') + + v.empty? ? nil : v + end - # Parse tags from environment - env_to_list(Core::Environment::Ext::ENV_TAGS, comma_separated_only: false).each do |tag| + values.compact! + values.each_with_object({}) do |tag, tags| key, value = tag.split(':', 2) tags[key] = value if value && !value.empty? end + end + o.setter do |new_value, old_value| + raw_tags = new_value || {} - # Override tags if defined - tags[Core::Environment::Ext::TAG_ENV] = env unless env.nil? - tags[Core::Environment::Ext::TAG_VERSION] = version unless version.nil? + env_value = env + version_value = version + service_name = service_without_fallback - tags - end + # Override tags if defined + raw_tags[Core::Environment::Ext::TAG_ENV] = env_value unless env_value.nil? + raw_tags[Core::Environment::Ext::TAG_VERSION] = version_value unless version_value.nil? - 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 = raw_tags.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) + if env_value.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 @@ -479,9 +526,9 @@ def initialize(*_) # @default `->{ Time.now }` # @return [Proc