Class: ScoutApm::Instruments::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/instruments/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



40
41
42
43
# File 'lib/scout_apm/instruments/active_record.rb', line 40

def initialize(context)
  @context = context
  @installed = false
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



38
39
40
# File 'lib/scout_apm/instruments/active_record.rb', line 38

def context
  @context
end

Instance Method Details

#add_instrumentsObject



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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/scout_apm/instruments/active_record.rb', line 73

def add_instruments
  # Setup Tracer on AR::Base
  if Utils::KlassHelper.defined?("ActiveRecord::Base")
    @installed = true

    ::ActiveRecord::Base.class_eval do
      include ::ScoutApm::Tracer
    end
  end

  # Install #log tracing
  if Utils::KlassHelper.defined?("ActiveRecord::ConnectionAdapters::AbstractAdapter")
    if Module.respond_to?(:prepend)
      ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecordInstruments)
      ::ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Tracer)
    else
      ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
        include ::ScoutApm::Instruments::ActiveRecordAliasMethodInstruments
        include ::ScoutApm::Tracer
      end
    end
  end

  if Utils::KlassHelper.defined?("ActiveRecord::Base")
    ::ActiveRecord::Base.class_eval do
      include ::ScoutApm::Instruments::ActiveRecordUpdateInstruments
    end
  end

  # Disabled until we can determine how to use Module#prepend in the
  # agent. Otherwise, this will cause infinite loops if NewRelic is
  # installed. We can't just use normal Module#include, since the
  # original methods don't call super the way Base#save does
  #
  #if Utils::KlassHelper.defined?("ActiveRecord::Relation")
  #  ::ActiveRecord::Relation.class_eval do
  #    include ::ScoutApm::Instruments::ActiveRecordRelationInstruments
  #  end
  #end

  if Utils::KlassHelper.defined?("ActiveRecord::Querying")
    ::ActiveRecord::Querying.module_eval do
      include ::ScoutApm::Tracer
      include ::ScoutApm::Instruments::ActiveRecordQueryingInstruments
    end
  end

  rails_3_2_or_above = defined?(::ActiveRecord::VERSION::MAJOR) &&
    defined?(::ActiveRecord::VERSION::MINOR) &&
    (::ActiveRecord::VERSION::MAJOR.to_i > 3 ||
     (::ActiveRecord::VERSION::MAJOR.to_i == 3 && ::ActiveRecord::VERSION::MINOR.to_i >= 2))
  if rails_3_2_or_above
    if Utils::KlassHelper.defined?("ActiveRecord::Relation")
      if @context.environment.supports_module_prepend?
        ::ActiveRecord::Relation.module_eval do
          prepend ::ScoutApm::Instruments::ActiveRecordRelationQueryInstruments
        end
      else
        ::ActiveRecord::Relation.module_eval do
          include ::ScoutApm::Instruments::ActiveRecordRelationQueryInstruments
        end
      end
    end
  else
    if Utils::KlassHelper.defined?("ActiveRecord::FinderMethods")
      ::ActiveRecord::FinderMethods.module_eval do
        include ::ScoutApm::Tracer
        include ::ScoutApm::Instruments::ActiveRecordFinderMethodsInstruments
      end
    end
  end

  if Utils::KlassHelper.defined?("ActiveSupport::Notifications")
    ActiveSupport::Notifications.subscribe("instantiation.active_record") do |event_name, start, stop, uuid, payload|
      req = ScoutApm::RequestManager.lookup
      layer = req.current_layer
      if layer && layer.type == "ActiveRecord"
        layer.annotate_layer({
          :class_name => payload[:class_name],
          :record_count => payload[:record_count]
        })
      elsif layer
        logger.debug("Expected layer type: ActiveRecord, got #{layer && layer.type}")
      else
        # noop, no layer at all. We're probably ignoring this req.
      end
    end
  end
rescue
  logger.warn "ActiveRecord instrumentation exception: #{$!.message}"
end

#installObject



53
54
55
56
57
58
59
60
61
# File 'lib/scout_apm/instruments/active_record.rb', line 53

def install
  if install_via_after_initialize?
    Rails.configuration.after_initialize do
      add_instruments
    end
  else
    add_instruments
  end
end

#install_via_after_initialize?Boolean

If we have the right version of rails, we should use the hooks provided to install these instruments

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/scout_apm/instruments/active_record.rb', line 65

def install_via_after_initialize?
  defined?(::Rails) &&
    defined?(::Rails::VERSION) &&
    defined?(::Rails::VERSION::MAJOR) &&
    ::Rails::VERSION::MAJOR.to_i == 3 &&
    ::Rails.respond_to?(:configuration)
end

#installed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/scout_apm/instruments/active_record.rb', line 49

def installed?
  @installed
end

#loggerObject



45
46
47
# File 'lib/scout_apm/instruments/active_record.rb', line 45

def logger
  context.logger
end