Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jul 12, 2023
1 parent 3cb1e89 commit ad94836
Show file tree
Hide file tree
Showing 22 changed files with 43 additions and 39 deletions.
8 changes: 6 additions & 2 deletions lib/datadog/core/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions lib/datadog/core/utils/variable_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,28 @@ 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(',')
else
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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/tracing/contrib/faraday/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/datadog/tracing/contrib/grape/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/contrib/http/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/datadog/tracing/contrib/rack/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions lib/datadog/tracing/contrib/rails/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions lib/datadog/tracing/contrib/sequel/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions lib/datadog/tracing/contrib/sequel/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/tracing/contrib/span_attribute_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==
Expand All @@ -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 ==
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/tracing/contrib/span_attribute_schema.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/datadog/core/utils/safe_dup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/datadog/tracing/contrib/faraday/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 }

Expand Down

0 comments on commit ad94836

Please sign in to comment.