Module: XRay::Rails::ActiveRecord

Defined in:
lib/aws-xray-sdk/facets/rails/active_record.rb

Overview

Recording Rails database transactions as subsegments.

Constant Summary collapse

IGNORE_OPS =
['SCHEMA', 'ActiveRecord::SchemaMigration Load',
'ActiveRecord::InternalMetadata Load'].freeze
DB_TYPE_MAPPING =
{
  mysql2: 'MySQL',
  postgresql: 'PostgreSQL'
}.freeze

Class Method Summary collapse

Class Method Details

.record(transaction) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aws-xray-sdk/facets/rails/active_record.rb', line 15

def record(transaction)
  payload = transaction.payload
  pool, conn = get_pool_n_conn(payload[:connection_id])

  return if IGNORE_OPS.include?(payload[:name]) || pool.nil? || conn.nil?
  # The spec notation is Rails < 6.1, later this can be found in the db_config
  db_config = if pool.respond_to?(:spec)
                pool.spec.config
              else
                pool.db_config.configuration_hash
              end
  name, sql = build_name_sql_meta config: db_config, conn: conn
  subsegment = XRay.recorder.begin_subsegment name, namespace: 'remote'
  # subsegment is nil in case of context missing
  return if subsegment.nil?
  # Rails 7.1 introduced time measurement in milliseconds instead seconds of causing xray-sdk to report wrong duration for transaction calls.
  # This is being handled in rails 7.2 and later. https://github.com/rails/rails/pull/50779 
  subsegment.start_time = (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? transaction.time.to_f/1000 : transaction.time.to_f
  subsegment.sql = sql
  XRay.recorder.end_subsegment end_time: (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? transaction.end.to_f/1000 : transaction.end.to_f
end