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
@@ -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,6 +17,9 @@ module ScoutApm
17
17
  def track_one!(type, name, value, options={})
18
18
  end
19
19
 
20
+ def track_trace!(trace, type)
21
+ end
22
+
20
23
  def track_histograms!(histograms, options={})
21
24
  end
22
25
 
@@ -15,7 +15,8 @@ module ScoutApm
15
15
 
16
16
  def present?
17
17
  defined?(::Rails) &&
18
- defined?(ActionController) &&
18
+ defined?(::Rails::VERSION) &&
19
+ defined?(ActionController) &&
19
20
  Rails::VERSION::MAJOR < 3
20
21
  end
21
22
 
@@ -15,7 +15,8 @@ module ScoutApm
15
15
 
16
16
  def present?
17
17
  defined?(::Rails) &&
18
- defined?(ActionController) &&
18
+ defined?(::Rails::VERSION) &&
19
+ defined?(ActionController) &&
19
20
  Rails::VERSION::MAJOR >= 3
20
21
  end
21
22
 
@@ -47,6 +48,7 @@ module ScoutApm
47
48
  when "sqlite" then :sqlite
48
49
  when "mysql" then :mysql
49
50
  when "mysql2" then :mysql
51
+ when "sqlserver" then :sqlserver
50
52
  else default
51
53
  end
52
54
  else
@@ -55,12 +57,21 @@ module ScoutApm
55
57
  end
56
58
  end
57
59
 
60
+ # Note, this code intentionally avoids `.respond_to?` because of an
61
+ # infinite loop created by the Textacular gem (tested against 3.2.2 of
62
+ # that gem), which some customers have installed.
63
+ #
64
+ # The loop was:
65
+ # - Ask for database adapter
66
+ # - Do .respond_to? on AR::Base to look for connection_config (which isn't present on some versions of rails)
67
+ # - Textacular gem has a monkey-patch that queries the columns of the db
68
+ # This obtains a connection, and runs a query.
69
+ # - Scout tries to run SQLSanitizer against the query, which needs the database adapter.
70
+ # - Goes back to first step.
71
+ #
72
+ # We avoid this issue by not calling .respond_to? here, and instead using the less optimal `rescue nil` approach
58
73
  def raw_database_adapter
59
- adapter = if ActiveRecord::Base.respond_to?(:connection_config)
60
- ActiveRecord::Base.connection_config[:adapter].to_s
61
- else
62
- nil
63
- end
74
+ adapter = ActiveRecord::Base.connection_config[:adapter].to_s rescue nil
64
75
 
65
76
  if adapter.nil?
66
77
  adapter = ActiveRecord::Base.configurations[env]["adapter"]
@@ -17,7 +17,7 @@ module ScoutApm
17
17
  private
18
18
 
19
19
  def detect
20
- detect_from_env_var ||
20
+ detect_from_config ||
21
21
  detect_from_heroku ||
22
22
  detect_from_capistrano ||
23
23
  detect_from_git
@@ -27,8 +27,11 @@ module ScoutApm
27
27
  ENV['HEROKU_SLUG_COMMIT']
28
28
  end
29
29
 
30
- def detect_from_env_var
31
- ENV['SCOUT_REVISION_SHA']
30
+ # Config will locate the value from:
31
+ # ENV variable - SCOUT_REVISION_SHA
32
+ # YAML setting - revision_sha
33
+ def detect_from_config
34
+ context.config.value('revision_sha')
32
35
  end
33
36
 
34
37
  def detect_from_capistrano
@@ -94,7 +94,8 @@ module ScoutApm
94
94
 
95
95
  def preconditions_met?
96
96
  if dev_trace_disabled?
97
- logger.debug("DevTrace: isn't activated via config. Try: SCOUT_DEV_TRACE=true rails server")
97
+ # The line below is very noise as it is called on every request.
98
+ # logger.debug("DevTrace: isn't activated via config. Try: SCOUT_DEV_TRACE=true rails server")
98
99
  return false
99
100
  end
100
101
 
@@ -31,6 +31,7 @@ module ScoutApm
31
31
  install_instrument(ScoutApm::Instruments::Mongoid)
32
32
  install_instrument(ScoutApm::Instruments::NetHttp)
33
33
  install_instrument(ScoutApm::Instruments::HttpClient)
34
+ install_instrument(ScoutApm::Instruments::Memcached)
34
35
  install_instrument(ScoutApm::Instruments::Redis)
35
36
  install_instrument(ScoutApm::Instruments::InfluxDB)
36
37
  install_instrument(ScoutApm::Instruments::Elasticsearch)
@@ -41,13 +42,19 @@ module ScoutApm
41
42
  logger.warn $!.backtrace
42
43
  end
43
44
 
45
+ # Allows users to skip individual instruments via the config file
46
+ def skip_instrument?(instrument_klass)
47
+ instrument_short_name = instrument_klass.name.split("::").last
48
+ (config.value("disabled_instruments") || []).include?(instrument_short_name)
49
+ end
50
+
44
51
  private
45
52
 
46
53
  def install_instrument(instrument_klass)
47
54
  return if already_installed?(instrument_klass)
48
55
 
49
56
  if skip_instrument?(instrument_klass)
50
- logger.info "Skipping Disabled Instrument: #{instrument_short_name} - To re-enable, change `disabled_instruments` key in scout_apm.yml"
57
+ logger.info "Skipping Disabled Instrument: #{instrument_klass} - To re-enable, change `disabled_instruments` key in scout_apm.yml"
51
58
  return
52
59
  end
53
60
 
@@ -56,12 +63,6 @@ module ScoutApm
56
63
  instance.install
57
64
  end
58
65
 
59
- # Allows users to skip individual instruments via the config file
60
- def skip_instrument?(instrument_klass)
61
- instrument_short_name = instrument_klass.name.split("::").last
62
- (config.value("disabled_instruments") || []).include?(instrument_short_name)
63
- end
64
-
65
66
  def already_installed?(instrument_klass)
66
67
  @installed_instruments.any? do |already_installed_instrument|
67
68
  instrument_klass === already_installed_instrument