Class: Datadog::Core::Configuration::Option
- Inherits:
-
Object
- Object
- Datadog::Core::Configuration::Option
- Defined in:
- lib/datadog/core/configuration/option.rb
Overview
Represents an instance of an integration configuration option
Defined Under Namespace
Modules: Precedence Classes: InvalidDefinitionError
Instance Attribute Summary collapse
-
#definition ⇒ Configuration::OptionDefinition
readonly
The definition object that matches this option.
-
#precedence_set ⇒ Object
readonly
Returns the value of attribute precedence_set.
Instance Method Summary collapse
- #default_precedence? ⇒ Boolean
- #default_value ⇒ Object
- #get ⇒ Object
-
#initialize(definition, context) ⇒ Option
constructor
A new instance of Option.
-
#name_with_settings_path ⇒ String
Computes the name of the option with the settings path.
- #reset ⇒ Object
-
#set(value, precedence: Precedence::PROGRAMMATIC) ⇒ Object
Overrides the current value for this option if the
precedenceis equal or higher than the previously set value. - #settings? ⇒ Boolean
- #unset(precedence) ⇒ Object
- #values_per_precedence ⇒ Object
Constructor Details
#initialize(definition, context) ⇒ Option
Returns a new instance of Option.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/datadog/core/configuration/option.rb', line 66 def initialize(definition, context) @definition = definition @context = context @value = nil @is_set = false # One value is stored per precedence, to allow unsetting a higher # precedence value and falling back to a lower precedence one. @value_per_precedence = Hash.new(UNSET) # Lowest precedence, to allow for `#set` to always succeed for a brand new `Option` instance. @precedence_set = Precedence::DEFAULT end |
Instance Attribute Details
#definition ⇒ Configuration::OptionDefinition (readonly)
The definition object that matches this option.
18 19 20 |
# File 'lib/datadog/core/configuration/option.rb', line 18 def definition @definition end |
#precedence_set ⇒ Object (readonly)
Returns the value of attribute precedence_set.
18 |
# File 'lib/datadog/core/configuration/option.rb', line 18 attr_reader :definition, :precedence_set |
Instance Method Details
#default_precedence? ⇒ Boolean
187 188 189 |
# File 'lib/datadog/core/configuration/option.rb', line 187 def default_precedence? precedence_set == Precedence::DEFAULT end |
#default_value ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/datadog/core/configuration/option.rb', line 178 def default_value if definition.default.instance_of?(Proc) # Steep: https://github.com/soutaro/steep/issues/335 context_eval(&definition.default) # steep:ignore BlockTypeMismatch else definition.default_proc || Core::Utils::SafeDup.frozen_or_dup(definition.default) end end |
#get ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/datadog/core/configuration/option.rb', line 147 def get unless @is_set # Ensures that both the default value and the environment value are set. # This approach handles scenarios where an environment value is unset # by falling back to the default value consistently. set_default_value set_customer_stable_config_value set_env_value set_fleet_stable_config_value end @value end |
#name_with_settings_path ⇒ String
Computes the name of the option with the settings path. E.g. "tracing.rails.middleware_names" for the "middleware_names" option in the "tracing.rails" settings.
83 84 85 86 87 88 |
# File 'lib/datadog/core/configuration/option.rb', line 83 def name_with_settings_path @name_with_settings_path ||= begin settings_path = @context.class.settings_path settings_path.nil? ? definition.name.to_s : "#{settings_path}.#{definition.name}" end end |
#reset ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/datadog/core/configuration/option.rb', line 161 def reset @value = if definition.resetter # Don't change @is_set to false; custom resetters are # responsible for changing @value back to a good state. # Setting @is_set = false would cause a default to be applied. context_exec(@value, &definition.resetter) else @is_set = false nil end # Reset back to the lowest precedence, to allow all `set`s to succeed right after a reset. @precedence_set = Precedence::DEFAULT # Reset all stored values @value_per_precedence = Hash.new(UNSET) end |
#set(value, precedence: Precedence::PROGRAMMATIC) ⇒ Object
Overrides the current value for this option if the precedence is equal or higher than
the previously set value.
The first call to #set will always store the value regardless of precedence.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/datadog/core/configuration/option.rb', line 96 def set(value, precedence: Precedence::PROGRAMMATIC) # Is there a higher precedence value set? if @precedence_set > precedence # This should be uncommon, as higher precedence values tend to # happen later in the application lifecycle. Datadog.logger.info do "Option '#{name_with_settings_path}' not changed to '#{value}' (precedence: #{precedence.name}) because the higher " \ "precedence value '#{@value}' (precedence: #{@precedence_set.name}) was already set." end # But if it happens, we have to store the lower precedence value `value` # because it's possible to revert to it by `#unset`ting # the existing, higher-precedence value. # Effectively, we always store one value pre precedence. @value_per_precedence[precedence] = value return @value end internal_set(value, precedence) end |
#settings? ⇒ Boolean
191 192 193 |
# File 'lib/datadog/core/configuration/option.rb', line 191 def settings? @definition.is_settings end |
#unset(precedence) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/datadog/core/configuration/option.rb', line 118 def unset(precedence) @value_per_precedence[precedence] = UNSET # If we are unsetting the currently active value, we have to restore # a lower precedence one... if precedence == @precedence_set # Find a lower precedence value that is already set. Precedence::LIST.each do |p| # DEV: This search can be optimized, but the list is small, and unset is # DEV: only called from direct user interaction in the Datadog UI. next unless p < precedence # Look for value that is set. # The hash `@value_per_precedence` has a custom default value of `UNSET`. if (value = @value_per_precedence[p]) != UNSET internal_set(value, p) return nil end end # If no value is left to fall back on, reset this option reset end # ... otherwise, we are either unsetting a higher precedence value that is not # yet set, thus there's nothing to do; or we are unsetting a lower precedence # value, which also does not change the current value. end |
#values_per_precedence ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/datadog/core/configuration/option.rb', line 195 def values_per_precedence # value_per_precedence is only filled after we call `get` once. get unless @is_set @value_per_precedence.each_with_object({}) do |(precedence, value), result| next if value.equal?(UNSET) result[precedence] = value end end |