Class: Datadog::Core::Configuration::Option

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(definition, context) ⇒ Option

Returns a new instance of Option.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datadog/core/configuration/option.rb', line 48

def initialize(definition, context)
  @definition = definition
  @context = context
  @value = nil
  @is_set = false
  @resolved_env = nil

  # 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

#definitionConfiguration::OptionDefinition (readonly)

The definition object that matches this option.



17
18
19
# File 'lib/datadog/core/configuration/option.rb', line 17

def definition
  @definition
end

#precedence_setPrecedence::Value (readonly)

When this option was last set, what was the value precedence used?

Returns:



17
# File 'lib/datadog/core/configuration/option.rb', line 17

attr_reader :definition, :precedence_set, :resolved_env

#resolved_envObject (readonly)

Returns the value of attribute resolved_env.



17
18
19
# File 'lib/datadog/core/configuration/option.rb', line 17

def resolved_env
  @resolved_env
end

Instance Method Details

#default_precedence?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/datadog/core/configuration/option.rb', line 153

def default_precedence?
  precedence_set == Precedence::DEFAULT
end

#default_valueObject



145
146
147
148
149
150
151
# File 'lib/datadog/core/configuration/option.rb', line 145

def default_value
  if definition.default.instance_of?(Proc)
    context_eval(&definition.default)
  else
    definition.default_proc || Core::Utils::SafeDup.frozen_or_dup(definition.default)
  end
end

#getObject



120
121
122
123
124
125
126
# File 'lib/datadog/core/configuration/option.rb', line 120

def get
  if @is_set
    @value
  else
    set_value_from_env_or_default
  end
end

#resetObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/datadog/core/configuration/option.rb', line 128

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, resolved_env: nil) ⇒ 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.

Parameters:

  • value (Object)

    the new value to be associated with this option

  • precedence (Precedence) (defaults to: Precedence::PROGRAMMATIC)

    from what precedence order this new value comes from



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/datadog/core/configuration/option.rb', line 69

def set(value, precedence: Precedence::PROGRAMMATIC, resolved_env: nil)
  # 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 '#{definition.name}' 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, resolved_env)
end

#unset(precedence) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/datadog/core/configuration/option.rb', line 91

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, nil)
        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