scout_apm 2.6.10 → 3.0.0.pre0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -2
  3. data/.rubocop.yml +3 -11
  4. data/CHANGELOG.markdown +4 -362
  5. data/Gemfile +1 -14
  6. data/README.markdown +7 -52
  7. data/Rakefile +1 -0
  8. data/ext/allocations/allocations.c +1 -7
  9. data/ext/allocations/extconf.rb +0 -1
  10. data/ext/rusage/rusage.c +0 -26
  11. data/ext/stacks/extconf.rb +37 -0
  12. data/ext/stacks/scout_atomics.h +86 -0
  13. data/ext/stacks/stacks.c +811 -0
  14. data/lib/scout_apm/agent/logging.rb +69 -0
  15. data/lib/scout_apm/agent/reporting.rb +126 -0
  16. data/lib/scout_apm/agent.rb +259 -138
  17. data/lib/scout_apm/app_server_load.rb +15 -41
  18. data/lib/scout_apm/attribute_arranger.rb +3 -14
  19. data/lib/scout_apm/background_job_integrations/delayed_job.rb +1 -70
  20. data/lib/scout_apm/background_job_integrations/sidekiq.rb +24 -31
  21. data/lib/scout_apm/background_worker.rb +12 -23
  22. data/lib/scout_apm/capacity.rb +57 -0
  23. data/lib/scout_apm/config.rb +37 -206
  24. data/lib/scout_apm/context.rb +4 -20
  25. data/lib/scout_apm/deploy_integrations/capistrano_2.cap +12 -0
  26. data/lib/scout_apm/deploy_integrations/capistrano_2.rb +83 -0
  27. data/lib/scout_apm/deploy_integrations/capistrano_3.cap +12 -0
  28. data/lib/scout_apm/deploy_integrations/capistrano_3.rb +88 -0
  29. data/lib/scout_apm/environment.rb +28 -42
  30. data/lib/scout_apm/fake_store.rb +0 -12
  31. data/lib/scout_apm/framework_integrations/rails_2.rb +1 -2
  32. data/lib/scout_apm/framework_integrations/rails_3_or_4.rb +6 -17
  33. data/lib/scout_apm/framework_integrations/sinatra.rb +1 -1
  34. data/lib/scout_apm/histogram.rb +3 -12
  35. data/lib/scout_apm/instant/assets/xmlhttp_instrumentation.html +2 -2
  36. data/lib/scout_apm/instant/middleware.rb +54 -202
  37. data/lib/scout_apm/instant_reporting.rb +7 -7
  38. data/lib/scout_apm/instruments/.DS_Store +0 -0
  39. data/lib/scout_apm/instruments/action_controller_rails_2.rb +9 -15
  40. data/lib/scout_apm/instruments/action_controller_rails_3_rails4.rb +76 -124
  41. data/lib/scout_apm/instruments/active_record.rb +29 -324
  42. data/lib/scout_apm/instruments/delayed_job.rb +57 -0
  43. data/lib/scout_apm/instruments/elasticsearch.rb +6 -10
  44. data/lib/scout_apm/instruments/grape.rb +9 -12
  45. data/lib/scout_apm/instruments/http_client.rb +7 -14
  46. data/lib/scout_apm/instruments/influxdb.rb +6 -10
  47. data/lib/scout_apm/instruments/middleware_detailed.rb +11 -15
  48. data/lib/scout_apm/instruments/middleware_summary.rb +5 -11
  49. data/lib/scout_apm/instruments/mongoid.rb +8 -39
  50. data/lib/scout_apm/instruments/moped.rb +6 -11
  51. data/lib/scout_apm/instruments/net_http.rb +9 -27
  52. data/lib/scout_apm/instruments/percentile_sampler.rb +23 -42
  53. data/lib/scout_apm/instruments/process/process_cpu.rb +6 -11
  54. data/lib/scout_apm/instruments/process/process_memory.rb +12 -17
  55. data/lib/scout_apm/instruments/rails_router.rb +6 -12
  56. data/lib/scout_apm/instruments/redis.rb +6 -10
  57. data/lib/scout_apm/instruments/sinatra.rb +4 -5
  58. data/lib/scout_apm/job_record.rb +2 -4
  59. data/lib/scout_apm/layaway.rb +34 -88
  60. data/lib/scout_apm/layaway_file.rb +3 -13
  61. data/lib/scout_apm/layer.rb +60 -25
  62. data/lib/scout_apm/layer_converters/allocation_metric_converter.rb +6 -7
  63. data/lib/scout_apm/layer_converters/converter_base.rb +14 -203
  64. data/lib/scout_apm/layer_converters/depth_first_walker.rb +10 -22
  65. data/lib/scout_apm/layer_converters/error_converter.rb +8 -8
  66. data/lib/scout_apm/layer_converters/job_converter.rb +50 -37
  67. data/lib/scout_apm/layer_converters/metric_converter.rb +19 -18
  68. data/lib/scout_apm/layer_converters/request_queue_time_converter.rb +13 -13
  69. data/lib/scout_apm/layer_converters/slow_job_converter.rb +116 -52
  70. data/lib/scout_apm/layer_converters/slow_request_converter.rb +120 -51
  71. data/lib/scout_apm/metric_meta.rb +5 -0
  72. data/lib/scout_apm/metric_set.rb +1 -9
  73. data/lib/scout_apm/metric_stats.rb +8 -7
  74. data/lib/scout_apm/middleware.rb +9 -7
  75. data/lib/scout_apm/reporter.rb +24 -71
  76. data/lib/scout_apm/request_histograms.rb +0 -12
  77. data/lib/scout_apm/request_manager.rb +7 -5
  78. data/lib/scout_apm/scored_item_set.rb +0 -7
  79. data/lib/scout_apm/serializers/app_server_load_serializer.rb +0 -4
  80. data/lib/scout_apm/serializers/deploy_serializer.rb +16 -0
  81. data/lib/scout_apm/serializers/directive_serializer.rb +0 -4
  82. data/lib/scout_apm/serializers/payload_serializer.rb +4 -11
  83. data/lib/scout_apm/serializers/payload_serializer_to_json.rb +16 -35
  84. data/lib/scout_apm/serializers/slow_jobs_serializer_to_json.rb +1 -2
  85. data/lib/scout_apm/server_integrations/passenger.rb +1 -1
  86. data/lib/scout_apm/server_integrations/puma.rb +2 -5
  87. data/lib/scout_apm/slow_job_policy.rb +13 -25
  88. data/lib/scout_apm/slow_job_record.rb +4 -13
  89. data/lib/scout_apm/slow_request_policy.rb +13 -25
  90. data/lib/scout_apm/slow_transaction.rb +5 -25
  91. data/lib/scout_apm/store.rb +32 -99
  92. data/lib/scout_apm/trace_compactor.rb +312 -0
  93. data/lib/scout_apm/tracer.rb +31 -35
  94. data/lib/scout_apm/tracked_request.rb +95 -262
  95. data/lib/scout_apm/utils/active_record_metric_name.rb +13 -88
  96. data/lib/scout_apm/utils/backtrace_parser.rb +4 -7
  97. data/lib/scout_apm/utils/fake_stacks.rb +87 -0
  98. data/lib/scout_apm/utils/installed_gems.rb +3 -7
  99. data/lib/scout_apm/utils/klass_helper.rb +2 -8
  100. data/lib/scout_apm/utils/null_logger.rb +13 -0
  101. data/lib/scout_apm/utils/sql_sanitizer.rb +5 -16
  102. data/lib/scout_apm/utils/sql_sanitizer_regex.rb +0 -7
  103. data/lib/scout_apm/utils/sql_sanitizer_regex_1_8_7.rb +0 -6
  104. data/lib/scout_apm/utils/unique_id.rb +0 -27
  105. data/lib/scout_apm/version.rb +2 -1
  106. data/lib/scout_apm.rb +25 -84
  107. data/scout_apm.gemspec +3 -17
  108. data/test/test_helper.rb +3 -57
  109. data/test/unit/agent_test.rb +54 -1
  110. data/test/unit/background_job_integrations/sidekiq_test.rb +3 -0
  111. data/test/unit/config_test.rb +12 -25
  112. data/test/unit/context_test.rb +4 -4
  113. data/test/unit/histogram_test.rb +4 -25
  114. data/test/unit/ignored_uris_test.rb +1 -1
  115. data/test/unit/instruments/active_record_instruments_test.rb +5 -0
  116. data/test/unit/layaway_test.rb +2 -62
  117. data/test/unit/serializers/payload_serializer_test.rb +15 -43
  118. data/test/unit/slow_request_policy_test.rb +6 -15
  119. data/test/unit/sql_sanitizer_test.rb +6 -53
  120. data/test/unit/store_test.rb +4 -73
  121. data/test/unit/utils/active_record_metric_name_test.rb +5 -59
  122. data/test/unit/utils/backtrace_parser_test.rb +1 -6
  123. data/tester.rb +53 -0
  124. metadata +28 -229
  125. data/.travis.yml +0 -26
  126. data/Guardfile +0 -43
  127. data/gems/README.md +0 -28
  128. data/gems/octoshark.gemfile +0 -4
  129. data/gems/rails3.gemfile +0 -5
  130. data/gems/rails4.gemfile +0 -4
  131. data/gems/rails5.gemfile +0 -4
  132. data/gems/rails6.gemfile +0 -4
  133. data/lib/scout_apm/agent/exit_handler.rb +0 -65
  134. data/lib/scout_apm/agent/preconditions.rb +0 -81
  135. data/lib/scout_apm/agent_context.rb +0 -261
  136. data/lib/scout_apm/auto_instrument/instruction_sequence.rb +0 -31
  137. data/lib/scout_apm/auto_instrument/layer.rb +0 -23
  138. data/lib/scout_apm/auto_instrument/parser.rb +0 -27
  139. data/lib/scout_apm/auto_instrument/rails.rb +0 -175
  140. data/lib/scout_apm/auto_instrument.rb +0 -5
  141. data/lib/scout_apm/background_job_integrations/legacy_sneakers.rb +0 -55
  142. data/lib/scout_apm/background_job_integrations/que.rb +0 -134
  143. data/lib/scout_apm/background_job_integrations/resque.rb +0 -88
  144. data/lib/scout_apm/background_job_integrations/shoryuken.rb +0 -124
  145. data/lib/scout_apm/background_job_integrations/sneakers.rb +0 -87
  146. data/lib/scout_apm/background_recorder.rb +0 -48
  147. data/lib/scout_apm/db_query_metric_set.rb +0 -97
  148. data/lib/scout_apm/db_query_metric_stats.rb +0 -102
  149. data/lib/scout_apm/debug.rb +0 -37
  150. data/lib/scout_apm/detailed_trace.rb +0 -217
  151. data/lib/scout_apm/error.rb +0 -27
  152. data/lib/scout_apm/error_service/error_buffer.rb +0 -39
  153. data/lib/scout_apm/error_service/error_record.rb +0 -211
  154. data/lib/scout_apm/error_service/ignored_exceptions.rb +0 -66
  155. data/lib/scout_apm/error_service/middleware.rb +0 -32
  156. data/lib/scout_apm/error_service/notifier.rb +0 -33
  157. data/lib/scout_apm/error_service/payload.rb +0 -47
  158. data/lib/scout_apm/error_service/periodic_work.rb +0 -17
  159. data/lib/scout_apm/error_service/railtie.rb +0 -11
  160. data/lib/scout_apm/error_service/sidekiq.rb +0 -80
  161. data/lib/scout_apm/error_service.rb +0 -32
  162. data/lib/scout_apm/extensions/config.rb +0 -87
  163. data/lib/scout_apm/extensions/transaction_callback_payload.rb +0 -74
  164. data/lib/scout_apm/git_revision.rb +0 -59
  165. data/lib/scout_apm/instrument_manager.rb +0 -88
  166. data/lib/scout_apm/instruments/action_view.rb +0 -141
  167. data/lib/scout_apm/instruments/http.rb +0 -48
  168. data/lib/scout_apm/instruments/memcached.rb +0 -43
  169. data/lib/scout_apm/instruments/resque.rb +0 -39
  170. data/lib/scout_apm/instruments/samplers.rb +0 -11
  171. data/lib/scout_apm/layer_children_set.rb +0 -86
  172. data/lib/scout_apm/layer_converters/database_converter.rb +0 -70
  173. data/lib/scout_apm/layer_converters/find_layer_by_type.rb +0 -38
  174. data/lib/scout_apm/layer_converters/histograms.rb +0 -15
  175. data/lib/scout_apm/layer_converters/trace_converter.rb +0 -184
  176. data/lib/scout_apm/limited_layer.rb +0 -126
  177. data/lib/scout_apm/logger.rb +0 -158
  178. data/lib/scout_apm/periodic_work.rb +0 -47
  179. data/lib/scout_apm/rack.rb +0 -26
  180. data/lib/scout_apm/remote/message.rb +0 -27
  181. data/lib/scout_apm/remote/recorder.rb +0 -57
  182. data/lib/scout_apm/remote/router.rb +0 -49
  183. data/lib/scout_apm/remote/server.rb +0 -60
  184. data/lib/scout_apm/reporting.rb +0 -143
  185. data/lib/scout_apm/serializers/db_query_serializer_to_json.rb +0 -15
  186. data/lib/scout_apm/serializers/histograms_serializer_to_json.rb +0 -21
  187. data/lib/scout_apm/synchronous_recorder.rb +0 -30
  188. data/lib/scout_apm/tasks/doctor.rb +0 -75
  189. data/lib/scout_apm/tasks/support.rb +0 -22
  190. data/lib/scout_apm/transaction.rb +0 -13
  191. data/lib/scout_apm/transaction_time_consumed.rb +0 -51
  192. data/lib/scout_apm/utils/gzip_helper.rb +0 -24
  193. data/lib/scout_apm/utils/marshal_logging.rb +0 -90
  194. data/lib/scout_apm/utils/numbers.rb +0 -14
  195. data/lib/scout_apm/utils/scm.rb +0 -14
  196. data/lib/tasks/doctor.rake +0 -11
  197. data/test/tmp/README.md +0 -17
  198. data/test/unit/agent_context_test.rb +0 -15
  199. data/test/unit/auto_instrument/assignments-instrumented.rb +0 -31
  200. data/test/unit/auto_instrument/assignments.rb +0 -31
  201. data/test/unit/auto_instrument/controller-ast.txt +0 -57
  202. data/test/unit/auto_instrument/controller-instrumented.rb +0 -49
  203. data/test/unit/auto_instrument/controller.rb +0 -49
  204. data/test/unit/auto_instrument/rescue_from-instrumented.rb +0 -13
  205. data/test/unit/auto_instrument/rescue_from.rb +0 -13
  206. data/test/unit/auto_instrument_test.rb +0 -54
  207. data/test/unit/db_query_metric_set_test.rb +0 -67
  208. data/test/unit/db_query_metric_stats_test.rb +0 -113
  209. data/test/unit/error_service/error_buffer_test.rb +0 -25
  210. data/test/unit/error_service/ignored_exceptions_test.rb +0 -49
  211. data/test/unit/extensions/periodic_callbacks_test.rb +0 -58
  212. data/test/unit/extensions/transaction_callbacks_test.rb +0 -58
  213. data/test/unit/fake_store_test.rb +0 -10
  214. data/test/unit/git_revision_test.rb +0 -15
  215. data/test/unit/instruments/active_record_test.rb +0 -40
  216. data/test/unit/instruments/net_http_test.rb +0 -27
  217. data/test/unit/instruments/percentile_sampler_test.rb +0 -133
  218. data/test/unit/layer_children_set_test.rb +0 -97
  219. data/test/unit/layer_converters/depth_first_walker_test.rb +0 -70
  220. data/test/unit/layer_converters/metric_converter_test.rb +0 -22
  221. data/test/unit/layer_converters/stubs.rb +0 -33
  222. data/test/unit/limited_layer_test.rb +0 -53
  223. data/test/unit/logger_test.rb +0 -69
  224. data/test/unit/remote/test_message.rb +0 -13
  225. data/test/unit/remote/test_router.rb +0 -33
  226. data/test/unit/remote/test_server.rb +0 -15
  227. data/test/unit/request_histograms_test.rb +0 -17
  228. data/test/unit/tracer_test.rb +0 -76
  229. data/test/unit/tracked_request_test.rb +0 -71
  230. data/test/unit/transaction_test.rb +0 -14
  231. data/test/unit/transaction_time_consumed_test.rb +0 -46
  232. data/test/unit/utils/numbers_test.rb +0 -15
  233. data/test/unit/utils/scm.rb +0 -17
@@ -5,11 +5,6 @@ module ScoutApm
5
5
  class Page
6
6
  def initialize(html)
7
7
  @html = html
8
-
9
- if html.is_a?(Array)
10
- @html = html.inject("") { |memo, str| memo + str }
11
- end
12
-
13
8
  @to_add_to_head = []
14
9
  @to_add_to_body = []
15
10
  end
@@ -50,206 +45,63 @@ module ScoutApm
50
45
  end
51
46
 
52
47
  def call(env)
53
- rack_response = @app.call(env)
54
- begin
55
- DevTraceResponseManipulator.new(env, rack_response).call
56
- rescue Exception => e
57
- # If anything went wrong at all, just bail out and return the unmodified response.
58
- ScoutApm::Agent.instance.context.logger.debug("DevTrace: Raised an exception: #{e.message}, #{e.backtrace}")
59
- rack_response
60
- end
61
- end
62
- end
63
-
64
- class DevTraceResponseManipulator
65
- attr_reader :rack_response
66
- attr_reader :rack_status, :rack_headers, :rack_body
67
- attr_reader :env
68
-
69
- def initialize(env, rack_response)
70
- @env = env
71
- @rack_response = rack_response
72
-
73
- @rack_status = rack_response[0]
74
- @rack_headers = rack_response[1]
75
- @rack_body = rack_response[2]
76
- end
77
-
78
- def call
79
- return rack_response unless preconditions_met?
80
-
81
- if ajax_request?
82
- ScoutApm::Agent.instance.context.logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body. This is either AJAX or JSON. Path=#{path}; ContentType=#{content_type}")
83
- adjust_ajax_header
84
- else
85
- adjust_html_response
86
- end
87
-
88
- rebuild_rack_response
89
- end
90
-
91
- ###########################
92
- # Precondition checking #
93
- ###########################
94
-
95
- def preconditions_met?
96
- if dev_trace_disabled?
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")
99
- return false
100
- end
101
-
102
- # Don't attempt to instrument assets.
103
- # Don't log this case, since it would be very noisy
104
- logger.debug("DevTrace: dev asset ignored") and return false if development_asset?
105
-
106
- # If we didn't have a tracked_request object, or we explicitly ignored
107
- # this request, don't do any work.
108
- logger.debug("DevTrace: no tracked request") and return false if tracked_request.nil? || tracked_request.ignoring_request?
109
-
110
- # If we didn't get a trace, we can't show a trace...
111
- if trace.nil?
112
- logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body, but no trace was found. Path=#{path}; ContentType=#{content_type}")
113
- return false
114
- end
115
-
116
- true
117
- end
118
-
119
- def dev_trace_disabled?
120
- ! ScoutApm::Agent.instance.context.config.value('dev_trace')
121
- end
122
-
123
- ########################
124
- # Response Injection #
125
- ########################
126
-
127
- def rebuild_rack_response
128
- [rack_status, rack_headers, rack_body]
129
- end
130
-
131
- def adjust_ajax_header
132
- rack_headers['X-scoutapminstant'] = payload
133
- end
134
-
135
- def adjust_html_response
136
- case true
137
- when older_rails_response? then adjust_older_rails_response
138
- when newer_rails_response? then adjust_newer_rails_response
139
- when rack_proxy_response? then adjust_rack_proxy_response
48
+ status, headers, response = @app.call(env)
49
+ path, content_type = env['PATH_INFO'], headers['Content-Type']
50
+ if ScoutApm::Agent.instance.config.value('dev_trace')
51
+ if response.respond_to?(:body)
52
+ req = ScoutApm::RequestManager.lookup
53
+ slow_converter = LayerConverters::SlowRequestConverter.new(req)
54
+ trace = slow_converter.call
55
+ if trace
56
+ metadata = {
57
+ :app_root => ScoutApm::Environment.instance.root.to_s,
58
+ :unique_id => env['action_dispatch.request_id'], # note, this is a different unique_id than what "normal" payloads use
59
+ :agent_version => ScoutApm::VERSION,
60
+ :platform => "ruby",
61
+ }
62
+ hash = ScoutApm::Serializers::PayloadSerializerToJson.rearrange_slow_transaction(trace)
63
+ hash.merge!(:metadata => metadata)
64
+ payload = ScoutApm::Serializers::PayloadSerializerToJson.jsonify_hash(hash)
65
+
66
+ if env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || content_type.include?("application/json")
67
+ ScoutApm::Agent.instance.logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body. This is either AJAX or JSON. Path=#{path}; ContentType=#{content_type}")
68
+ # Add the payload as a header if it's an AJAX call or JSON
69
+ headers['X-scoutapminstant'] = payload
70
+ [status, headers, response]
71
+ else
72
+ # otherwise, attempt to add it inline in the page, along with the appropriate JS & CSS. Note, if page doesn't have a head or body,
73
+ #duration = (req.root_layer.total_call_time*1000).to_i
74
+ apm_host=ScoutApm::Agent.instance.config.value("direct_host")
75
+ page = ScoutApm::Instant::Page.new(response.body)
76
+ page.add_to_head(ScoutApm::Instant::Util.read_asset("xmlhttp_instrumentation.html")) # This monkey-patches XMLHttpRequest. It could possibly be part of the main scout_instant.js too. Putting it here so it runs as soon as possible.
77
+ page.add_to_head("<link href='#{apm_host}/instant/scout_instant.css?cachebust=#{Time.now.to_i}' media='all' rel='stylesheet' />")
78
+ page.add_to_body("<script src='#{apm_host}/instant/scout_instant.js?cachebust=#{Time.now.to_i}'></script>")
79
+ page.add_to_body("<script>var scoutInstantPageTrace=#{payload};window.scoutInstant=window.scoutInstant('#{apm_host}', scoutInstantPageTrace)</script>")
80
+
81
+ if response.is_a?(ActionDispatch::Response)
82
+ ScoutApm::Agent.instance.logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body. This appears to be an HTML page and an ActionDispatch::Response. Path=#{path}; ContentType=#{content_type}")
83
+ # preserve the ActionDispatch::Response when applicable
84
+ response.body=[page.res]
85
+ [status, headers, response]
86
+ else
87
+ ScoutApm::Agent.instance.logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body. This appears to be an HTML page but not an ActionDispatch::Response. Path=#{path}; ContentType=#{content_type}")
88
+ # otherwise, just return an array
89
+ [status, headers, [page.res]]
90
+ end
91
+ end
92
+ else
93
+ ScoutApm::Agent.instance.logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body, but no trace was found. Path=#{path}; ContentType=#{content_type}")
94
+ [status, headers, response]
95
+ end
96
+ else
97
+ # don't log anything here - this is the path for all assets served in development, and the log would get noisy
98
+ [status, headers, response]
99
+ end
140
100
  else
141
- # No action taken, we only adjust if we know exactly what we have.
101
+ ScoutApm::Agent.instance.logger.debug("DevTrace: isn't activated via config. Try: SCOUT_DEV_TRACE=true rails server")
102
+ [status, headers, response]
142
103
  end
143
104
  end
144
-
145
- def older_rails_response?
146
- if defined?(ActionDispatch::Response)
147
- return true if rack_body.is_a?(ActionDispatch::Response)
148
- end
149
- end
150
-
151
- def newer_rails_response?
152
- if defined?(ActionDispatch::Response::RackBody)
153
- return true if rack_body.is_a?(ActionDispatch::Response::RackBody)
154
- end
155
- end
156
-
157
- def rack_proxy_response?
158
- rack_body.is_a?(::Rack::BodyProxy)
159
- end
160
-
161
- def adjust_older_rails_response
162
- logger.debug("DevTrace: in middleware, dev_trace is active, and response has a (older) body. This appears to be an HTML page and an ActionDispatch::Response. Path=#{path}; ContentType=#{content_type}")
163
- rack_body.body = [ html_manipulator.res ]
164
- end
165
-
166
- # Preserve the ActionDispatch::Response object we're working with
167
- def adjust_newer_rails_response
168
- logger.debug("DevTrace: in middleware, dev_trace is active, and response has a (newer) body. This appears to be an HTML page and an ActionDispatch::Response. Path=#{path}; ContentType=#{content_type}")
169
- @rack_body = [ html_manipulator.res ]
170
- end
171
-
172
- def adjust_rack_proxy_response
173
- logger.debug("DevTrace: in middleware, dev_trace is active, and response has a body. This appears to be an HTML page and an Rack::BodyProxy. Path=#{path}; ContentType=#{content_type}")
174
- @rack_body = [ html_manipulator.res ]
175
- @rack_headers.delete("Content-Length")
176
- end
177
-
178
- def html_manipulator
179
- @html_manipulator ||=
180
- begin
181
- page = ScoutApm::Instant::Page.new(rack_body.body)
182
-
183
- # This monkey-patches XMLHttpRequest. It could possibly be part of the main scout_instant.js too. Putting it here so it runs as soon as possible.
184
- page.add_to_head(ScoutApm::Instant::Util.read_asset("xmlhttp_instrumentation.html"))
185
-
186
- # Add a link to CSS, then JS
187
- page.add_to_head("<link href='#{apm_host}/instant/scout_instant.css?cachebust=#{Time.now.to_i}' media='all' rel='stylesheet' />")
188
- page.add_to_body("<script src='#{apm_host}/instant/scout_instant.js?cachebust=#{Time.now.to_i}'></script>")
189
- page.add_to_body("<script>var scoutInstantPageTrace=#{payload};window.scoutInstant=window.scoutInstant('#{apm_host}', scoutInstantPageTrace)</script>")
190
-
191
- page
192
- end
193
- end
194
-
195
- def ajax_request?
196
- env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || content_type.include?("application/json")
197
- end
198
-
199
- def development_asset?
200
- !rack_body.respond_to?(:body)
201
- end
202
-
203
- def path
204
- env['PATH_INFO']
205
- end
206
-
207
- def content_type
208
- rack_headers['Content-Type']
209
- end
210
-
211
- ##############################
212
- # APM Helpers & Shorthands #
213
- ##############################
214
-
215
- def logger
216
- ScoutApm::Agent.instance.context.logger
217
- end
218
-
219
- def tracked_request
220
- @tracked_request ||= ScoutApm::RequestManager.lookup
221
- end
222
-
223
- def apm_host
224
- ScoutApm::Agent.instance.context.config.value("direct_host")
225
- end
226
-
227
- def trace
228
- @trace ||=
229
- begin
230
- layer_finder = LayerConverters::FindLayerByType.new(tracked_request)
231
- converter = LayerConverters::SlowRequestConverter.new(ScoutApm::Agent.instance.context, tracked_request, layer_finder, ScoutApm::FakeStore.new)
232
- converter.call
233
- end
234
- end
235
-
236
- def payload
237
- @payload ||=
238
- begin
239
- metadata = {
240
- :app_root => ScoutApm::Agent.instance.context.environment.root.to_s,
241
- :unique_id => env['action_dispatch.request_id'], # note, this is a different unique_id than what "normal" payloads use
242
- :agent_version => ScoutApm::VERSION,
243
- :platform => "ruby",
244
- }
245
-
246
- hash = ScoutApm::Serializers::PayloadSerializerToJson.
247
- rearrange_slow_transaction(trace).
248
- merge!(:metadata => metadata)
249
- ScoutApm::Serializers::PayloadSerializerToJson.jsonify_hash(hash)
250
- end
251
- end
252
-
253
105
  end
254
106
  end
255
107
  end
@@ -15,12 +15,12 @@ module ScoutApm
15
15
  # Serialize that trace. We reuse the PayloadSerializer, but only provide the metadata and traces.
16
16
  # In this case, the traces array will always have just one element.
17
17
  metadata = {
18
- :app_root => ScoutApm::Agent.instance.context.environment.root.to_s,
19
- :unique_id => ScoutApm::Utils::UniqueId.simple,
20
- :agent_version => ScoutApm::VERSION,
21
- :agent_time => Time.now.iso8601,
22
- :agent_pid => Process.pid,
23
- :platform => "ruby",
18
+ :app_root => ScoutApm::Environment.instance.root.to_s,
19
+ :unique_id => ScoutApm::Utils::UniqueId.simple,
20
+ :agent_version => ScoutApm::VERSION,
21
+ :agent_time => Time.now.iso8601,
22
+ :agent_pid => Process.pid,
23
+ :platform => "ruby",
24
24
  }
25
25
 
26
26
  metrics = []
@@ -31,7 +31,7 @@ module ScoutApm
31
31
  payload = ScoutApm::Serializers::PayloadSerializer.serialize(metadata, metrics, traces, jobs, slow_jobs)
32
32
 
33
33
  # Hand it off to the reporter for POST to our servers
34
- reporter = Reporter.new(context, :instant_trace, @instant_key)
34
+ reporter = Reporter.new(:instant_trace, Agent.instance.config, Agent.instance.logger, @instant_key)
35
35
  reporter.report(payload, {'Content-Type' => 'application/json'} )
36
36
  end
37
37
  end
Binary file
@@ -1,31 +1,27 @@
1
1
  module ScoutApm
2
2
  module Instruments
3
3
  class ActionControllerRails2
4
- attr_reader :context
4
+ attr_reader :logger
5
5
 
6
- def initialize(context)
7
- @context = context
6
+ def initalize(logger=ScoutApm::Agent.instance.logger)
7
+ @logger = logger
8
8
  @installed = false
9
9
  end
10
10
 
11
- def logger
12
- context.logger
13
- end
14
-
15
11
  def installed?
16
12
  @installed
17
13
  end
18
14
 
19
15
  def install
20
- if defined?(::ActionController) && defined?(::ActionController::Base)
21
- @installed = true
16
+ @installed = true
22
17
 
18
+ if defined?(::ActionController) && defined?(::ActionController::Base)
23
19
  ::ActionController::Base.class_eval do
24
20
  include ScoutApm::Tracer
25
21
  include ::ScoutApm::Instruments::ActionControllerRails2Instruments
26
22
  end
27
23
 
28
- logger.info "Instrumenting ActionView::Template"
24
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActionView::Template"
29
25
  ::ActionView::Template.class_eval do
30
26
  include ::ScoutApm::Tracer
31
27
  instrument_method :render, :type => "View", :name => '#{path[%r{^(/.*/)?(.*)$},2]}/Rendering', :scope => true
@@ -37,8 +33,7 @@ module ScoutApm
37
33
 
38
34
  module ActionControllerRails2Instruments
39
35
  def self.included(instrumented_class)
40
- # XXX: Don't lookup context by global
41
- ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
36
+ ScoutApm::Agent.instance.logger.info "Instrumenting #{instrumented_class.inspect}"
42
37
  instrumented_class.class_eval do
43
38
  unless instrumented_class.method_defined?(:perform_action_without_scout_instruments)
44
39
  alias_method :perform_action_without_scout_instruments, :perform_action
@@ -54,11 +49,10 @@ module ScoutApm
54
49
  def perform_action_with_scout_instruments(*args, &block)
55
50
  req = ScoutApm::RequestManager.lookup
56
51
  req.annotate_request(:uri => request.path) # for security by-default, we don't use request.fullpath which could reveal filtered params.
57
- if ScoutApm::Agent.instance.context.config.value('collect_remote_ip')
58
- req.context.add_user(:ip => request.remote_ip)
59
- end
52
+ req.context.add_user(:ip => request.remote_ip)
60
53
  req.set_headers(request.headers)
61
54
  req.start_layer( ScoutApm::Layer.new("Controller", "#{controller_path}/#{action_name}") )
55
+ req.web!
62
56
 
63
57
  begin
64
58
  perform_action_without_scout_instruments(*args, &block)
@@ -2,159 +2,111 @@ module ScoutApm
2
2
  module Instruments
3
3
  # instrumentation for Rails 3 and Rails 4 is the same.
4
4
  class ActionControllerRails3Rails4
5
- attr_reader :context
5
+ attr_reader :logger
6
6
 
7
- def initialize(context)
8
- @context = context
7
+ def initalize(logger=ScoutApm::Agent.instance.logger)
8
+ @logger = logger
9
9
  @installed = false
10
10
  end
11
11
 
12
- def logger
13
- context.logger
14
- end
15
-
16
12
  def installed?
17
13
  @installed
18
14
  end
19
15
 
20
- def installed!
16
+ def install
21
17
  @installed = true
22
- end
23
18
 
24
- def install
25
- if !defined?(::ActiveSupport)
26
- return
19
+ # We previously instrumented ActionController::Metal, which missed
20
+ # before and after filter timing. Instrumenting Base includes those
21
+ # filters, at the expense of missing out on controllers that don't use
22
+ # the full Rails stack.
23
+ if defined?(::ActionController) && defined?(::ActionController::Base)
24
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActionController::Base"
25
+ ::ActionController::Base.class_eval do
26
+ # include ScoutApm::Tracer
27
+ include ScoutApm::Instruments::ActionControllerRails3Rails4Instruments
28
+ end
29
+ ScoutApm::Agent.instance.logger.info "Installing ScoutProf profiling" if ScoutApm::Agent.instance.config.value('profile')
27
30
  end
28
31
 
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
32
+ if defined?(::ActionView) && defined?(::ActionView::PartialRenderer)
33
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActionView::PartialRenderer"
34
+ ::ActionView::PartialRenderer.class_eval do
35
+ include ScoutApm::Tracer
31
36
 
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
37
+ instrument_method :render_partial,
38
+ :type => "View",
39
+ :name => '#{@template.virtual_path rescue "Unknown Partial"}/Rendering',
40
+ :scope => true
40
41
 
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.
45
- if defined?(::ActionController::Base)
46
- this.logger.info "Instrumenting ActionController::Base"
47
- ::ActionController::Base.class_eval do
48
- include ScoutApm::Instruments::ActionControllerBaseInstruments
49
- end
42
+ instrument_method :collection_with_template,
43
+ :type => "View",
44
+ :name => '#{@template.virtual_path rescue "Unknown Collection"}/Rendering',
45
+ :scope => true
50
46
  end
51
47
 
52
- if defined?(::ActionController::Metal)
53
- this.logger.info "Instrumenting ActionController::Metal"
54
- ::ActionController::Metal.class_eval do
55
- include ScoutApm::Instruments::ActionControllerMetalInstruments
56
- end
57
- end
58
-
59
- if defined?(::ActionController::API)
60
- this.logger.info "Instrumenting ActionController::Api"
61
- ::ActionController::API.class_eval do
62
- include ScoutApm::Instruments::ActionControllerAPIInstruments
63
- end
48
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActionView::TemplateRenderer"
49
+ ::ActionView::TemplateRenderer.class_eval do
50
+ include ScoutApm::Tracer
51
+ instrument_method :render_template,
52
+ :type => "View",
53
+ :name => '#{args[0].virtual_path rescue "Unknown"}/Rendering',
54
+ :scope => true
64
55
  end
65
56
  end
66
-
67
- ScoutApm::Agent.instance.context.logger.info("Instrumenting ActionController (hook installed)")
68
57
  end
58
+ end
69
59
 
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
75
- def self.build_instrument_module
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
60
+ module ActionControllerRails3Rails4Instruments
61
+ def process_action(*args)
62
+ req = ScoutApm::RequestManager.lookup
63
+ req.annotate_request(:uri => scout_transaction_uri(request))
86
64
 
87
- def process_action(*args)
88
- req = ScoutApm::RequestManager.lookup
89
- current_layer = req.current_layer
90
- agent_context = ScoutApm::Agent.instance.context
91
-
92
- # Check if this this request is to be reported instantly
93
- if instant_key = request.cookies['scoutapminstant']
94
- agent_context.logger.info "Instant trace request with key=#{instant_key} for path=#{path}"
95
- req.instant_key = instant_key
96
- end
97
-
98
- if current_layer && current_layer.type == "Controller"
99
- # Don't start a new layer if ActionController::API or ActionController::Base handled it already.
100
- super
101
- else
102
- begin
103
- uri = scout_transaction_uri
104
- req.annotate_request(:uri => uri)
105
- rescue
106
- end
107
-
108
- # IP Spoofing Protection can throw an exception, just move on w/o remote ip
109
- if agent_context.config.value('collect_remote_ip')
110
- req.context.add_user(:ip => request.remote_ip) rescue nil
111
- end
112
- req.set_headers(request.headers)
113
-
114
- resolved_name = scout_action_name(*args)
115
- req.start_layer( ScoutApm::Layer.new("Controller", "#{controller_path}/#{resolved_name}") )
116
- begin
117
- super
118
- rescue
119
- req.error!
120
- raise
121
- ensure
122
- req.stop_layer
123
- end
124
- end
125
- end
126
- end
127
- end
65
+ # IP Spoofing Protection can throw an exception, just move on w/o remote ip
66
+ req.context.add_user(:ip => request.remote_ip) rescue nil
128
67
 
129
- end
68
+ req.set_headers(request.headers)
130
69
 
131
- module ActionControllerMetalInstruments
132
- include ScoutApm::Instruments::ActionControllerRails3Rails4.build_instrument_module
70
+ # Check if this this request is to be reported instantly
71
+ if instant_key = request.cookies['scoutapminstant']
72
+ Agent.instance.logger.info "Instant trace request with key=#{instant_key} for path=#{path}"
73
+ req.instant_key = instant_key
74
+ end
133
75
 
134
- def scout_action_name(*args)
135
- action_name = args[0]
136
- end
137
- end
76
+ req.web!
138
77
 
139
- # Empty, noop module to provide compatibility w/ previous manual instrumentation
140
- module ActionControllerRails3Rails4Instruments
141
- end
78
+ layer = ScoutApm::Layer.new("Controller", "#{controller_path}/#{action_name}")
142
79
 
143
- module ActionControllerBaseInstruments
144
- include ScoutApm::Instruments::ActionControllerRails3Rails4.build_instrument_module
80
+ if ScoutApm::Agent.instance.config.value('profile')
81
+ # Capture ScoutProf if we can
82
+ req.enable_profiled_thread!
83
+ layer.set_root_class(self.class)
84
+ layer.traced!
85
+ end
145
86
 
146
- def scout_action_name(*args)
147
- action_name
87
+ # Start the layer, then execute the user's code
88
+ req.start_layer(layer)
89
+ begin
90
+ super
91
+ rescue
92
+ req.error!
93
+ raise
94
+ ensure
95
+ req.stop_layer
96
+ end
97
+ end # process_action
98
+
99
+ # Given an +ActionDispatch::Request+, formats the uri based on config settings.
100
+ def scout_transaction_uri(request)
101
+ case ScoutApm::Agent.instance.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
148
107
  end
149
- end
150
108
 
151
- module ActionControllerAPIInstruments
152
- include ScoutApm::Instruments::ActionControllerRails3Rails4.build_instrument_module
153
-
154
- def scout_action_name(*args)
155
- action_name
156
- end
157
- end
109
+ end # ActionControllerRails3Rails4Instruments
158
110
  end
159
111
  end
160
112