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

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
# File 'lib/datadog/core/configuration/option.rb', line 48

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

#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_setObject (readonly)

Returns the value of attribute precedence_set.



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

attr_reader :definition, :precedence_set

Instance Method Details

#default_precedence?Boolean

Returns:

  • (Boolean)


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

def default_precedence?
  precedence_set == Precedence::DEFAULT
end

#default_valueObject



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

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



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

def get
  if @is_set
    @value
  else
    set_value_from_env_or_default
  end
end

#resetObject



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

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.

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



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

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 '#{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)
end

#unset(precedence) ⇒ Object



90
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
# File 'lib/datadog/core/configuration/option.rb', line 90

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