Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code performances #656

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/ddtrace/contrib/active_record/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Contrib
module ActiveRecord
# Common utilities for Rails
module Utils
EMPTY_CONFIG = {}.freeze

def self.adapter_name
Datadog::Utils::Database.normalize_vendor(connection_config[:adapter])
end
Expand All @@ -23,14 +25,9 @@ def self.connection_config(connection = nil)
connection.nil? ? default_connection_config : connection_config_from_connection(connection)
end

# Attempt to retrieve the connection config from an object ID.
# Typical of ActiveSupport::Notifications `sql.active_record`
def self.connection_config_from_connection(connection)
if connection.instance_variable_defined?(:@config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to keep the defined? check here, otherwise this generates warnings.

connection.instance_variable_get(:@config)
else
{}
end
connection.instance_variable_get(:@config) || EMPTY_CONFIG
end

def self.default_connection_config
Expand All @@ -42,9 +39,9 @@ def self.default_connection_config
end

connection_pool = ::ActiveRecord::Base.connection_handler.retrieve_connection_pool(current_connection_name)
connection_pool.nil? ? {} : (@default_connection_config = connection_pool.spec.config)
connection_pool.nil? ? EMPTY_CONFIG : (@default_connection_config = connection_pool.spec.config)
rescue StandardError
{}
EMPTY_CONFIG
end
end
end
Expand Down