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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, context) ⇒ Option

Returns a new instance of Option.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/core/configuration/option.rb', line 42

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

#definitionObject (readonly)

Returns the value of attribute definition.



11
12
13
# File 'lib/datadog/core/configuration/option.rb', line 11

def definition
  @definition
end

Instance Method Details

#default_precedence?Boolean

Returns:

  • (Boolean)


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

def default_precedence?
  precedence_set == Precedence::DEFAULT
end

#default_valueObject



138
139
140
141
142
143
144
# File 'lib/datadog/core/configuration/option.rb', line 138

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



113
114
115
116
117
118
119
# File 'lib/datadog/core/configuration/option.rb', line 113

def get
  if @is_set
    @value
  else
    set_value_from_env_or_default
  end
end

#resetObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/datadog/core/configuration/option.rb', line 121

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/datadog/core/configuration/option.rb', line 62

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/datadog/core/configuration/option.rb', line 84

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