scout_apm 2.6.6 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -4
  3. data/.travis.yml +0 -6
  4. data/CHANGELOG.markdown +30 -0
  5. data/Gemfile +1 -8
  6. data/lib/scout_apm.rb +21 -1
  7. data/lib/scout_apm/agent.rb +22 -0
  8. data/lib/scout_apm/agent_context.rb +14 -2
  9. data/lib/scout_apm/background_job_integrations/sidekiq.rb +2 -2
  10. data/lib/scout_apm/config.rb +17 -2
  11. data/lib/scout_apm/detailed_trace.rb +2 -1
  12. data/lib/scout_apm/error.rb +27 -0
  13. data/lib/scout_apm/error_service.rb +32 -0
  14. data/lib/scout_apm/error_service/error_buffer.rb +39 -0
  15. data/lib/scout_apm/error_service/error_record.rb +211 -0
  16. data/lib/scout_apm/error_service/ignored_exceptions.rb +66 -0
  17. data/lib/scout_apm/error_service/middleware.rb +32 -0
  18. data/lib/scout_apm/error_service/notifier.rb +33 -0
  19. data/lib/scout_apm/error_service/payload.rb +47 -0
  20. data/lib/scout_apm/error_service/periodic_work.rb +17 -0
  21. data/lib/scout_apm/error_service/railtie.rb +11 -0
  22. data/lib/scout_apm/error_service/sidekiq.rb +80 -0
  23. data/lib/scout_apm/extensions/transaction_callback_payload.rb +1 -1
  24. data/lib/scout_apm/instruments/action_controller_rails_3_rails4.rb +47 -26
  25. data/lib/scout_apm/instruments/action_view.rb +7 -2
  26. data/lib/scout_apm/instruments/active_record.rb +13 -28
  27. data/lib/scout_apm/middleware.rb +1 -1
  28. data/lib/scout_apm/reporter.rb +8 -3
  29. data/lib/scout_apm/serializers/payload_serializer_to_json.rb +28 -10
  30. data/lib/scout_apm/slow_policy/age_policy.rb +33 -0
  31. data/lib/scout_apm/slow_policy/percent_policy.rb +22 -0
  32. data/lib/scout_apm/slow_policy/percentile_policy.rb +24 -0
  33. data/lib/scout_apm/slow_policy/policy.rb +21 -0
  34. data/lib/scout_apm/slow_policy/speed_policy.rb +16 -0
  35. data/lib/scout_apm/slow_request_policy.rb +18 -77
  36. data/lib/scout_apm/utils/sql_sanitizer.rb +1 -0
  37. data/lib/scout_apm/utils/sql_sanitizer_regex.rb +1 -1
  38. data/lib/scout_apm/utils/sql_sanitizer_regex_1_8_7.rb +1 -0
  39. data/lib/scout_apm/version.rb +1 -1
  40. data/scout_apm.gemspec +6 -6
  41. data/test/unit/agent_context_test.rb +29 -0
  42. data/test/unit/error_service/error_buffer_test.rb +25 -0
  43. data/test/unit/error_service/ignored_exceptions_test.rb +49 -0
  44. data/test/unit/serializers/payload_serializer_test.rb +36 -0
  45. data/test/unit/slow_request_policy_test.rb +41 -13
  46. data/test/unit/sql_sanitizer_test.rb +7 -0
  47. metadata +25 -62
  48. data/lib/scout_apm/slow_job_policy.rb +0 -111
  49. data/test/unit/slow_job_policy_test.rb +0 -6
@@ -0,0 +1,66 @@
1
+ # Encapsulates the management and checking of ignored exceptions. Allows using
2
+ # string matches on the class name, or arbitrary matching with a callback
3
+ module ScoutApm
4
+ module ErrorService
5
+ class IgnoredExceptions
6
+ attr_reader :ignored_exceptions
7
+ attr_reader :blocks
8
+
9
+ def initialize(context, from_config)
10
+ @context = context
11
+ @ignored_exceptions = Array(from_config).map{ |e| normalize_as_klass(e) }
12
+ @blocks = []
13
+ end
14
+
15
+ # Add a single ignored exception by class name
16
+ def add(klass_or_str)
17
+ @ignored_exceptions << normalize_as_klass(klass_or_str)
18
+ end
19
+
20
+ # Add a callback block that will be called on every error. If it returns
21
+ # Signature of blocks: ->(exception object): truthy or falsy value
22
+ def add_callback(&block)
23
+ @blocks << block
24
+ end
25
+
26
+ def ignored?(exception_object)
27
+ klass = normalize_as_klass(exception_object)
28
+
29
+ # Check if we ignored this error by name (typical way to ignore)
30
+ if ignored_exceptions.any? { |ignored| klass.ancestors.include?(ignored) }
31
+ return true
32
+ end
33
+
34
+ # For each block, see if it says we should ignore this error
35
+ blocks.each do |b|
36
+ if b.call(exception_object)
37
+ return true
38
+ end
39
+ end
40
+
41
+ false
42
+ end
43
+
44
+ private
45
+
46
+ def normalize_as_klass(klass_or_str)
47
+ if Module === klass_or_str
48
+ return klass_or_str
49
+ end
50
+
51
+ if klass_or_str.is_a?(Exception)
52
+ return klass_or_str.class
53
+ end
54
+
55
+ if String === klass_or_str
56
+ maybe = ScoutApm::Utils::KlassHelper.lookup(klass_or_str)
57
+ if Module === maybe
58
+ return maybe
59
+ end
60
+ end
61
+
62
+ klass_or_str
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,32 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class Middleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ begin
10
+ response = @app.call(env)
11
+ rescue Exception => exception
12
+ puts "[Scout Error Service] Caught Exception: #{exception.class.name}"
13
+
14
+ context = ScoutApm::Agent.instance.context
15
+
16
+ # Bail out early, and reraise if the error is not interesting.
17
+ if context.ignored_exceptions.ignored?(exception)
18
+ raise
19
+ end
20
+
21
+ # Capture the error for further processing and shipping
22
+ context.error_buffer.capture(exception, env)
23
+
24
+ # Finally re-raise
25
+ raise
26
+ end
27
+
28
+ response
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class Notifier
4
+ attr_reader :context
5
+ attr_reader :reporter
6
+
7
+ def initialize(context)
8
+ @context = context
9
+ @reporter = ScoutApm::Reporter.new(context, :errors)
10
+ end
11
+
12
+ def ship
13
+ error_records = context.error_buffer.get_and_reset_error_records
14
+ if error_records.any?
15
+ payload = ScoutApm::ErrorService::Payload.new(context, error_records)
16
+ reporter.report(
17
+ payload.serialize,
18
+ default_headers.merge("X-Error-Count" => error_records.length.to_s)
19
+ )
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def default_headers
26
+ {
27
+ "Content-Type" => "application/json",
28
+ "Accept" => "application/json"
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class Payload
4
+ attr_reader :context
5
+ attr_reader :errors
6
+
7
+ def initialize(context, errors)
8
+ @context = context
9
+ @errors = errors
10
+ end
11
+
12
+ # TODO: Don't use to_json since it isn't supported in Ruby 1.8.7
13
+ def serialize
14
+ payload = as_json.to_json
15
+ context.logger.info(payload)
16
+ payload
17
+ end
18
+
19
+ def as_json
20
+ serialized_errors = errors.map do |error_record|
21
+ serialize_error_record(error_record)
22
+ end
23
+
24
+ {
25
+ :notifier => "scout_apm_ruby",
26
+ :environment => context.environment.env,
27
+ :root => context.environment.root,
28
+ :problems => serialized_errors,
29
+ }
30
+ end
31
+
32
+ def serialize_error_record(error_record)
33
+ {
34
+ :exception_class => error_record.exception_class,
35
+ :message => error_record.message,
36
+ :request_uri => error_record.request_uri,
37
+ :request_params => error_record.request_params,
38
+ :request_session => error_record.request_session,
39
+ :environment => error_record.environment,
40
+ :trace => error_record.trace,
41
+ :request_components => error_record.request_components,
42
+ :context => error_record.context,
43
+ }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class PeriodicWork
4
+ attr_reader :context
5
+
6
+ def initialize(context)
7
+ @context = context
8
+ @notifier = ScoutApm::ErrorService::Notifier.new(context)
9
+ end
10
+
11
+ # Expected to be called many times over the life of the agent
12
+ def run
13
+ @notifier.ship
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class Railtie < Rails::Railtie
4
+ initializer "scoutapm_error_service.middleware" do |app|
5
+ next if ScoutApm::Agent.instance.config.value("error_service")
6
+
7
+ app.config.middleware.insert_after ActionDispatch::DebugExceptions, ScoutApm::ErrorService::Rack
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,80 @@
1
+ module ScoutApm
2
+ module ErrorService
3
+ class Sidekiq
4
+ def initialize
5
+ @context = ScoutApm::Agent.instance.context
6
+ end
7
+
8
+ def install
9
+ return false unless defined?(::Sidekiq)
10
+
11
+ if ::Sidekiq::VERSION < "3"
12
+ install_sidekiq_with_middleware
13
+ else
14
+ install_sidekiq_with_error_handler
15
+ end
16
+
17
+ true
18
+ end
19
+
20
+ def install_sidekiq_with_middleware
21
+ # old behavior
22
+ ::Sidekiq.configure_server do |config|
23
+ config.server_middleware do |chain|
24
+ chain.add ScoutApm::ErrorService::Sidekiq::SidekiqExceptionMiddleware
25
+ end
26
+ end
27
+ end
28
+
29
+ def install_sidekiq_with_error_handler
30
+ ::Sidekiq.configure_server do |config|
31
+ config.error_handlers << proc { |exception, job_info|
32
+ context = ScoutApm::Agent.instance.context
33
+
34
+ # Bail out early, and reraise if the error is not interesting.
35
+ if context.ignored_exceptions.ignored?(exception)
36
+ raise
37
+ end
38
+
39
+ job_class =
40
+ begin
41
+ job_class = job_info[:job]["class"]
42
+ job_class = job_info[:job]["args"][0]["job_class"] if job_class == "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
43
+ job_class
44
+ rescue
45
+ "UnknownJob"
46
+ end
47
+
48
+ # Capture the error for further processing and shipping
49
+ context.error_buffer.capture(exception, job_info.merge(:custom_controller => job_class))
50
+ }
51
+ end
52
+ end
53
+
54
+ class SidekiqExceptionMiddleware
55
+ def call(worker, msg, queue)
56
+ yield
57
+ rescue => exception
58
+ context = ScoutApm::Agent.instance.context
59
+
60
+ # Bail out early, and reraise if the error is not interesting.
61
+ if context.ignored_exceptions.ignored?(exception)
62
+ raise
63
+ end
64
+
65
+ # Capture the error for further processing and shipping
66
+ context.error_buffer.capture(
67
+ exception,
68
+ {
69
+ :custom_params => msg,
70
+ :custom_controller => msg["class"]
71
+ }
72
+ )
73
+
74
+ # Finally, reraise
75
+ raise exception
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -26,7 +26,7 @@ module ScoutApm
26
26
  # The time in queue of the transaction in ms. If not present, +nil+ is returned as this is unknown.
27
27
  def queue_time_ms
28
28
  # Controller logic
29
- if converter_results[:queue_time] && converter_results[:queue].any?
29
+ if converter_results[:queue_time] && converter_results[:queue_time].any?
30
30
  converter_results[:queue_time].values.first.total_call_time*1000 # ms
31
31
  # Job logic
32
32
  elsif converter_results[:job]
@@ -17,46 +17,73 @@ 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
27
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
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
 
51
- # Returns a new anonymous module each time it is called. So
52
- # we can insert this multiple times into the ancestors
53
- # stack. Otherwise it only exists the first time you include it
54
- # (under Metal, instead of under API) and we miss instrumenting
55
- # before_action callbacks
67
+ ScoutApm::Agent.instance.context.logger.info("Instrumenting ActionController (hook installed)")
56
68
  end
57
69
 
70
+ # Returns a new anonymous module each time it is called. So
71
+ # we can insert this multiple times into the ancestors
72
+ # stack. Otherwise it only exists the first time you include it
73
+ # (under Metal, instead of under API) and we miss instrumenting
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
@@ -72,7 +99,11 @@ module ScoutApm
72
99
  # Don't start a new layer if ActionController::API or ActionController::Base handled it already.
73
100
  super
74
101
  else
75
- 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
76
107
 
77
108
  # IP Spoofing Protection can throw an exception, just move on w/o remote ip
78
109
  if agent_context.config.value('collect_remote_ip')
@@ -95,16 +126,6 @@ module ScoutApm
95
126
  end
96
127
  end
97
128
 
98
- # Given an +ActionDispatch::Request+, formats the uri based on config settings.
99
- # XXX: Don't lookup context like this - find a way to pass it through
100
- def self.scout_transaction_uri(request, config=ScoutApm::Agent.instance.context.config)
101
- case config.value("uri_reporting")
102
- when 'path'
103
- request.path # strips off the query string for more security
104
- else # default handles filtered params
105
- request.filtered_path
106
- end
107
- end
108
129
  end
109
130
 
110
131
  module ActionControllerMetalInstruments
@@ -75,13 +75,18 @@ module ScoutApm
75
75
  end
76
76
 
77
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)
78
80
  def render_partial(*args)
79
81
  req = ScoutApm::RequestManager.lookup
80
82
 
81
- template_name = @template.virtual_path rescue "Unknown Partial"
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
82
87
  template_name ||= "Unknown Partial"
83
- layer_name = template_name + "/Rendering"
84
88
 
89
+ layer_name = template_name + "/Rendering"
85
90
  layer = ScoutApm::Layer.new("View", layer_name)
86
91
  layer.subscopable!
87
92