# encoding: utf-8 require 'one_apm/agent/database/active_record_helper' module OneApm module Agent module Instrumentation module ActiveRecord EXPLAINER = lambda do |config, query| connection = OneApm::Agent::Database.get_connection(config) do ::ActiveRecord::Base.send("#{config[:adapter]}_connection", config) end if connection && connection.respond_to?(:execute) connection.execute("EXPLAIN #{query}") end end def self.insert_instrumentation ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do include ::OneApm::Agent::Instrumentation::ActiveRecord end end def self.included(instrumented_class) instrumented_class.class_eval do unless instrumented_class.method_defined?(:log_without_oneapm_instrumentation) alias_method :log_without_oneapm_instrumentation, :log alias_method :log, :log_with_oneapm_instrumentation protected :log end end end def log_with_oneapm_instrumentation(*args, &block) state = OneApm::TransactionState.tl_get if !state.is_execution_traced? return log_without_oneapm_instrumentation(*args, &block) end sql, name, _ = args metric = ActiveRecordHelper.metric_for_name(OneApm::Helper.correctly_encoded(name)) || ActiveRecordHelper.metric_for_sql(OneApm::Helper.correctly_encoded(sql)) if !metric log_without_oneapm_instrumentation(*args, &block) else metrics = [metric, remote_service_metric].compact metrics += ActiveRecordHelper.rollup_metrics_for(metric) self.class.trace_execution_scoped(metrics) do t0 = Time.now begin log_without_oneapm_instrumentation(*args, &block) ensure elapsed_time = (Time.now - t0).to_f OneApm::Manager.notice_sql(sql, metric, @config, elapsed_time, state, &EXPLAINER) end end end end def remote_service_metric if @config && @config[:adapter] ActiveRecordHelper.remote_service_metric(@config[:adapter], @config[:host]) end end end end end end LibraryDetection.defer do @name = :active_record depends_on do defined?(::ActiveRecord) && defined?(::ActiveRecord::Base) && (!defined?(::ActiveRecord::VERSION) || ::ActiveRecord::VERSION::MAJOR.to_i <= 3) end depends_on do !OneApm::Manager.config[:disable_activerecord] end executes do OneApm::Manager.logger.info 'Installing ActiveRecord instrumentation' end executes do if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i == 3 ActiveSupport.on_load(:active_record) do ::OneApm::Agent::Instrumentation::ActiveRecord.insert_instrumentation end else ::OneApm::Agent::Instrumentation::ActiveRecord.insert_instrumentation end end end