Skip to content

Commit

Permalink
add default_proc option to store block value as default value
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jun 27, 2023
1 parent 35ddd1c commit 32988eb
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/core/configuration/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def default_value
if definition.default.instance_of?(Proc)
context_eval(&definition.default)
else
definition.default
definition.default_proc || definition.default
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/datadog/core/configuration/option_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OptionDefinition

attr_reader \
:default,
:default_proc,
:delegate_to,
:depends_on,
:name,
Expand All @@ -21,6 +22,7 @@ class OptionDefinition

def initialize(name, meta = {}, &block)
@default = meta[:default]
@default_proc = meta[:default_proc]
@delegate_to = meta[:delegate_to]
@depends_on = meta[:depends_on] || []
@name = name.to_sym
Expand All @@ -43,6 +45,7 @@ class Builder

def initialize(name, options = {})
@default = nil
@default_proc = nil
@delegate_to = nil
@depends_on = []
@helpers = {}
Expand All @@ -67,6 +70,10 @@ def default(value = nil, &block)
@default = block || value
end

def default_proc(&block)
@default_proc = block
end

def delegate_to(&block)
@delegate_to = block
end
Expand Down Expand Up @@ -96,6 +103,7 @@ def apply_options!(options = {})
return if options.nil? || options.empty?

default(options[:default]) if options.key?(:default)
default_proc(&options[:default_proc]) if options.key?(:default_proc)
delegate_to(&options[:delegate_to]) if options.key?(:delegate_to)
depends_on(*options[:depends_on]) if options.key?(:depends_on)
on_set(&options[:on_set]) if options.key?(:on_set)
Expand All @@ -111,6 +119,7 @@ def to_definition
def meta
{
default: @default,
default_proc: @default_proc,
delegate_to: @delegate_to,
depends_on: @depends_on,
on_set: @on_set,
Expand Down
5 changes: 3 additions & 2 deletions lib/datadog/core/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ def initialize(*_)
# @default `->{ Time.now }`
# @return [Proc<Time>]
option :time_now_provider do |o|
o.default { -> { ::Time.now } }

o.default_proc do
::Time.now
end
o.on_set do |time_provider|
Core::Utils::Time.now_provider = time_provider
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Settings < Contrib::Configuration::Settings
end

option :service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Settings < Contrib::Configuration::Settings

option :service_name
option :client_service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Settings < Contrib::Configuration::Settings
end

option :distributed_tracing, default: true
option :error_handler, default: DEFAULT_ERROR_HANDLER
option :error_handler, default_proc: DEFAULT_ERROR_HANDLER
option :split_by_domain, default: false

option :service_name do |o|
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/contrib/grpc/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Settings < Contrib::Configuration::Settings
end
end

option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/contrib/que/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Settings < Contrib::Configuration::Settings
option :tag_data do |o|
o.default { env_to_bool(Ext::ENV_TAG_DATA_ENABLED, false) }
end
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Settings < Contrib::Configuration::Settings
end

option :service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Settings < Contrib::Configuration::Settings
end

option :service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :tag_body, default: false
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Settings < Contrib::Configuration::Settings

option :service_name
option :client_service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :quantize, default: {}
option :distributed_tracing, default: false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Settings < Contrib::Configuration::Settings
end

option :service_name
option :error_handler, default: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :error_handler, default_proc: Tracing::SpanOperation::Events::DEFAULT_ON_ERROR
option :tag_body, default: false
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/datadog/core/configuration/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
instance_double(
Datadog::Core::Configuration::OptionDefinition,
default: default,
default_proc: default_proc,
delegate_to: delegate,
on_set: nil,
resetter: nil,
setter: setter
)
end
let(:default) { double('default') }
let(:default_proc) { nil }
let(:delegate) { nil }
let(:setter) { proc { setter_value } }
let(:setter_value) { double('setter_value') }
Expand Down

0 comments on commit 32988eb

Please sign in to comment.