Class: Datadog::Core::Configuration::ConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/configuration/config_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_env: ENV, supported_configurations: SUPPORTED_CONFIGURATIONS, aliases: ALIASES, alias_to_canonical: ALIAS_TO_CANONICAL, raise_on_unknown_env_var: false) ⇒ ConfigHelper

Returns a new instance of ConfigHelper.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/datadog/core/configuration/config_helper.rb', line 10

def initialize(
  source_env: ENV,
  supported_configurations: SUPPORTED_CONFIGURATIONS,
  aliases: ALIASES,
  alias_to_canonical: ALIAS_TO_CANONICAL,
  raise_on_unknown_env_var: false
)
  @source_env = source_env
  @supported_configurations = supported_configurations
  @aliases = aliases
  @alias_to_canonical = alias_to_canonical
  @raise_on_unknown_env_var = raise_on_unknown_env_var
end

Instance Method Details

#[](name) ⇒ Object



24
25
26
# File 'lib/datadog/core/configuration/config_helper.rb', line 24

def [](name)
  get_environment_variable(name)
end

#fetch(name, default_value = UNSET) ⇒ Object

Raises:

  • (KeyError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/datadog/core/configuration/config_helper.rb', line 28

def fetch(name, default_value = UNSET)
  if (item = get_environment_variable(name))
    return item
  end

  return yield(name) if block_given?
  return default_value unless default_value == UNSET

  raise KeyError, "key not found: #{name}"
end

#get_environment_variable(name, default_value = nil, source_env: @source_env) ⇒ String?

Returns the environment variable value if the environment variable is a supported Datadog configuration (starts with DD_ or OTEL_) or if it is not a Datadog configuration. Otherwise, it returns nil.

Parameters:

  • name (String)

    Environment variable name

  • default_value (String, nil) (defaults to: nil)

    Default value to return if the environment variable is not set

  • source_env (Hash[String, String]) (defaults to: @source_env)

    Environment variables to use

Returns:

  • (String, nil)

    The environment variable value

Raises:

  • (RuntimeError)

    if the configuration is not supported



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datadog/core/configuration/config_helper.rb', line 55

def get_environment_variable(name, default_value = nil, source_env: @source_env)
  # datadog-ci-rb is using dd-trace-rb config DSL, which uses this method.
  # Until we've correctly implemented support for datadog-ci-rb, we disable config inversion if ci is enabled.
  if !defined?(::Datadog::CI) &&
      (name.start_with?('DD_', 'OTEL_') || @alias_to_canonical[name]) &&
      !@supported_configurations[name]
    if defined?(@raise_on_unknown_env_var) && @raise_on_unknown_env_var # Only enabled for tests!
      if @alias_to_canonical[name]
        raise "Please use #{@alias_to_canonical[name]} instead of #{name}. See docs/AccessEnvironmentVariables.md for details."
      else
        raise "Missing #{name} env/configuration in \"supported-configurations.json\" file. See docs/AccessEnvironmentVariables.md for details."
      end
    end
    # TODO: Send telemetry to know if we ever miss an env var
    return nil
  end

  env_value = source_env[name]
  if env_value.nil? && @aliases[name]
    @aliases[name].each do |alias_name|
      return source_env[alias_name] if source_env[alias_name]
    end
  end

  env_value || default_value
end

#key?(name) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


39
40
41
# File 'lib/datadog/core/configuration/config_helper.rb', line 39

def key?(name)
  !get_environment_variable(name).nil?
end

#resolve_env(name, source_env: @source_env) ⇒ Object

Only used in error message creation. Match get_environment_variable logic to return the resolved environment variable name.



83
84
85
86
87
88
89
90
91
# File 'lib/datadog/core/configuration/config_helper.rb', line 83

def resolve_env(name, source_env: @source_env)
  if source_env[name].nil? && @aliases[name]
    @aliases[name].each do |alias_name|
      return alias_name if source_env[alias_name]
    end
  end

  name
end