Module: ScoutApm::Instruments::ActiveRecordInstruments
- Defined in:
- lib/scout_apm/instruments/active_record.rb
Overview
Contains ActiveRecord instrument, aliasing ActiveRecord::ConnectionAdapters::AbstractAdapter#log calls to trace calls to the database.
#log instrument.
#log is very close to where AR calls out to the database itself. We have access to the real SQL, and an AR generated “name” for the Query
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(instrumented_class) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/scout_apm/instruments/active_record.rb', line 85 def self.included(instrumented_class) ScoutApm::Agent.instance.logger.info "Instrumenting #{instrumented_class.inspect}" instrumented_class.class_eval do unless instrumented_class.method_defined?(:log_without_scout_instruments) alias_method :log_without_scout_instruments, :log alias_method :log, :log_with_scout_instruments end end end |
Instance Method Details
#log_with_scout_instruments(*args, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/scout_apm/instruments/active_record.rb', line 95 def log_with_scout_instruments(*args, &block) # Extract data from the arguments sql, name = args metric_name = Utils::ActiveRecordMetricName.new(sql, name) desc = Utils::SqlSanitizer.new(sql) # Get current ScoutApm context req = ScoutApm::RequestManager.lookup current_layer = req.current_layer # If we call #log, we have a real query to run, and we've already # gotten through the cache gatekeeper. Since we want to only trace real # queries, and not repeated identical queries that just hit cache, we # mark layer as ignorable initially in #find_by_sql, then only when we # know it's a real database call do we mark it back as usable. # # This flag is later used in SlowRequestConverter to skip adding ignorable layers current_layer.annotate_layer(:ignorable => false) if current_layer # Either: update the current layer and yield, don't start a new one. if current_layer && current_layer.type == "ActiveRecord" # TODO: Get rid of call .to_s, need to find this without forcing a previous run of the name logic if current_layer.name.to_s == Utils::ActiveRecordMetricName::DEFAULT_METRIC current_layer.name = metric_name current_layer.desc = desc end log_without_scout_instruments(sql, name, &block) # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack. else layer = ScoutApm::Layer.new("ActiveRecord", metric_name) layer.desc = desc req.start_layer(layer) begin log_without_scout_instruments(sql, name, &block) ensure req.stop_layer end end end |