diff --git a/lib/datadog/core/configuration/settings.rb b/lib/datadog/core/configuration/settings.rb index c170f5c978d..cf998248f76 100644 --- a/lib/datadog/core/configuration/settings.rb +++ b/lib/datadog/core/configuration/settings.rb @@ -402,8 +402,12 @@ def initialize(*_) o.env_var 'DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED' o.default :auto o.setter do |value| - if value != :auto - val_to_bool(value) + if ['true', true, 'false', false, :auto].include?(value) + if value == :auto + value + else + val_to_bool(value) + end else value end diff --git a/lib/datadog/core/utils/variable_helpers.rb b/lib/datadog/core/utils/variable_helpers.rb index a4bb01d3104..37572265daa 100644 --- a/lib/datadog/core/utils/variable_helpers.rb +++ b/lib/datadog/core/utils/variable_helpers.rb @@ -62,8 +62,6 @@ def val_to_float(val) def val_to_list(val, default = [], comma_separated_only:) if val values = case val - when Array - val when String if val.include?(',') || comma_separated_only val.split(',') @@ -71,13 +69,21 @@ def val_to_list(val, default = [], comma_separated_only:) val.split(' ') # rubocop:disable Style/RedundantArgument end else - raise ArgumentError, "Value `#{val}` can not be converted to an Array" + begin + Array(val) + rescue ArgumentError => e + raise e.class, "Value `#{val}` can not be converted to an Array" + end end result = values.map do |v| - v.gsub!(/\A[\s,]*|[\s,]*\Z/, '') + if v.is_a?(String) + v.gsub!(/\A[\s,]*|[\s,]*\Z/, '') - v.empty? ? nil : v + v.empty? ? nil : v + else + v + end end result.compact! diff --git a/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb b/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb index 30bd17672fe..d083d5fc669 100644 --- a/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb @@ -21,9 +21,8 @@ class Settings < Contrib::Configuration::Settings option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) unless value.nil? end end diff --git a/lib/datadog/tracing/contrib/active_record/configuration/settings.rb b/lib/datadog/tracing/contrib/active_record/configuration/settings.rb index a5f54d6a888..7a4df643744 100644 --- a/lib/datadog/tracing/contrib/active_record/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/active_record/configuration/settings.rb @@ -39,7 +39,6 @@ class Settings < Contrib::Configuration::Settings option :service_name do |o| o.default do Contrib::SpanAttributeSchema.fetch_service_name( - '', Utils.adapter_name ) end diff --git a/lib/datadog/tracing/contrib/active_support/configuration/settings.rb b/lib/datadog/tracing/contrib/active_support/configuration/settings.rb index 0a90f9ec53a..f008fa28c5b 100644 --- a/lib/datadog/tracing/contrib/active_support/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/active_support/configuration/settings.rb @@ -38,7 +38,6 @@ class Settings < Contrib::Configuration::Settings option :cache_service do |o| o.default do Contrib::SpanAttributeSchema.fetch_service_name( - '', Ext::SERVICE_CACHE ) end diff --git a/lib/datadog/tracing/contrib/faraday/middleware.rb b/lib/datadog/tracing/contrib/faraday/middleware.rb index 7d77f642c65..4abdfe33f8b 100644 --- a/lib/datadog/tracing/contrib/faraday/middleware.rb +++ b/lib/datadog/tracing/contrib/faraday/middleware.rb @@ -64,7 +64,9 @@ def annotate!(span, env, options) end def handle_response(span, env, options) - span.set_error(["Error #{env[:status]}", env[:body]]) if options.fetch(:error_handler).call(env) + if options[:error_handler] && options[:error_handler].call(env) + span.set_error(["Error #{env[:status]}", env[:body]]) + end span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, env[:status]) end diff --git a/lib/datadog/tracing/contrib/grape/configuration/settings.rb b/lib/datadog/tracing/contrib/grape/configuration/settings.rb index 1fa2e5b80f5..012dc413f5a 100644 --- a/lib/datadog/tracing/contrib/grape/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/grape/configuration/settings.rb @@ -22,9 +22,8 @@ class Settings < Contrib::Configuration::Settings option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) if value end end diff --git a/lib/datadog/tracing/contrib/graphql/configuration/settings.rb b/lib/datadog/tracing/contrib/graphql/configuration/settings.rb index d2610496b5d..2c4e3a5878a 100644 --- a/lib/datadog/tracing/contrib/graphql/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/graphql/configuration/settings.rb @@ -21,9 +21,8 @@ class Settings < Contrib::Configuration::Settings option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) if value end end diff --git a/lib/datadog/tracing/contrib/http/configuration/settings.rb b/lib/datadog/tracing/contrib/http/configuration/settings.rb index 1e8727a64af..4eb69651f2c 100644 --- a/lib/datadog/tracing/contrib/http/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/http/configuration/settings.rb @@ -50,7 +50,7 @@ class Settings < Contrib::Configuration::Settings option :error_status_codes do |o| o.env_var Ext::ENV_ERROR_STATUS_CODES o.setter do |value| - val_to_list(value, 400...600, comma_separated_only: false) + val_to_list(value, Array(400...600), comma_separated_only: false) end end diff --git a/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb b/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb index 43fa5f01b66..2ff3abe6149 100644 --- a/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb @@ -50,7 +50,7 @@ class Settings < Contrib::Configuration::Settings option :error_status_codes do |o| o.env_var Ext::ENV_ERROR_STATUS_CODES o.setter do |value| - val_to_list(value, 400...600, comma_separated_only: false) + val_to_list(value, Array(400...600), comma_separated_only: false) end end diff --git a/lib/datadog/tracing/contrib/httprb/configuration/settings.rb b/lib/datadog/tracing/contrib/httprb/configuration/settings.rb index c461301a4e6..184edb0b347 100644 --- a/lib/datadog/tracing/contrib/httprb/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/httprb/configuration/settings.rb @@ -50,7 +50,7 @@ class Settings < Contrib::Configuration::Settings option :error_status_codes do |o| o.env_var Ext::ENV_ERROR_STATUS_CODES o.setter do |value| - value_to_list(value, 400...600, comma_separated_only: false) + val_to_list(value, Array(400...600), comma_separated_only: false) end end diff --git a/lib/datadog/tracing/contrib/opensearch/configuration/settings.rb b/lib/datadog/tracing/contrib/opensearch/configuration/settings.rb index c6488c3ea6e..b0dad89494e 100644 --- a/lib/datadog/tracing/contrib/opensearch/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/opensearch/configuration/settings.rb @@ -20,7 +20,7 @@ class Settings < Contrib::Configuration::Settings end option :analytics_enabled do |o| - o.env_var Ext::ENV_ANALYTICS_ENABLED, false + o.env_var Ext::ENV_ANALYTICS_ENABLED o.default false o.setter do |value| val_to_bool(value) diff --git a/lib/datadog/tracing/contrib/racecar/configuration/settings.rb b/lib/datadog/tracing/contrib/racecar/configuration/settings.rb index 94f62e39ed6..86c5d562b32 100644 --- a/lib/datadog/tracing/contrib/racecar/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/racecar/configuration/settings.rb @@ -38,7 +38,6 @@ class Settings < Contrib::Configuration::Settings option :service_name do |o| o.default do Contrib::SpanAttributeSchema.fetch_service_name( - '', Ext::DEFAULT_PEER_SERVICE_NAME ) end diff --git a/lib/datadog/tracing/contrib/rack/configuration/settings.rb b/lib/datadog/tracing/contrib/rack/configuration/settings.rb index 54f84f58e7e..30f47d2a169 100644 --- a/lib/datadog/tracing/contrib/rack/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/rack/configuration/settings.rb @@ -26,9 +26,8 @@ class Settings < Contrib::Configuration::Settings option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) if value end end diff --git a/lib/datadog/tracing/contrib/rails/configuration/settings.rb b/lib/datadog/tracing/contrib/rails/configuration/settings.rb index d1a6ff13645..ac1357fe4e9 100644 --- a/lib/datadog/tracing/contrib/rails/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/rails/configuration/settings.rb @@ -33,14 +33,13 @@ def initialize(options = {}) option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) unless value.nil? end o.on_set do |value| # Update ActionPack analytics too - Datadog.configuration.tracing[:action_pack][:analytics_enabled] = value + Datadog.configuration.tracing[:action_pack][:analytics_enabled] = value unless value.nil? end end diff --git a/lib/datadog/tracing/contrib/sequel/database.rb b/lib/datadog/tracing/contrib/sequel/database.rb index 412edf7768c..d394dc3b5b9 100644 --- a/lib/datadog/tracing/contrib/sequel/database.rb +++ b/lib/datadog/tracing/contrib/sequel/database.rb @@ -23,10 +23,7 @@ def run(sql, options = ::Sequel::OPTS) Tracing.trace(Ext::SPAN_QUERY) do |span| span.service = Datadog.configuration_for(self, :service_name) \ || Datadog.configuration.tracing[:sequel][:service_name] \ - || Contrib::SpanAttributeSchema.fetch_service_name( - '', - adapter_name - ) + || Contrib::SpanAttributeSchema.fetch_service_name(adapter_name) span.resource = opts[:query] span.span_type = Tracing::Metadata::Ext::SQL::TYPE Utils.set_common_tags(span, self) diff --git a/lib/datadog/tracing/contrib/sequel/dataset.rb b/lib/datadog/tracing/contrib/sequel/dataset.rb index 6f7f39b49c9..2d638a11c54 100644 --- a/lib/datadog/tracing/contrib/sequel/dataset.rb +++ b/lib/datadog/tracing/contrib/sequel/dataset.rb @@ -42,10 +42,7 @@ def trace_execute(super_method, sql, options, &block) Tracing.trace(Ext::SPAN_QUERY) do |span| span.service = Datadog.configuration_for(db, :service_name) \ || Datadog.configuration.tracing[:sequel][:service_name] \ - || Contrib::SpanAttributeSchema.fetch_service_name( - '', - adapter_name - ) + || Contrib::SpanAttributeSchema.fetch_service_name(adapter_name) span.resource = opts[:query] span.span_type = Tracing::Metadata::Ext::SQL::TYPE Utils.set_common_tags(span, db) diff --git a/lib/datadog/tracing/contrib/sinatra/configuration/settings.rb b/lib/datadog/tracing/contrib/sinatra/configuration/settings.rb index 4dd5f501bb9..f403a4f033d 100644 --- a/lib/datadog/tracing/contrib/sinatra/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/sinatra/configuration/settings.rb @@ -23,9 +23,8 @@ class Settings < Contrib::Configuration::Settings option :analytics_enabled do |o| o.env_var Ext::ENV_ANALYTICS_ENABLED - o.default false o.setter do |value| - val_to_bool(value) + val_to_bool(value) if value end end diff --git a/lib/datadog/tracing/contrib/span_attribute_schema.rb b/lib/datadog/tracing/contrib/span_attribute_schema.rb index 920dd74bf4e..c10d2d58507 100644 --- a/lib/datadog/tracing/contrib/span_attribute_schema.rb +++ b/lib/datadog/tracing/contrib/span_attribute_schema.rb @@ -7,7 +7,8 @@ module Contrib module SpanAttributeSchema module_function - def fetch_service_name(value, default) + # rubocop:disable Style/OptionalArguments + def fetch_service_name(value = nil, default) if value value elsif Datadog.configuration.tracing.span_attribute_schema == @@ -17,6 +18,7 @@ def fetch_service_name(value, default) default end end + # rubocop:enable Style/OptionalArguments def default_span_attribute_schema? Datadog.configuration.tracing.span_attribute_schema == diff --git a/sig/datadog/tracing/contrib/span_attribute_schema.rbs b/sig/datadog/tracing/contrib/span_attribute_schema.rbs index ca6227f0a8f..a3af2f75d35 100644 --- a/sig/datadog/tracing/contrib/span_attribute_schema.rbs +++ b/sig/datadog/tracing/contrib/span_attribute_schema.rbs @@ -2,7 +2,7 @@ module Datadog module Tracing module Contrib module SpanAttributeSchema - def self?.fetch_service_name: (untyped env, untyped default) -> untyped + def self?.fetch_service_name: (untyped value, untyped default) -> untyped def self?.default_span_attribute_schema?: () -> untyped end diff --git a/spec/datadog/core/utils/safe_dup_spec.rb b/spec/datadog/core/utils/safe_dup_spec.rb index 0821f7eef41..34b16ef7663 100644 --- a/spec/datadog/core/utils/safe_dup_spec.rb +++ b/spec/datadog/core/utils/safe_dup_spec.rb @@ -126,6 +126,10 @@ end context 'Boolean' do + before do + skip 'TrueClass and FalseClass are not frozen by default on ruby 2.1' if RUBY_VERSION < '2.2' + end + describe '.frozen_or_dup' do context 'when given a boolean' do it 'returns the original input' do diff --git a/spec/datadog/tracing/contrib/faraday/middleware_spec.rb b/spec/datadog/tracing/contrib/faraday/middleware_spec.rb index 25b232d2140..b28ba7e2072 100644 --- a/spec/datadog/tracing/contrib/faraday/middleware_spec.rb +++ b/spec/datadog/tracing/contrib/faraday/middleware_spec.rb @@ -25,6 +25,7 @@ let(:use_middleware) { true } let(:middleware_options) { {} } let(:configuration_options) { {} } + let(:custom_handler) { ->(env) { (400...600).cover?(env[:status]) } } before do Datadog.configure do |c| @@ -181,6 +182,7 @@ end context 'when there is a failing request' do + let(:middleware_options) { { error_handler: custom_handler } } subject!(:response) { client.post('/failure') } it_behaves_like 'environment service name', 'DD_TRACE_FARADAY_SERVICE_NAME' @@ -261,7 +263,6 @@ subject!(:response) { client.get('not_found') } let(:middleware_options) { { error_handler: custom_handler } } - let(:custom_handler) { ->(env) { (400...600).cover?(env[:status]) } } it { expect(span).to have_error }