Module: NewRelic::Agent::Instrumentation::ActiveRecordHelper
- Defined in:
- lib/new_relic/agent/instrumentation/active_record_helper.rb
Defined Under Namespace
Modules: InstanceIdentification
Constant Summary collapse
- ACTIVE_RECORD =
'ActiveRecord'.freeze
- OTHER =
'other'.freeze
- MAKARA_SUFFIX =
'_makara'.freeze
- TABLE_NAME_CACHE =
{}
- SPACE =
' '.freeze
- OPERATION_NAMES =
These are used primarily to optimize and avoid allocation on well known operations coming in. Anything not matching the list is fine, it just needs to get downcased directly for use.
{ 'Find' => 'find', 'Load' => 'find', 'Count' => 'find', 'Exists' => 'find', 'Create' => 'create', 'Columns' => 'columns', 'Indexes' => 'indexes', 'Destroy' => 'destroy', 'Update' => 'update', 'Save' => 'save' }.freeze
- PRODUCT_NAMES =
{ 'mysql' => 'MySQL', 'mysql2' => 'MySQL', 'postgresql' => 'Postgres', 'sqlite3' => 'SQLite', # https://rubygems.org/gems/trilogy 'trilogy' => 'MySQL', # https://rubygems.org/gems/activerecord-jdbcpostgresql-adapter 'jdbcmysql' => 'MySQL', # https://rubygems.org/gems/activerecord-jdbcpostgresql-adapter 'jdbcpostgresql' => 'Postgres', # https://rubygems.org/gems/activerecord-postgis-adapter 'postgis' => 'Postgres', # https://rubygems.org/gems/activerecord-jdbcsqlite3-adapter 'jdbcsqlite3' => 'SQLite', # https://rubygems.org/gems/activerecord-jdbcderby-adapter 'derby' => 'Derby', 'jdbcderby' => 'Derby', # https://rubygems.org/gems/activerecord-jdbc-adapter 'jdbc' => 'JDBC', # https://rubygems.org/gems/activerecord-jdbcmssql-adapter 'jdbcmssql' => 'MSSQL', 'mssql' => 'MSSQL', # https://rubygems.org/gems/activerecord-sqlserver-adapter 'sqlserver' => 'MSSQL', # https://rubygems.org/gems/activerecord-odbc-adapter 'odbc' => 'ODBC', # https://rubygems.org/gems/activerecord-oracle_enhanced-adapter 'oracle_enhanced' => 'Oracle', # https://rubygems.org/gems/activerecord-amazon-timestream-adapter 'amazon_timestream' => 'Timestream' }.freeze
- ACTIVE_RECORD_DEFAULT_PRODUCT_NAME =
'ActiveRecord'.freeze
Class Method Summary collapse
-
.bare_adapter_name(adapter_name) ⇒ Object
convert vendor (makara, etc.) wrapper names to their bare names ex: postgresql_makara -> postgresql.
-
.instrument_additional_methods ⇒ Object
Used by both the AR 3.x and 4.x instrumentation.
- .instrument_relation_methods ⇒ Object
- .instrument_save_methods ⇒ Object
- .map_operation(raw_operation) ⇒ Object
- .map_product(adapter_name) ⇒ Object
- .model_from_splits(splits) ⇒ Object
- .operation_from_splits(splits, sql) ⇒ Object
- .product_operation_collection_for(name, sql, adapter_name) ⇒ Object
- .resolve_table_name(model_name) ⇒ Object
- .split_name(name) ⇒ Object
- .table_name_for(model_name) ⇒ Object
Class Method Details
.bare_adapter_name(adapter_name) ⇒ Object
convert vendor (makara, etc.) wrapper names to their bare names ex: postgresql_makara -> postgresql
113 114 115 116 117 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 113 def (adapter_name) return adapter_name.delete_suffix(MAKARA_SUFFIX) if adapter_name&.end_with?(MAKARA_SUFFIX) adapter_name end |
.instrument_additional_methods ⇒ Object
Used by both the AR 3.x and 4.x instrumentation
15 16 17 18 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 15 def instrument_additional_methods instrument_save_methods instrument_relation_methods end |
.instrument_relation_methods ⇒ Object
45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 45 def instrument_relation_methods ::ActiveRecord::Relation.class_eval do def newrelic_relation_collection_name ::NewRelic::Agent.config[:active_record_use_table_name] ? self.klass.table_name : self.name end private(:newrelic_relation_collection_name) alias_method(:update_all_without_newrelic, :update_all) def update_all(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do update_all_without_newrelic(*args, &blk) end end alias_method(:delete_all_without_newrelic, :delete_all) if NewRelic::Helper.version_satisfied?(RUBY_VERSION, '<', '2.7.0') def delete_all(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do delete_all_without_newrelic(*args, &blk) end end else def delete_all(*args, **kwargs, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do delete_all_without_newrelic(*args, **kwargs, &blk) end end end alias_method(:destroy_all_without_newrelic, :destroy_all) def destroy_all(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do destroy_all_without_newrelic(*args, &blk) end end alias_method(:calculate_without_newrelic, :calculate) def calculate(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do calculate_without_newrelic(*args, &blk) end end if method_defined?(:pluck) alias_method(:pluck_without_newrelic, :pluck) def pluck(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_relation_collection_name, nil, ACTIVE_RECORD) do pluck_without_newrelic(*args, &blk) end end end end end |
.instrument_save_methods ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 20 def instrument_save_methods ::ActiveRecord::Base.class_eval do def newrelic_model_collection_name ::NewRelic::Agent.config[:active_record_use_table_name] ? self.class.table_name : self.class.name end private(:newrelic_model_collection_name) alias_method(:save_without_newrelic, :save) def save(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_model_collection_name, nil, ACTIVE_RECORD) do save_without_newrelic(*args, &blk) end end alias_method(:save_without_newrelic!, :save!) def save!(*args, &blk) ::NewRelic::Agent.with_database_metric_name(newrelic_model_collection_name, nil, ACTIVE_RECORD) do save_without_newrelic!(*args, &blk) end end end end |
.map_operation(raw_operation) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 189 def map_operation(raw_operation) direct_op = OPERATION_NAMES[raw_operation] return direct_op if direct_op raw_operation.downcase end |
.map_product(adapter_name) ⇒ Object
245 246 247 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 245 def map_product(adapter_name) PRODUCT_NAMES.fetch(adapter_name, ACTIVE_RECORD_DEFAULT_PRODUCT_NAME) end |
.model_from_splits(splits) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 137 def model_from_splits(splits) return unless splits.length == 2 model_name = splits.first return model_name unless NewRelic::Agent.config[:active_record_use_table_name] table_name_for(model_name) || model_name end |
.operation_from_splits(splits, sql) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 165 def operation_from_splits(splits, sql) if splits.length == 2 map_operation(splits[1]) else NewRelic::Agent::Database.parse_operation_from_query(sql) || OTHER end end |
.product_operation_collection_for(name, sql, adapter_name) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 119 def product_operation_collection_for(name, sql, adapter_name) product = map_product((adapter_name)) splits = split_name(name) model = model_from_splits(splits) operation = operation_from_splits(splits, sql) NewRelic::Agent::Datastores::MetricHelper.product_operation_collection_for(product, operation, model, ACTIVE_RECORD) end |
.resolve_table_name(model_name) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 155 def resolve_table_name(model_name) klass = Object.const_get(model_name) return nil unless klass.is_a?(Class) klass.table_name rescue => e NewRelic::Agent.logger.debug("Failed to get table name for model #{model_name}: #{e}") nil end |
.split_name(name) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 129 def split_name(name) if name&.respond_to?(:split) name.split(SPACE) else NewRelic::EMPTY_ARRAY end end |
.table_name_for(model_name) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 146 def table_name_for(model_name) return TABLE_NAME_CACHE[model_name] if TABLE_NAME_CACHE.key?(model_name) @table_name_cache_lock.synchronize do TABLE_NAME_CACHE[model_name] = resolve_table_name(model_name) unless TABLE_NAME_CACHE.key?(model_name) TABLE_NAME_CACHE[model_name] end end |