scout_apm 3.0.0.pre23 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.rubocop.yml +3 -4
  4. data/.travis.yml +17 -14
  5. data/CHANGELOG.markdown +150 -4
  6. data/Gemfile +2 -8
  7. data/README.markdown +30 -4
  8. data/Rakefile +1 -1
  9. data/ext/allocations/allocations.c +2 -0
  10. data/gems/README.md +28 -0
  11. data/gems/octoshark.gemfile +4 -0
  12. data/gems/rails3.gemfile +5 -0
  13. data/gems/rails4.gemfile +4 -0
  14. data/gems/rails5.gemfile +4 -0
  15. data/gems/rails6.gemfile +4 -0
  16. data/lib/scout_apm.rb +39 -9
  17. data/lib/scout_apm/agent.rb +29 -10
  18. data/lib/scout_apm/agent/exit_handler.rb +0 -1
  19. data/lib/scout_apm/agent_context.rb +26 -3
  20. data/lib/scout_apm/app_server_load.rb +7 -2
  21. data/lib/scout_apm/attribute_arranger.rb +0 -2
  22. data/lib/scout_apm/auto_instrument.rb +5 -0
  23. data/lib/scout_apm/auto_instrument/instruction_sequence.rb +31 -0
  24. data/lib/scout_apm/auto_instrument/layer.rb +23 -0
  25. data/lib/scout_apm/auto_instrument/parser.rb +27 -0
  26. data/lib/scout_apm/auto_instrument/rails.rb +175 -0
  27. data/lib/scout_apm/background_job_integrations/delayed_job.rb +1 -1
  28. data/lib/scout_apm/background_job_integrations/legacy_sneakers.rb +55 -0
  29. data/lib/scout_apm/background_job_integrations/que.rb +134 -0
  30. data/lib/scout_apm/background_job_integrations/resque.rb +6 -2
  31. data/lib/scout_apm/background_job_integrations/shoryuken.rb +124 -0
  32. data/lib/scout_apm/background_job_integrations/sidekiq.rb +5 -19
  33. data/lib/scout_apm/background_job_integrations/sneakers.rb +87 -0
  34. data/lib/scout_apm/config.rb +48 -7
  35. data/lib/scout_apm/detailed_trace.rb +217 -0
  36. data/lib/scout_apm/environment.rb +3 -0
  37. data/lib/scout_apm/error.rb +27 -0
  38. data/lib/scout_apm/error_service.rb +32 -0
  39. data/lib/scout_apm/error_service/error_buffer.rb +39 -0
  40. data/lib/scout_apm/error_service/error_record.rb +211 -0
  41. data/lib/scout_apm/error_service/ignored_exceptions.rb +66 -0
  42. data/lib/scout_apm/error_service/middleware.rb +32 -0
  43. data/lib/scout_apm/error_service/notifier.rb +33 -0
  44. data/lib/scout_apm/error_service/payload.rb +47 -0
  45. data/lib/scout_apm/error_service/periodic_work.rb +17 -0
  46. data/lib/scout_apm/error_service/railtie.rb +11 -0
  47. data/lib/scout_apm/error_service/sidekiq.rb +80 -0
  48. data/lib/scout_apm/extensions/transaction_callback_payload.rb +1 -1
  49. data/lib/scout_apm/fake_store.rb +3 -0
  50. data/lib/scout_apm/framework_integrations/rails_2.rb +2 -1
  51. data/lib/scout_apm/framework_integrations/rails_3_or_4.rb +17 -6
  52. data/lib/scout_apm/git_revision.rb +6 -3
  53. data/lib/scout_apm/instant/middleware.rb +2 -1
  54. data/lib/scout_apm/instrument_manager.rb +8 -7
  55. data/lib/scout_apm/instruments/action_controller_rails_2.rb +3 -1
  56. data/lib/scout_apm/instruments/action_controller_rails_3_rails4.rb +56 -55
  57. data/lib/scout_apm/instruments/action_view.rb +114 -26
  58. data/lib/scout_apm/instruments/active_record.rb +66 -19
  59. data/lib/scout_apm/instruments/http.rb +48 -0
  60. data/lib/scout_apm/instruments/memcached.rb +43 -0
  61. data/lib/scout_apm/instruments/mongoid.rb +9 -4
  62. data/lib/scout_apm/instruments/net_http.rb +8 -1
  63. data/lib/scout_apm/job_record.rb +4 -2
  64. data/lib/scout_apm/layaway_file.rb +4 -0
  65. data/lib/scout_apm/layer.rb +5 -56
  66. data/lib/scout_apm/layer_children_set.rb +15 -6
  67. data/lib/scout_apm/layer_converters/converter_base.rb +15 -30
  68. data/lib/scout_apm/layer_converters/database_converter.rb +2 -15
  69. data/lib/scout_apm/layer_converters/slow_job_converter.rb +12 -2
  70. data/lib/scout_apm/layer_converters/slow_request_converter.rb +14 -4
  71. data/lib/scout_apm/layer_converters/trace_converter.rb +184 -0
  72. data/lib/scout_apm/limited_layer.rb +0 -7
  73. data/lib/scout_apm/metric_stats.rb +0 -8
  74. data/lib/scout_apm/middleware.rb +1 -1
  75. data/lib/scout_apm/periodic_work.rb +19 -0
  76. data/lib/scout_apm/remote/message.rb +4 -0
  77. data/lib/scout_apm/reporter.rb +8 -3
  78. data/lib/scout_apm/reporting.rb +2 -1
  79. data/lib/scout_apm/request_histograms.rb +8 -0
  80. data/lib/scout_apm/serializers/app_server_load_serializer.rb +4 -0
  81. data/lib/scout_apm/serializers/directive_serializer.rb +4 -0
  82. data/lib/scout_apm/serializers/payload_serializer.rb +2 -2
  83. data/lib/scout_apm/serializers/payload_serializer_to_json.rb +30 -15
  84. data/lib/scout_apm/slow_job_record.rb +5 -1
  85. data/lib/scout_apm/slow_policy/age_policy.rb +33 -0
  86. data/lib/scout_apm/slow_policy/percent_policy.rb +22 -0
  87. data/lib/scout_apm/slow_policy/percentile_policy.rb +24 -0
  88. data/lib/scout_apm/slow_policy/policy.rb +21 -0
  89. data/lib/scout_apm/slow_policy/speed_policy.rb +16 -0
  90. data/lib/scout_apm/slow_request_policy.rb +18 -60
  91. data/lib/scout_apm/slow_transaction.rb +3 -1
  92. data/lib/scout_apm/store.rb +14 -10
  93. data/lib/scout_apm/tracked_request.rb +41 -30
  94. data/lib/scout_apm/transaction_time_consumed.rb +51 -0
  95. data/lib/scout_apm/utils/active_record_metric_name.rb +16 -3
  96. data/lib/scout_apm/utils/backtrace_parser.rb +3 -0
  97. data/lib/scout_apm/utils/marshal_logging.rb +90 -0
  98. data/lib/scout_apm/utils/sql_sanitizer.rb +10 -1
  99. data/lib/scout_apm/utils/sql_sanitizer_regex.rb +7 -0
  100. data/lib/scout_apm/utils/sql_sanitizer_regex_1_8_7.rb +6 -0
  101. data/lib/scout_apm/utils/unique_id.rb +27 -0
  102. data/lib/scout_apm/version.rb +1 -1
  103. data/scout_apm.gemspec +13 -7
  104. data/test/test_helper.rb +2 -2
  105. data/test/unit/agent_context_test.rb +29 -0
  106. data/test/unit/auto_instrument/assignments-instrumented.rb +31 -0
  107. data/test/unit/auto_instrument/assignments.rb +31 -0
  108. data/test/unit/auto_instrument/controller-ast.txt +57 -0
  109. data/test/unit/auto_instrument/controller-instrumented.rb +49 -0
  110. data/test/unit/auto_instrument/controller.rb +49 -0
  111. data/test/unit/auto_instrument/rescue_from-instrumented.rb +13 -0
  112. data/test/unit/auto_instrument/rescue_from.rb +13 -0
  113. data/test/unit/auto_instrument_test.rb +54 -0
  114. data/test/unit/error_service/error_buffer_test.rb +25 -0
  115. data/test/unit/error_service/ignored_exceptions_test.rb +49 -0
  116. data/test/unit/extensions/periodic_callbacks_test.rb +2 -2
  117. data/test/unit/instruments/active_record_test.rb +40 -0
  118. data/test/unit/layer_children_set_test.rb +9 -0
  119. data/test/unit/request_histograms_test.rb +17 -0
  120. data/test/unit/serializers/payload_serializer_test.rb +39 -5
  121. data/test/unit/slow_request_policy_test.rb +42 -9
  122. data/test/unit/sql_sanitizer_test.rb +47 -0
  123. data/test/unit/transaction_time_consumed_test.rb +46 -0
  124. data/test/unit/utils/active_record_metric_name_test.rb +10 -2
  125. metadata +101 -19
  126. data/ext/stacks/extconf.rb +0 -37
  127. data/ext/stacks/scout_atomics.h +0 -86
  128. data/ext/stacks/stacks.c +0 -814
  129. data/lib/scout_apm/slow_job_policy.rb +0 -94
  130. data/lib/scout_apm/trace_compactor.rb +0 -312
  131. data/lib/scout_apm/utils/fake_stacks.rb +0 -88
  132. data/test/unit/instruments/active_record_instruments_test.rb +0 -5
  133. data/test/unit/slow_job_policy_test.rb +0 -6
  134. data/tester.rb +0 -53
@@ -54,7 +54,9 @@ module ScoutApm
54
54
  def perform_action_with_scout_instruments(*args, &block)
55
55
  req = ScoutApm::RequestManager.lookup
56
56
  req.annotate_request(:uri => request.path) # for security by-default, we don't use request.fullpath which could reveal filtered params.
57
- req.context.add_user(:ip => request.remote_ip)
57
+ if ScoutApm::Agent.instance.context.config.value('collect_remote_ip')
58
+ req.context.add_user(:ip => request.remote_ip)
59
+ end
58
60
  req.set_headers(request.headers)
59
61
  req.start_layer( ScoutApm::Layer.new("Controller", "#{controller_path}/#{action_name}") )
60
62
 
@@ -1,6 +1,6 @@
1
1
  module ScoutApm
2
2
  module Instruments
3
- # instrumentation for Rails 3, 4, and 5 is the same.
3
+ # instrumentation for Rails 3 and Rails 4 is the same.
4
4
  class ActionControllerRails3Rails4
5
5
  attr_reader :context
6
6
 
@@ -17,37 +17,54 @@ module ScoutApm
17
17
  @installed
18
18
  end
19
19
 
20
+ def installed!
21
+ @installed = true
22
+ end
23
+
20
24
  def install
21
- # We previously instrumented ActionController::Metal, which missed
22
- # before and after filter timing. Instrumenting Base includes those
23
- # filters, at the expense of missing out on controllers that don't use
24
- # the full Rails stack.
25
- if defined?(::ActionController)
26
- @installed = true
25
+ if !defined?(::ActiveSupport)
26
+ return
27
+ end
28
+
29
+ # The block below runs with `self` equal to the ActionController::Base or ::API module, not this class we're in now. By saving an instance of ourselves into the `this` variable, we can continue accessing what we need.
30
+ this = self
31
+
32
+ ActiveSupport.on_load(:action_controller) do
33
+ if this.installed?
34
+ this.logger.info("Skipping ActionController - Already Ran")
35
+ next
36
+ else
37
+ this.logger.info("Instrumenting ActionController (on_load)")
38
+ this.installed!
39
+ end
27
40
 
41
+ # We previously instrumented ActionController::Metal, which missed
42
+ # before and after filter timing. Instrumenting Base includes those
43
+ # filters, at the expense of missing out on controllers that don't use
44
+ # the full Rails stack.
28
45
  if defined?(::ActionController::Base)
29
- logger.info "Instrumenting ActionController::Base"
46
+ this.logger.info "Instrumenting ActionController::Base"
30
47
  ::ActionController::Base.class_eval do
31
- # include ScoutApm::Tracer
32
48
  include ScoutApm::Instruments::ActionControllerBaseInstruments
33
49
  end
34
50
  end
35
51
 
36
52
  if defined?(::ActionController::Metal)
37
- logger.info "Instrumenting ActionController::Metal"
53
+ this.logger.info "Instrumenting ActionController::Metal"
38
54
  ::ActionController::Metal.class_eval do
39
55
  include ScoutApm::Instruments::ActionControllerMetalInstruments
40
56
  end
41
57
  end
42
58
 
43
59
  if defined?(::ActionController::API)
44
- logger.info "Instrumenting ActionController::Api"
60
+ this.logger.info "Instrumenting ActionController::Api"
45
61
  ::ActionController::API.class_eval do
46
62
  include ScoutApm::Instruments::ActionControllerAPIInstruments
47
63
  end
48
64
  end
49
65
  end
50
66
 
67
+ ScoutApm::Agent.instance.context.logger.info("Instrumenting ActionController (hook installed)")
51
68
  end
52
69
 
53
70
  # Returns a new anonymous module each time it is called. So
@@ -57,13 +74,24 @@ module ScoutApm
57
74
  # before_action callbacks
58
75
  def self.build_instrument_module
59
76
  Module.new do
77
+ # Determine the URI of this request to capture. Overridable by users in their controller.
78
+ def scout_transaction_uri(config=ScoutApm::Agent.instance.context.config)
79
+ case config.value("uri_reporting")
80
+ when 'path'
81
+ request.path # strips off the query string for more security
82
+ else # default handles filtered params
83
+ request.filtered_path
84
+ end
85
+ end
86
+
60
87
  def process_action(*args)
61
88
  req = ScoutApm::RequestManager.lookup
62
89
  current_layer = req.current_layer
90
+ agent_context = ScoutApm::Agent.instance.context
63
91
 
64
92
  # Check if this this request is to be reported instantly
65
93
  if instant_key = request.cookies['scoutapminstant']
66
- ScoutApm::Agent.instance.context.logger.info "Instant trace request with key=#{instant_key} for path=#{path}"
94
+ agent_context.logger.info "Instant trace request with key=#{instant_key} for path=#{path}"
67
95
  req.instant_key = instant_key
68
96
  end
69
97
 
@@ -71,25 +99,20 @@ module ScoutApm
71
99
  # Don't start a new layer if ActionController::API or ActionController::Base handled it already.
72
100
  super
73
101
  else
74
- req.annotate_request(:uri => ScoutApm::Instruments::ActionControllerRails3Rails4.scout_transaction_uri(request))
102
+ begin
103
+ uri = scout_transaction_uri
104
+ req.annotate_request(:uri => uri)
105
+ rescue
106
+ end
75
107
 
76
108
  # IP Spoofing Protection can throw an exception, just move on w/o remote ip
77
- req.context.add_user(:ip => request.remote_ip) rescue nil
109
+ if agent_context.config.value('collect_remote_ip')
110
+ req.context.add_user(:ip => request.remote_ip) rescue nil
111
+ end
78
112
  req.set_headers(request.headers)
79
113
 
80
114
  resolved_name = scout_action_name(*args)
81
- layer = ScoutApm::Layer.new("Controller", "#{controller_path}/#{resolved_name}")
82
-
83
- if enable_scoutprof? && ScoutApm::Agent.instance.context.config.value('profile') && ScoutApm::Instruments::Stacks::ENABLED
84
- if defined?(ScoutApm::Instruments::Stacks::INSTALLED) && ScoutApm::Instruments::Stacks::INSTALLED
85
- # Capture ScoutProf if we can
86
- req.enable_profiled_thread!
87
- layer.set_root_class(self.class)
88
- layer.traced!
89
- end
90
- end
91
-
92
- req.start_layer(layer)
115
+ req.start_layer( ScoutApm::Layer.new("Controller", "#{controller_path}/#{resolved_name}") )
93
116
  begin
94
117
  super
95
118
  rescue
@@ -103,39 +126,25 @@ module ScoutApm
103
126
  end
104
127
  end
105
128
 
106
- # Given an +ActionDispatch::Request+, formats the uri based on config settings.
107
- # XXX: Don't lookup context like this - find a way to pass it through
108
- def self.scout_transaction_uri(request, config=ScoutApm::Agent.instance.context.config)
109
- case config.value("uri_reporting")
110
- when 'path'
111
- request.path # strips off the query string for more security
112
- else # default handles filtered params
113
- request.filtered_path
114
- end
115
- end
116
129
  end
117
130
 
118
- module ActionControllerBaseInstruments
131
+ module ActionControllerMetalInstruments
119
132
  include ScoutApm::Instruments::ActionControllerRails3Rails4.build_instrument_module
120
133
 
121
134
  def scout_action_name(*args)
122
- action_name
135
+ action_name = args[0]
123
136
  end
137
+ end
124
138
 
125
- def enable_scoutprof?
126
- true
127
- end
139
+ # Empty, noop module to provide compatibility w/ previous manual instrumentation
140
+ module ActionControllerRails3Rails4Instruments
128
141
  end
129
142
 
130
- module ActionControllerMetalInstruments
143
+ module ActionControllerBaseInstruments
131
144
  include ScoutApm::Instruments::ActionControllerRails3Rails4.build_instrument_module
132
145
 
133
146
  def scout_action_name(*args)
134
- action_name = args[0]
135
- end
136
-
137
- def enable_scoutprof?
138
- false
147
+ action_name
139
148
  end
140
149
  end
141
150
 
@@ -145,14 +154,6 @@ module ScoutApm
145
154
  def scout_action_name(*args)
146
155
  action_name
147
156
  end
148
-
149
- def enable_scoutprof?
150
- false
151
- end
152
- end
153
-
154
- # Empty, noop module to provide compatibility w/ previous manual instrumentation
155
- module ActionControllerRails3Rails4Instruments
156
157
  end
157
158
  end
158
159
  end
@@ -1,6 +1,14 @@
1
+ # Note, this instrument has the same logic in both Tracer and Module Prepend
2
+ # versions. If you update, be sure you update in both spots.
3
+ #
4
+ # The prepend version was added for Rails 6 support - ActiveRecord prepends on
5
+ # top of PartialRenderer#collection_with_template, which can (and does) cause
6
+ # infinite loops with our alias_method approach.
7
+ #
8
+ # Even though Rails 6 forced us to use a prepend version, it is now used for
9
+ # all Rubies that support it.
1
10
  module ScoutApm
2
11
  module Instruments
3
- # instrumentation for Rails 3 and Rails 4 is the same.
4
12
  class ActionView
5
13
  attr_reader :context
6
14
 
@@ -17,37 +25,117 @@ module ScoutApm
17
25
  @installed
18
26
  end
19
27
 
28
+ def prependable?
29
+ context.environment.supports_module_prepend?
30
+ end
31
+
20
32
  def install
21
- if defined?(::ActionView) && defined?(::ActionView::PartialRenderer)
22
- @installed = true
23
-
24
- logger.info "Instrumenting ActionView::PartialRenderer"
25
- ::ActionView::PartialRenderer.class_eval do
26
- include ScoutApm::Tracer
27
-
28
- instrument_method :render_partial,
29
- :type => "View",
30
- :name => '#{@template.virtual_path rescue "Unknown Partial"}/Rendering',
31
- :scope => true
32
-
33
- instrument_method :collection_with_template,
34
- :type => "View",
35
- :name => '#{@template.virtual_path rescue "Unknown Collection"}/Rendering',
36
- :scope => true
33
+ return unless defined?(::ActionView) && defined?(::ActionView::PartialRenderer)
34
+
35
+ if prependable?
36
+ install_using_prepend
37
+ else
38
+ install_using_tracer
39
+ end
40
+ @installed = true
41
+ end
42
+
43
+ def install_using_tracer
44
+ logger.info "Instrumenting ActionView::PartialRenderer"
45
+ ::ActionView::PartialRenderer.class_eval do
46
+ include ScoutApm::Tracer
47
+
48
+ instrument_method :render_partial,
49
+ :type => "View",
50
+ :name => '#{@template.virtual_path rescue "Unknown Partial"}/Rendering',
51
+ :scope => true
52
+
53
+ instrument_method :collection_with_template,
54
+ :type => "View",
55
+ :name => '#{@template.virtual_path rescue "Unknown Collection"}/Rendering',
56
+ :scope => true
57
+ end
58
+
59
+ logger.info "Instrumenting ActionView::TemplateRenderer"
60
+ ::ActionView::TemplateRenderer.class_eval do
61
+ include ScoutApm::Tracer
62
+ instrument_method :render_template,
63
+ :type => "View",
64
+ :name => '#{args[0].virtual_path rescue "Unknown"}/Rendering',
65
+ :scope => true
66
+ end
67
+ end
68
+
69
+ def install_using_prepend
70
+ logger.info "Instrumenting ActionView::PartialRenderer"
71
+ ::ActionView::PartialRenderer.prepend(ActionViewPartialRendererInstruments)
72
+
73
+ logger.info "Instrumenting ActionView::TemplateRenderer"
74
+ ::ActionView::TemplateRenderer.prepend(ActionViewTemplateRendererInstruments)
75
+ end
76
+
77
+ module ActionViewPartialRendererInstruments
78
+ # In Rails 6, the signature changed to pass the view & template args directly, as opposed to through the instance var
79
+ # New signature is: def render_partial(view, template)
80
+ def render_partial(*args)
81
+ req = ScoutApm::RequestManager.lookup
82
+
83
+ maybe_template = args[1]
84
+
85
+ template_name = @template.virtual_path rescue nil # Works on Rails 3.2 -> end of Rails 5 series
86
+ template_name ||= maybe_template.virtual_path rescue nil # Works on Rails 6 -> 6.0.3
87
+ template_name ||= "Unknown Partial"
88
+
89
+ layer_name = template_name + "/Rendering"
90
+ layer = ScoutApm::Layer.new("View", layer_name)
91
+ layer.subscopable!
92
+
93
+ begin
94
+ req.start_layer(layer)
95
+ super(*args)
96
+ ensure
97
+ req.stop_layer
98
+ end
99
+ end
100
+
101
+ def collection_with_template(*args)
102
+ req = ScoutApm::RequestManager.lookup
103
+
104
+ template_name = @template.virtual_path rescue "Unknown Collection"
105
+ template_name ||= "Unknown Collection"
106
+ layer_name = template_name + "/Rendering"
107
+
108
+ layer = ScoutApm::Layer.new("View", layer_name)
109
+ layer.subscopable!
110
+
111
+ begin
112
+ req.start_layer(layer)
113
+ super(*args)
114
+ ensure
115
+ req.stop_layer
37
116
  end
117
+ end
118
+ end
119
+
120
+ module ActionViewTemplateRendererInstruments
121
+ def render_template(*args)
122
+ req = ScoutApm::RequestManager.lookup
38
123
 
39
- logger.info "Instrumenting ActionView::TemplateRenderer"
40
- ::ActionView::TemplateRenderer.class_eval do
41
- include ScoutApm::Tracer
42
- instrument_method :render_template,
43
- :type => "View",
44
- :name => '#{args[0].virtual_path rescue "Unknown"}/Rendering',
45
- :scope => true
124
+ template_name = args[0].virtual_path rescue "Unknown"
125
+ template_name ||= "Unknown"
126
+ layer_name = template_name + "/Rendering"
127
+
128
+ layer = ScoutApm::Layer.new("View", layer_name)
129
+ layer.subscopable!
130
+
131
+ begin
132
+ req.start_layer(layer)
133
+ super(*args)
134
+ ensure
135
+ req.stop_layer
46
136
  end
47
137
  end
48
138
  end
49
139
  end
50
140
  end
51
141
  end
52
-
53
-
@@ -82,10 +82,8 @@ module ScoutApm
82
82
 
83
83
  # Install #log tracing
84
84
  if Utils::KlassHelper.defined?("ActiveRecord::ConnectionAdapters::AbstractAdapter")
85
- ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
86
- include ::ScoutApm::Instruments::ActiveRecordInstruments
87
- include ::ScoutApm::Tracer
88
- end
85
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecordInstruments)
86
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Tracer)
89
87
  end
90
88
 
91
89
  if Utils::KlassHelper.defined?("ActiveRecord::Base")
@@ -142,7 +140,10 @@ module ScoutApm
142
140
  req = ScoutApm::RequestManager.lookup
143
141
  layer = req.current_layer
144
142
  if layer && layer.type == "ActiveRecord"
145
- layer.annotate_layer(payload)
143
+ layer.annotate_layer({
144
+ :class_name => payload[:class_name],
145
+ :record_count => payload[:record_count]
146
+ })
146
147
  elsif layer
147
148
  logger.debug("Expected layer type: ActiveRecord, got #{layer && layer.type}")
148
149
  else
@@ -165,17 +166,63 @@ module ScoutApm
165
166
  #
166
167
  ################################################################################
167
168
  module ActiveRecordInstruments
168
- def self.included(instrumented_class)
169
+ def self.prepended(instrumented_class)
169
170
  ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
170
- instrumented_class.class_eval do
171
- unless instrumented_class.method_defined?(:log_without_scout_instruments)
172
- alias_method :log_without_scout_instruments, :log
173
- alias_method :log, :log_with_scout_instruments
171
+ end
172
+
173
+ def log(*args, &block)
174
+ # Extract data from the arguments
175
+ sql, name = args
176
+ metric_name = Utils::ActiveRecordMetricName.new(sql, name)
177
+ desc = SqlList.new(sql)
178
+
179
+ # Get current ScoutApm context
180
+ req = ScoutApm::RequestManager.lookup
181
+ current_layer = req.current_layer
182
+
183
+ # If we call #log, we have a real query to run, and we've already
184
+ # gotten through the cache gatekeeper. Since we want to only trace real
185
+ # queries, and not repeated identical queries that just hit cache, we
186
+ # mark layer as ignorable initially in #find_by_sql, then only when we
187
+ # know it's a real database call do we mark it back as usable.
188
+ #
189
+ # This flag is later used in SlowRequestConverter to skip adding ignorable layers
190
+ current_layer.annotate_layer(:ignorable => false) if current_layer
191
+
192
+ # Either: update the current layer and yield, don't start a new one.
193
+ if current_layer && current_layer.type == "ActiveRecord"
194
+ # TODO: Get rid of call .to_s, need to find this without forcing a previous run of the name logic
195
+ if current_layer.name.to_s == Utils::ActiveRecordMetricName::DEFAULT_METRIC
196
+ current_layer.name = metric_name
197
+ end
198
+
199
+ if current_layer.desc.nil?
200
+ current_layer.desc = SqlList.new
201
+ end
202
+ current_layer.desc.merge(desc)
203
+
204
+ super(*args, &block)
205
+
206
+ # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack.
207
+ else
208
+ layer = ScoutApm::Layer.new("ActiveRecord", metric_name)
209
+ layer.desc = desc
210
+ req.start_layer(layer)
211
+ begin
212
+ super(*args, &block)
213
+ ensure
214
+ req.stop_layer
174
215
  end
175
216
  end
176
217
  end
218
+ end
219
+
220
+ module ActiveRecordInstruments
221
+ def self.prepended(instrumented_class)
222
+ ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
223
+ end
177
224
 
178
- def log_with_scout_instruments(*args, &block)
225
+ def log(*args, &block)
179
226
  # Extract data from the arguments
180
227
  sql, name = args
181
228
  metric_name = Utils::ActiveRecordMetricName.new(sql, name)
@@ -206,7 +253,7 @@ module ScoutApm
206
253
  end
207
254
  current_layer.desc.merge(desc)
208
255
 
209
- log_without_scout_instruments(*args, &block)
256
+ super(*args, &block)
210
257
 
211
258
  # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack.
212
259
  else
@@ -214,7 +261,7 @@ module ScoutApm
214
261
  layer.desc = desc
215
262
  req.start_layer(layer)
216
263
  begin
217
- log_without_scout_instruments(*args, &block)
264
+ super(*args, &block)
218
265
  ensure
219
266
  req.stop_layer
220
267
  end
@@ -261,14 +308,14 @@ module ScoutApm
261
308
  end
262
309
  end
263
310
 
264
- def find_by_sql_with_scout_instruments(*args, &block)
311
+ def find_by_sql_with_scout_instruments(*args, **kwargs, &block)
265
312
  req = ScoutApm::RequestManager.lookup
266
313
  layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName::DEFAULT_METRIC)
267
314
  layer.annotate_layer(:ignorable => true)
268
315
  req.start_layer(layer)
269
316
  req.ignore_children!
270
317
  begin
271
- find_by_sql_without_scout_instruments(*args, &block)
318
+ find_by_sql_without_scout_instruments(*args, **kwargs, &block)
272
319
  ensure
273
320
  req.acknowledge_children!
274
321
  req.stop_layer
@@ -346,7 +393,7 @@ module ScoutApm
346
393
  end
347
394
 
348
395
  module ActiveRecordUpdateInstruments
349
- def save(*args, &block)
396
+ def save(*args, **options, &block)
350
397
  model = self.class.name
351
398
  operation = self.persisted? ? "Update" : "Create"
352
399
 
@@ -356,14 +403,14 @@ module ScoutApm
356
403
  req.start_layer(layer)
357
404
  req.ignore_children!
358
405
  begin
359
- super(*args, &block)
406
+ super(*args, **options, &block)
360
407
  ensure
361
408
  req.acknowledge_children!
362
409
  req.stop_layer
363
410
  end
364
411
  end
365
412
 
366
- def save!(*args, &block)
413
+ def save!(*args, **options, &block)
367
414
  model = self.class.name
368
415
  operation = self.persisted? ? "Update" : "Create"
369
416
 
@@ -372,7 +419,7 @@ module ScoutApm
372
419
  req.start_layer(layer)
373
420
  req.ignore_children!
374
421
  begin
375
- super(*args, &block)
422
+ super(*args, **options, &block)
376
423
  ensure
377
424
  req.acknowledge_children!
378
425
  req.stop_layer