Class: Datadog::Tracing::Contrib::ActiveRecord::Configuration::Resolver

Inherits:
Configuration::Resolver show all
Includes:
MakaraResolver
Defined in:
lib/datadog/tracing/contrib/active_record/configuration/resolver.rb

Overview

Converts Symbols, Strings, and Hashes to a normalized connection settings Hash.

When matching using a Hash, these are the valid fields: “‘

adapter: ...,
host: ...,
port: ...,
database: ...,
username: ...,
role: ...,

“‘

Partial matching is supported: not including certain fields or setting them to ‘nil` will cause them to matching all values for that field. For example: `database: nil` will match any database, given the remaining fields match.

Any fields not listed above are discarded.

When more than one configuration could be matched, the last one to match is selected, based on addition order (‘#add`).

Instance Attribute Summary

Attributes inherited from Configuration::Resolver

#configurations

Instance Method Summary collapse

Methods inherited from Configuration::Resolver

#get

Constructor Details

#initialize(active_record_configuration = nil) ⇒ Resolver

Returns a new instance of Resolver.



35
36
37
38
39
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 35

def initialize(active_record_configuration = nil)
  super()

  @active_record_configuration = active_record_configuration
end

Instance Method Details

#active_record_configurationObject



41
42
43
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 41

def active_record_configuration
  @active_record_configuration || ::ActiveRecord::Base.configurations
end

#add(matcher, value) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 45

def add(matcher, value)
  parsed = parse_matcher(matcher)

  # In case of error parsing, don't store `nil` key
  # as it wouldn't be useful for matching configuration
  # hashes in `#resolve`.
  super(parsed, value) if parsed
end

#resolve(db_config) ⇒ Object



54
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
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 54

def resolve(db_config)
  active_record_config = resolve_connection_key(db_config).symbolize_keys

  hash = normalize_for_resolve(active_record_config)

  # Hashes in Ruby maintain insertion order
  _, config = @configurations.reverse_each.find do |matcher, _|
    matcher.none? do |key, value|
      value != hash[key]
    end
  end

  config
rescue => e
  # Resolving a valid database configuration should not raise an exception,
  # but if it does, it can be due to adding a broken pattern match prior to this call.
  #
  # `db_config` input may contain sensitive information such as passwords,
  # hence provide a succinct summary for the error logging.
  Datadog.logger.error(
    'Failed to resolve ActiveRecord database configuration. '\
    "Cause: #{e.class.name} Source: #{Array(e.backtrace).first}"
  )

  nil
end