Module: Datadog::Contrib::Rails::Utils
- Defined in:
- lib/ddtrace/contrib/rails/utils.rb
Overview
common utilities for Rails
Class Method Summary collapse
- .adapter_host ⇒ Object
- .adapter_name ⇒ Object
- .adapter_port ⇒ Object
- .app_name ⇒ Object
-
.connection_by_id(object_id) ⇒ Object
Attempt to retrieve the connection from an object ID.
- .connection_config(object_id = nil) ⇒ Object
-
.connection_config_by_id(object_id) ⇒ Object
Attempt to retrieve the connection config from an object ID.
- .database_name ⇒ Object
- .default_connection_config ⇒ Object
- .exception_is_error?(exception) ⇒ Boolean
-
.normalize_template_name(name) ⇒ Object
in Rails the template name includes the template full path and it’s better to avoid storing such information.
-
.normalize_vendor(vendor) ⇒ Object
TODO: Consider moving this out of Rails.
Class Method Details
.adapter_host ⇒ Object
57 58 59 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 57 def self.adapter_host connection_config[:adapter_host] end |
.adapter_name ⇒ Object
49 50 51 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 49 def self.adapter_name connection_config[:adapter_name] end |
.adapter_port ⇒ Object
61 62 63 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 61 def self.adapter_port connection_config[:adapter_port] end |
.app_name ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 41 def self.app_name if ::Rails::VERSION::MAJOR >= 4 ::Rails.application.class.parent_name.underscore else ::Rails.application.class.to_s.underscore end end |
.connection_by_id(object_id) ⇒ Object
Attempt to retrieve the connection from an object ID.
76 77 78 79 80 81 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 76 def self.connection_by_id(object_id) return nil if object_id.nil? ObjectSpace._id2ref(object_id) rescue StandardError nil end |
.connection_config(object_id = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 65 def self.connection_config(object_id = nil) config = object_id.nil? ? default_connection_config : connection_config_by_id(object_id) { adapter_name: normalize_vendor(config[:adapter]), adapter_host: config[:host], adapter_port: config[:port], database_name: config[:database] } end |
.connection_config_by_id(object_id) ⇒ Object
Attempt to retrieve the connection config from an object ID. Typical of ActiveSupport::Notifications sql.active_record
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 85 def self.connection_config_by_id(object_id) connection = connection_by_id(object_id) return {} if connection.nil? if connection.instance_variable_defined?(:@config) connection.instance_variable_get(:@config) else {} end end |
.database_name ⇒ Object
53 54 55 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 53 def self.database_name connection_config[:database_name] end |
.default_connection_config ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 96 def self.default_connection_config return @default_connection_config unless @default_connection_config.nil? 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? ? {} : (@default_connection_config = connection_pool.spec.config) rescue StandardError {} end |
.exception_is_error?(exception) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 110 def self.exception_is_error?(exception) if defined?(::ActionDispatch::ExceptionWrapper) # Gets the equivalent status code for the exception (not all are 5XX) # You can add custom errors via `config.action_dispatch.rescue_responses` status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) # Only 5XX exceptions are actually errors (e.g. don't flag 404s) status.to_s.starts_with?('5') else true end end |
.normalize_template_name(name) ⇒ Object
in Rails the template name includes the template full path and it’s better to avoid storing such information. This method returns the relative path from views/ or the template name if a views/ folder is not in the template full path. A wrong usage ensures that this method will not crash the tracing system.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 11 def self.normalize_template_name(name) return if name.nil? base_path = Datadog.configuration[:rails][:template_base_path] sections_view = name.split(base_path) if sections_view.length == 1 name.split('/')[-1] else sections_view[-1] end rescue return name.to_s end |
.normalize_vendor(vendor) ⇒ Object
TODO: Consider moving this out of Rails. Return a canonical name for a type of database
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ddtrace/contrib/rails/utils.rb', line 28 def self.normalize_vendor(vendor) case vendor when nil 'defaultdb' when 'sqlite3' 'sqlite' when 'postgresql' 'postgres' else vendor end end |