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

Inherits:
Configuration::Resolver show all
Includes:
MakaraResolver, Configuration::CachingResolver
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 included from Configuration::CachingResolver

#reset_cache

Methods inherited from Configuration::Resolver

#get

Constructor Details

#initialize(active_record_configuration = nil) ⇒ Resolver

Returns a new instance of Resolver.



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

def initialize(active_record_configuration = nil)
  super()

  @active_record_configuration = active_record_configuration
end

Instance Method Details

#active_record_configurationObject



44
45
46
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 44

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

#add(matcher, value) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 48

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/datadog/tracing/contrib/active_record/configuration/resolver.rb', line 57

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}"
  )
  Core::Telemetry::Logger.report(e, description: 'Failed to resolve ActiveRecord database configuration')

  nil
end