Module: Datadog::Contrib::ActiveRecord::Utils
- Defined in:
- lib/ddtrace/contrib/active_record/utils.rb
Overview
Common utilities for Rails
Constant Summary collapse
- EMPTY_CONFIG =
{}.freeze
Class Method Summary collapse
- .adapter_host ⇒ Object
- .adapter_name ⇒ Object
- .adapter_port ⇒ Object
-
.connection_config(connection = nil, connection_id = nil) ⇒ Object
In newer Rails versions, the ‘payload` contains both the `connection` and its `object_id` named `connection_id`.
- .database_name ⇒ Object
- .default_connection_config ⇒ Object
Class Method Details
.adapter_host ⇒ Object
16 17 18 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 16 def self.adapter_host connection_config[:host] end |
.adapter_name ⇒ Object
8 9 10 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 8 def self.adapter_name Datadog::Utils::Database.normalize_vendor(connection_config[:adapter]) end |
.adapter_port ⇒ Object
20 21 22 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 20 def self.adapter_port connection_config[:port] end |
.connection_config(connection = nil, connection_id = nil) ⇒ Object
In newer Rails versions, the ‘payload` contains both the `connection` and its `object_id` named `connection_id`.
So, if rails is recent we’ll have a direct access to the connection. Else, we’ll find it thanks to the passed ‘connection_id`.
See this PR for more details: github.com/rails/rails/pull/34602
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 31 def self.connection_config(connection = nil, connection_id = nil) return default_connection_config if connection.nil? && connection_id.nil? conn = if !connection.nil? connection # Rails 3.0 - 3.2 elsif Gem.loaded_specs['activerecord'].version < Gem::Version.new('4.0') ::ActiveRecord::Base .connection_handler .connection_pools .values .flat_map(&:connections) .find { |c| c.object_id == connection_id } # Rails 4.2+ else ::ActiveRecord::Base .connection_handler .connection_pool_list .flat_map(&:connections) .find { |c| c.object_id == connection_id } end if conn.instance_variable_defined?(:@config) conn.instance_variable_get(:@config) else EMPTY_CONFIG end end |
.database_name ⇒ Object
12 13 14 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 12 def self.database_name connection_config[:database] end |
.default_connection_config ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ddtrace/contrib/active_record/utils.rb', line 60 def self.default_connection_config return @default_connection_config if instance_variable_defined?(:@default_connection_config) current_connection_name = if ::ActiveRecord::Base.respond_to?(:connection_specification_name) ::ActiveRecord::Base.connection_specification_name else ::ActiveRecord::Base end connection_pool = ::ActiveRecord::Base.connection_handler.retrieve_connection_pool(current_connection_name) connection_pool.nil? ? EMPTY_CONFIG : (@default_connection_config = connection_pool.spec.config) rescue StandardError EMPTY_CONFIG end |