Module: Contrast::Config::Diagnostics::SingletonTools

Included in:
Tools
Defined in:
lib/contrast/config/diagnostics/singleton_tools.rb

Overview

Tools to help with the config diagnostics, called directly from the module.

Constant Summary collapse

API_CREDENTIALS =
%w[api_key service_key].cs__freeze
CONTRAST_MARK =
'CONTRAST_'

Instance Method Summary collapse

Instance Method Details

#flatten_settings(data, path = [], config: Contrast::CONFIG.config.loaded_config, cli: false) ⇒ Object

Flattens out the read settings from file, env or contrast ui. example: Contrast::Config::Diagnostics::SingletonTools."agent"agent.polling"agent.polling.server_settings_ms"=>"50000"

If cli is set we avoid adding the path and additional '.' to the key.

@param data [Hash, nil] @param path [String] where to look for settings. @param config [Hash] symbolized config to fetch keys from. @param cli [Boolean] does the config come from cli.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/contrast/config/diagnostics/singleton_tools.rb', line 57

def flatten_settings data, path = [], config: Contrast::CONFIG.config.loaded_config, cli: false
  return [] unless data

  data.each_with_object([]) do |(k, v), entries|
    if v.cs__is_a?(Hash)
      entries.concat(flatten_settings(v, path.dup.append(k.to_sym)))
    else
      if API_CREDENTIALS.include?(k.to_s)
        entries << { k.to_s => Contrast::Configuration::EFFECTIVE_REDACTED } if cli
        entries << { "#{ path.join('.') }.#{ k }" => Contrast::Configuration::EFFECTIVE_REDACTED } unless cli
        next
      end
      entries << { k.to_s => value_to_s(config.dig(*path, k)) } if cli
      entries << { "#{ path.join('.') }.#{ k }" => value_to_s(config.dig(*path, k)) } unless cli
    end
  end.flatten # rubocop:disable Style/MethodCalledOnDoEndBlock
end

#to_config_values(flats, source: false, cli: false) ⇒ Array<Contrast::Config::Diagnostics::SourceConfigValue>

Creates new config instances for each read config entry from the flat generated configs.

Parameters:

  • flats (Array) —

    of flatten configs produced by #flatten_settings

  • source (Boolean) (defaults to: false) —

    flag to set the desired value class, it may be a effective or source value.

  • cli (Boolean) (defaults to: false) —

    flag to check if the value comes from cli.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/contrast/config/diagnostics/singleton_tools.rb', line 18

def to_config_values flats, source: false, cli: false
  config_value_klass = if source
                         Contrast::Config::Diagnostics::SourceConfigValue
                       else
                         Contrast::Config::Diagnostics::EffectiveConfigValue
                       end
  settings = []
  flats.each do |entry|
    entry.each do |key, value|
      efc_value = config_value_klass.new.tap do |config_value|
        config_value.canonical_name = key
        if cli && key.to_s.include?(CONTRAST_MARK)
          config_value.canonical_name = key.gsub(Contrast::Utils::ObjectShare::DOUBLE_UNDERSCORE,
                                                 Contrast::Utils::ObjectShare::PERIOD).downcase
        end
        config_value.key = key
        config_value.value = if API_CREDENTIALS.include?(key.to_s)
                               Contrast::Configuration::EFFECTIVE_REDACTED
                             else
                               value_to_s(value)
                             end
      end
      next unless efc_value

      settings << efc_value
    end
  end
  settings
end

#update_config(parts, value, source_type) ⇒ Object

Update the stored config values to ensure that we know about the correct values, and that the sources are correct for entries updated from the UI.

Parameters:

  • parts (Array<Symbols>, String) —

    the path to the setting in config Accepts Array: [:agent :enable] or String: 'agent.enable'

  • value (String, Integer, Array, nil) —

    the value for the configuration setting

  • source_type (String) —

    the source of the configuration setting



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/contrast/config/diagnostics/singleton_tools.rb', line 82

def update_config parts, value, source_type
  parts_array, string = handle_parts_array(parts)
  path = string ? parts : parts_array.join('.')
  return unless parts_array

  # Check to see whether the source has been overridden by local settings,
  # Before updating from Contrast UI.
  if source_type == Contrast::Components::Config::Sources::CONTRAST_UI &&
        Contrast::CONFIG.sources.source_overridden?(path)

    return
  end

  level = Contrast::CONFIG.config.loaded_config
  parts_array[0...-1]&.each do |segment|
    level[segment] ||= {}
    level = level[segment]
  end
  return unless level.cs__is_a?(Hash)

  level[parts_array[-1]] = value
  Contrast::CONFIG.sources.set(path, source_type)
end

#value_to_s(value) ⇒ Object

Recursively converts each value to string.

Parameters:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/contrast/config/diagnostics/singleton_tools.rb', line 109

def value_to_s value
  case value
  when String
    if Contrast::Utils::DuckUtils.empty_duck?(value)
      Contrast::Config::Diagnostics::SourceConfigValue::NULL
    else
      value
    end
  when Array
    handle_array_to_s(value)
  when Hash
    handle_hash_to_s(value)
  when TrueClass, FalseClass, Symbol, Integer
    value.to_s
  else
    Contrast::Config::Diagnostics::SourceConfigValue::NULL
  end
end