module XRay::Rails::ActiveRecord
Recording Rails
database transactions as subsegments.
Constants
- DB_TYPE_MAPPING
- IGNORE_OPS
Public Class Methods
record(transaction)
click to toggle source
# 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? subsegment.start_time = transaction.time.to_f subsegment.sql = sql XRay.recorder.end_subsegment end_time: transaction.end.to_f end
Private Class Methods
build_name_sql_meta(config:, conn:)
click to toggle source
# File lib/aws-xray-sdk/facets/rails/active_record.rb, line 37 def build_name_sql_meta(config:, conn:) # extract all available info adapter = config[:adapter] database = config[:database] host = config[:host].nil? ? nil : %(@#{config[:host]}) port = config[:port].nil? ? nil : %(:#{config[:port]}) username = config[:username] # assemble subsegment name name = %(#{database}#{host}) # assemble sql meta sql = {} sql[:user] = username sql[:url] = %(#{adapter}://#{username}#{host}#{port}/#{database}) sql[:database_type] = DB_TYPE_MAPPING[adapter.to_sym] [name, sql] end
get_pool_n_conn(conn_id)
click to toggle source
# File lib/aws-xray-sdk/facets/rails/active_record.rb, line 55 def get_pool_n_conn(conn_id) pool, conn = nil, nil ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |p| conn = p.connections.select { |c| c.object_id == conn_id } pool = p unless conn.nil? return [pool, conn] if !conn.nil? && !conn.empty? && !pool.nil? end [pool, conn] end