scout_apm 2.6.6 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -82,15 +82,8 @@ module ScoutApm
82
82
 
83
83
  # Install #log tracing
84
84
  if Utils::KlassHelper.defined?("ActiveRecord::ConnectionAdapters::AbstractAdapter")
85
- if Module.respond_to?(:prepend)
86
- ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecordInstruments)
87
- ::ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Tracer)
88
- else
89
- ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
90
- include ::ScoutApm::Instruments::ActiveRecordAliasMethodInstruments
91
- include ::ScoutApm::Tracer
92
- end
93
- end
85
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecordInstruments)
86
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Tracer)
94
87
  end
95
88
 
96
89
  if Utils::KlassHelper.defined?("ActiveRecord::Base")
@@ -172,20 +165,12 @@ module ScoutApm
172
165
  # to the real SQL, and an AR generated "name" for the Query
173
166
  #
174
167
  ################################################################################
175
- #
176
- # Note, if you change this instrumentation, you also need to change ActiveRecordInstruments.
177
- module ActiveRecordAliasMethodInstruments
178
- def self.included(instrumented_class)
168
+ module ActiveRecordInstruments
169
+ def self.prepended(instrumented_class)
179
170
  ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
180
- instrumented_class.class_eval do
181
- unless instrumented_class.method_defined?(:log_without_scout_instruments)
182
- alias_method :log_without_scout_instruments, :log
183
- alias_method :log, :log_with_scout_instruments
184
- end
185
- end
186
171
  end
187
172
 
188
- def log_with_scout_instruments(*args, &block)
173
+ def log(*args, &block)
189
174
  # Extract data from the arguments
190
175
  sql, name = args
191
176
  metric_name = Utils::ActiveRecordMetricName.new(sql, name)
@@ -216,7 +201,7 @@ module ScoutApm
216
201
  end
217
202
  current_layer.desc.merge(desc)
218
203
 
219
- log_without_scout_instruments(*args, &block)
204
+ super(*args, &block)
220
205
 
221
206
  # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack.
222
207
  else
@@ -224,7 +209,7 @@ module ScoutApm
224
209
  layer.desc = desc
225
210
  req.start_layer(layer)
226
211
  begin
227
- log_without_scout_instruments(*args, &block)
212
+ super(*args, &block)
228
213
  ensure
229
214
  req.stop_layer
230
215
  end
@@ -323,14 +308,14 @@ module ScoutApm
323
308
  end
324
309
  end
325
310
 
326
- def find_by_sql_with_scout_instruments(*args, &block)
311
+ def find_by_sql_with_scout_instruments(*args, **kwargs, &block)
327
312
  req = ScoutApm::RequestManager.lookup
328
313
  layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName::DEFAULT_METRIC)
329
314
  layer.annotate_layer(:ignorable => true)
330
315
  req.start_layer(layer)
331
316
  req.ignore_children!
332
317
  begin
333
- find_by_sql_without_scout_instruments(*args, &block)
318
+ find_by_sql_without_scout_instruments(*args, **kwargs, &block)
334
319
  ensure
335
320
  req.acknowledge_children!
336
321
  req.stop_layer
@@ -408,7 +393,7 @@ module ScoutApm
408
393
  end
409
394
 
410
395
  module ActiveRecordUpdateInstruments
411
- def save(*args, &block)
396
+ def save(*args, **options, &block)
412
397
  model = self.class.name
413
398
  operation = self.persisted? ? "Update" : "Create"
414
399
 
@@ -418,14 +403,14 @@ module ScoutApm
418
403
  req.start_layer(layer)
419
404
  req.ignore_children!
420
405
  begin
421
- super(*args, &block)
406
+ super(*args, **options, &block)
422
407
  ensure
423
408
  req.acknowledge_children!
424
409
  req.stop_layer
425
410
  end
426
411
  end
427
412
 
428
- def save!(*args, &block)
413
+ def save!(*args, **options, &block)
429
414
  model = self.class.name
430
415
  operation = self.persisted? ? "Update" : "Create"
431
416
 
@@ -434,7 +419,7 @@ module ScoutApm
434
419
  req.start_layer(layer)
435
420
  req.ignore_children!
436
421
  begin
437
- super(*args, &block)
422
+ super(*args, **options, &block)
438
423
  ensure
439
424
  req.acknowledge_children!
440
425
  req.stop_layer
@@ -26,7 +26,7 @@ module ScoutApm
26
26
  ScoutApm::Agent.instance.start
27
27
  @started = ScoutApm::Agent.instance.context.started? && ScoutApm::Agent.instance.background_worker_running?
28
28
  rescue => e
29
- ScoutApm::Agent.instance.context.logger("Failed to start via Middleware: #{e.message}\n\t#{e.backtrace.join("\n\t")}")
29
+ ScoutApm::Agent.instance.context.logger.info("Failed to start via Middleware: #{e.message}\n\t#{e.backtrace.join("\n\t")}")
30
30
  end
31
31
  end
32
32
  end
@@ -2,7 +2,6 @@ require 'openssl'
2
2
 
3
3
  module ScoutApm
4
4
  class Reporter
5
- CA_FILE = File.join( File.dirname(__FILE__), *%w[.. .. data cacert.pem] )
6
5
  VERIFY_MODE = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
7
6
 
8
7
  attr_reader :type
@@ -23,6 +22,7 @@ module ScoutApm
23
22
  context.logger
24
23
  end
25
24
 
25
+ # The fully serialized string payload to be sent
26
26
  def report(payload, headers = {})
27
27
  hosts = determine_hosts
28
28
 
@@ -36,6 +36,7 @@ module ScoutApm
36
36
  logger.debug("Original Size: #{original_payload_size} Compressed Size: #{compress_payload_size}")
37
37
  end
38
38
 
39
+ logger.info("Posting payload to #{hosts.inspect}")
39
40
  post_payload(hosts, payload, headers)
40
41
  end
41
42
 
@@ -52,6 +53,8 @@ module ScoutApm
52
53
  URI.parse("#{host}/apps/deploy.scout?key=#{key}&name=#{encoded_app_name}")
53
54
  when :instant_trace
54
55
  URI.parse("#{host}/apps/instant_trace.scout?key=#{key}&name=#{encoded_app_name}&instant_key=#{instant_key}")
56
+ when :errors
57
+ URI.parse("#{host}/apps/error.scout?key=#{key}&name=#{encoded_app_name}")
55
58
  end.tap { |u| logger.debug("Posting to #{u}") }
56
59
  end
57
60
 
@@ -90,7 +93,7 @@ module ScoutApm
90
93
  logger.debug "got response: #{response.inspect}"
91
94
  case response
92
95
  when Net::HTTPSuccess, Net::HTTPNotModified
93
- logger.debug "/#{type} OK"
96
+ logger.debug "#{type} OK"
94
97
  when Net::HTTPBadRequest
95
98
  logger.warn "/#{type} FAILED: The Account Key [#{config.value('key')}] is invalid."
96
99
  when Net::HTTPUnprocessableEntity
@@ -123,7 +126,7 @@ module ScoutApm
123
126
  proxy_uri.password).new(url.host, url.port)
124
127
  if url.is_a?(URI::HTTPS)
125
128
  http.use_ssl = true
126
- http.ca_file = CA_FILE
129
+ http.ca_file = config.value("ssl_cert_file")
127
130
  http.verify_mode = VERIFY_MODE
128
131
  end
129
132
  http
@@ -142,6 +145,8 @@ module ScoutApm
142
145
  def determine_hosts
143
146
  if [:deploy_hook, :instant_trace].include?(type)
144
147
  config.value('direct_host')
148
+ elsif [:errors].include?(type)
149
+ config.value('errors_host')
145
150
  else
146
151
  config.value('host')
147
152
  end
@@ -45,18 +45,36 @@ module ScoutApm
45
45
  "{#{str_parts.join(",")}}"
46
46
  end
47
47
 
48
- ESCAPE_MAPPINGS = {
49
- "\b" => '\\b',
50
- "\t" => '\\t',
51
- "\n" => '\\n',
52
- "\f" => '\\f',
53
- "\r" => '\\r',
54
- '"' => '\\"',
55
- '\\' => '\\\\',
56
- }
48
+ # Ruby 1.8.7 seems to be fundamentally different in how gsub or regexes
49
+ # work. This is a hack and will be removed as soon as we can drop
50
+ # support
51
+ if RUBY_VERSION == "1.8.7"
52
+ ESCAPE_MAPPINGS = {
53
+ "\b" => '\\b',
54
+ "\t" => '\\t',
55
+ "\n" => '\\n',
56
+ "\f" => '\\f',
57
+ "\r" => '\\r',
58
+ '"' => '\\"',
59
+ '\\' => '\\\\',
60
+ }
61
+ else
62
+ ESCAPE_MAPPINGS = {
63
+ # Stackoverflow answer on gsub matches and backslashes - https://stackoverflow.com/a/4149087/2705125
64
+ '\\' => '\\\\\\\\',
65
+ "\b" => '\\b',
66
+ "\t" => '\\t',
67
+ "\n" => '\\n',
68
+ "\f" => '\\f',
69
+ "\r" => '\\r',
70
+ '"' => '\\"',
71
+ }
72
+ end
57
73
 
58
74
  def escape(string)
59
- ESCAPE_MAPPINGS.inject(string.to_s) {|s, (bad, good)| s.gsub(bad, good) }
75
+ ESCAPE_MAPPINGS.inject(string.to_s) {|s, (bad, good)|
76
+ s.gsub(bad, good)
77
+ }
60
78
  end
61
79
 
62
80
  def format_by_type(formatee)
@@ -0,0 +1,33 @@
1
+ require 'scout_apm/slow_policy/policy'
2
+
3
+ module ScoutApm::SlowPolicy
4
+ class AgePolicy < Policy
5
+ # For each minute we haven't seen an endpoint
6
+ POINT_MULTIPLIER_AGE = 0.25
7
+
8
+ # A hash of Endpoint Name to the last time we stored a slow transaction for it.
9
+ #
10
+ # Defaults to a start time that is pretty close to application boot time.
11
+ # So the "age" of an endpoint we've never seen is the time the application
12
+ # has been running.
13
+ attr_reader :last_seen
14
+
15
+ def initialize(context)
16
+ super
17
+
18
+ zero_time = Time.now
19
+ @last_seen = Hash.new { |h, k| h[k] = zero_time }
20
+ end
21
+
22
+ def call(request)
23
+ # How long has it been since we've seen this?
24
+ age = Time.now - last_seen[request.unique_name]
25
+
26
+ age / 60.0 * POINT_MULTIPLIER_AGE
27
+ end
28
+
29
+ def stored!(request)
30
+ last_seen[request.unique_name] = Time.now
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'scout_apm/slow_policy/policy'
2
+
3
+ module ScoutApm::SlowPolicy
4
+ class PercentPolicy < Policy
5
+ # Points for an endpoint's who's throughput * response time is a large % of
6
+ # overall time spent processing requests
7
+ POINT_MULTIPLIER_PERCENT_TIME = 2.5
8
+
9
+ # Of the total time spent handling endpoints in this app, if this endpoint
10
+ # is a higher percent, it should get more points.
11
+ #
12
+ # A: 20 calls @ 100ms each => 2 seconds of total time
13
+ # B: 10 calls @ 100ms each => 1 second of total time
14
+ #
15
+ # Then A is 66% of the total call time
16
+ def call(request) # Scale 0.0 - 1.0
17
+ percent = context.transaction_time_consumed.percent_of_total(request.unique_name)
18
+
19
+ percent * POINT_MULTIPLIER_PERCENT_TIME
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ require 'scout_apm/slow_policy/policy'
2
+
3
+ module ScoutApm::SlowPolicy
4
+ class PercentilePolicy < Policy
5
+ def call(request)
6
+ # What approximate percentile was this request?
7
+ total_time = request.root_layer.total_call_time
8
+ percentile = context.request_histograms.approximate_quantile_of_value(request.unique_name, total_time)
9
+
10
+ if percentile < 40
11
+ 0.4 # Don't put much emphasis on capturing low percentiles.
12
+ elsif percentile < 60
13
+ 1.4 # Highest here to get mean traces
14
+ elsif percentile < 90
15
+ 0.7 # Between 60 & 90% is fine.
16
+ elsif percentile >= 90
17
+ 1.4 # Highest here to get 90+%ile traces
18
+ else
19
+ # impossible.
20
+ percentile
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ # Note that this is semi-internal API. You should not need this, and if you do
2
+ # we're here to help at support@scoutapm.com. TrackedRequest doesn't change
3
+ # often, but we can't promise a perfectly stable API for it either.
4
+ module ScoutApm::SlowPolicy
5
+ class Policy
6
+ attr_reader :context
7
+
8
+ def initialize(context)
9
+ @context = context
10
+ end
11
+
12
+ def call(request)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ # Override in subclasses to execute some behavior if the request gets a
17
+ # slot in the ScoredItemSet. Defaults to no-op
18
+ def stored!(request)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ require 'scout_apm/slow_policy/policy'
2
+
3
+ module ScoutApm::SlowPolicy
4
+ class SpeedPolicy < Policy
5
+ # Adjust speed points. See the function
6
+ POINT_MULTIPLIER_SPEED = 0.25
7
+
8
+ # Time in seconds
9
+ # Logarithm keeps huge times from swamping the other metrics.
10
+ # 1+ is necessary to keep the log function in positive territory.
11
+ def call(request)
12
+ total_time = request.root_layer.total_call_time
13
+ Math.log(1 + total_time) * POINT_MULTIPLIER_SPEED
14
+ end
15
+ end
16
+ end
@@ -3,43 +3,29 @@
3
3
 
4
4
  module ScoutApm
5
5
  class SlowRequestPolicy
6
- CAPTURE_TYPES = [
7
- CAPTURE_DETAIL = "capture_detail",
8
- CAPTURE_NONE = "capture_none",
9
- ]
10
-
11
- # Adjust speed points. See the function
12
- POINT_MULTIPLIER_SPEED = 0.25
13
-
14
- # For each minute we haven't seen an endpoint
15
- POINT_MULTIPLIER_AGE = 0.25
16
-
17
- # Outliers are worth up to "1000ms" of weight
18
- POINT_MULTIPLIER_PERCENTILE = 1.0
19
-
20
- # Points for an endpoint's who's throughput * response time is a large % of
21
- # overall time spent processing requests
22
- POINT_MULTIPLIER_PERCENT_TIME = 2.5
23
-
24
- # A hash of Endpoint Name to the last time we stored a slow transaction for it.
25
- #
26
- # Defaults to a start time that is pretty close to application boot time.
27
- # So the "age" of an endpoint we've never seen is the time the application
28
- # has been running.
29
- attr_reader :last_seen
30
-
31
6
  # The AgentContext we're running in
32
7
  attr_reader :context
8
+ attr_reader :policies
33
9
 
34
10
  def initialize(context)
35
11
  @context = context
12
+ @policies = []
13
+ end
36
14
 
37
- zero_time = Time.now
38
- @last_seen = Hash.new { |h, k| h[k] = zero_time }
15
+ def add_default_policies
16
+ add(SlowPolicy::SpeedPolicy.new(context))
17
+ add(SlowPolicy::PercentilePolicy.new(context))
18
+ add(SlowPolicy::AgePolicy.new(context))
19
+ add(SlowPolicy::PercentilePolicy.new(context))
39
20
  end
40
21
 
41
- def stored!(request)
42
- last_seen[request.unique_name] = Time.now
22
+ # policy is an object that behaves like a policy (responds to .call(req) for the score, and .store!(req))
23
+ def add(policy)
24
+ unless policy.respond_to?(:call) && policy.respond_to?(:stored!)
25
+ raise "SlowRequestPolicy must implement policy api call(req) and stored!(req)"
26
+ end
27
+
28
+ @policies << policy
43
29
  end
44
30
 
45
31
  # Determine if this request trace should be fully analyzed by scoring it
@@ -56,56 +42,11 @@ module ScoutApm
56
42
  return -1 # A negative score, should never be good enough to store.
57
43
  end
58
44
 
59
- total_time = request.root_layer.total_call_time
60
-
61
- # How long has it been since we've seen this?
62
- age = Time.now - last_seen[unique_name]
63
-
64
- # What approximate percentile was this request?
65
- percentile = context.request_histograms.approximate_quantile_of_value(unique_name, total_time)
66
-
67
- percent_of_total_time = context.transaction_time_consumed.percent_of_total(unique_name)
68
-
69
- return speed_points(total_time) + percentile_points(percentile) + age_points(age) + percent_time_points(percent_of_total_time)
70
- end
71
-
72
- private
73
-
74
- # Time in seconds
75
- # Logarithm keeps huge times from swamping the other metrics.
76
- # 1+ is necessary to keep the log function in positive territory.
77
- def speed_points(time)
78
- Math.log(1 + time) * POINT_MULTIPLIER_SPEED
79
- end
80
-
81
- def percentile_points(percentile)
82
- if percentile < 40
83
- 0.4 # Don't put much emphasis on capturing low percentiles.
84
- elsif percentile < 60
85
- 1.4 # Highest here to get mean traces
86
- elsif percentile < 90
87
- 0.7 # Between 60 & 90% is fine.
88
- elsif percentile >= 90
89
- 1.4 # Highest here to get 90+%ile traces
90
- else
91
- # impossible.
92
- percentile
93
- end
94
- end
95
-
96
- def age_points(age)
97
- age / 60.0 * POINT_MULTIPLIER_AGE
45
+ policies.map{ |p| p.call(request) }.sum
98
46
  end
99
47
 
100
- # Of the total time spent handling endpoints in this app, if this endpoint
101
- # is a higher percent, it should get more points.
102
- #
103
- # A: 20 calls @ 100ms each => 2 seconds of total time
104
- # B: 10 calls @ 100ms each => 1 second of total time
105
- #
106
- # Then A is 66% of the total call time
107
- def percent_time_points(percent) # Scale 0.0 - 1.0
108
- percent * POINT_MULTIPLIER_PERCENT_TIME
48
+ def stored!(request)
49
+ policies.each{ |p| p.stored!(request) }
109
50
  end
110
51
  end
111
52
  end