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
@@ -1,57 +1,23 @@
1
1
  require 'scout_apm/utils/sql_sanitizer'
2
2
 
3
3
  module ScoutApm
4
- class SqlList
5
- attr_reader :sqls
6
-
7
- def initialize(sql=nil)
8
- @sqls = []
9
-
10
- if !sql.nil?
11
- push(sql)
12
- end
13
- end
14
-
15
- def <<(sql)
16
- push(sql)
17
- end
18
-
19
- def push(sql)
20
- if !(Utils::SqlSanitizer === sql)
21
- sql = Utils::SqlSanitizer.new(sql)
22
- end
23
- @sqls << sql
24
- end
25
-
26
- # All of this one, then all of the other.
27
- def merge(other)
28
- @sqls += other.sqls
29
- end
30
-
31
- def to_s
32
- @sqls.map{|s| s.to_s }.join(";\n")
33
- end
34
- end
35
-
36
4
  module Instruments
37
5
  class ActiveRecord
38
- attr_reader :context
6
+ attr_reader :logger
39
7
 
40
- def initialize(context)
41
- @context = context
8
+ def initalize(logger=ScoutApm::Agent.instance.logger)
9
+ @logger = logger
42
10
  @installed = false
43
11
  end
44
12
 
45
- def logger
46
- context.logger
47
- end
48
-
49
13
  def installed?
50
14
  @installed
51
15
  end
52
16
 
53
17
  def install
54
- if install_via_after_initialize?
18
+ @installed = true
19
+
20
+ if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i == 3 && ::Rails.respond_to?(:configuration)
55
21
  Rails.configuration.after_initialize do
56
22
  add_instruments
57
23
  end
@@ -60,21 +26,9 @@ module ScoutApm
60
26
  end
61
27
  end
62
28
 
63
- # If we have the right version of rails, we should use the hooks provided
64
- # to install these instruments
65
- def install_via_after_initialize?
66
- defined?(::Rails) &&
67
- defined?(::Rails::VERSION) &&
68
- defined?(::Rails::VERSION::MAJOR) &&
69
- ::Rails::VERSION::MAJOR.to_i == 3 &&
70
- ::Rails.respond_to?(:configuration)
71
- end
72
-
73
29
  def add_instruments
74
30
  # Setup Tracer on AR::Base
75
31
  if Utils::KlassHelper.defined?("ActiveRecord::Base")
76
- @installed = true
77
-
78
32
  ::ActiveRecord::Base.class_eval do
79
33
  include ::ScoutApm::Tracer
80
34
  end
@@ -82,34 +36,12 @@ module ScoutApm
82
36
 
83
37
  # Install #log tracing
84
38
  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
94
- end
95
-
96
- if Utils::KlassHelper.defined?("ActiveRecord::Base")
97
- ::ActiveRecord::Base.class_eval do
98
- include ::ScoutApm::Instruments::ActiveRecordUpdateInstruments
39
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
40
+ include ::ScoutApm::Instruments::ActiveRecordInstruments
41
+ include ::ScoutApm::Tracer
99
42
  end
100
43
  end
101
44
 
102
- # Disabled until we can determine how to use Module#prepend in the
103
- # agent. Otherwise, this will cause infinite loops if NewRelic is
104
- # installed. We can't just use normal Module#include, since the
105
- # original methods don't call super the way Base#save does
106
- #
107
- #if Utils::KlassHelper.defined?("ActiveRecord::Relation")
108
- # ::ActiveRecord::Relation.class_eval do
109
- # include ::ScoutApm::Instruments::ActiveRecordRelationInstruments
110
- # end
111
- #end
112
-
113
45
  if Utils::KlassHelper.defined?("ActiveRecord::Querying")
114
46
  ::ActiveRecord::Querying.module_eval do
115
47
  include ::ScoutApm::Tracer
@@ -117,28 +49,10 @@ module ScoutApm
117
49
  end
118
50
  end
119
51
 
120
- rails_3_2_or_above = defined?(::ActiveRecord::VERSION::MAJOR) &&
121
- defined?(::ActiveRecord::VERSION::MINOR) &&
122
- (::ActiveRecord::VERSION::MAJOR.to_i > 3 ||
123
- (::ActiveRecord::VERSION::MAJOR.to_i == 3 && ::ActiveRecord::VERSION::MINOR.to_i >= 2))
124
- if rails_3_2_or_above
125
- if Utils::KlassHelper.defined?("ActiveRecord::Relation")
126
- if @context.environment.supports_module_prepend?
127
- ::ActiveRecord::Relation.module_eval do
128
- prepend ::ScoutApm::Instruments::ActiveRecordRelationQueryInstruments
129
- end
130
- else
131
- ::ActiveRecord::Relation.module_eval do
132
- include ::ScoutApm::Instruments::ActiveRecordRelationQueryInstruments
133
- end
134
- end
135
- end
136
- else
137
- if Utils::KlassHelper.defined?("ActiveRecord::FinderMethods")
138
- ::ActiveRecord::FinderMethods.module_eval do
139
- include ::ScoutApm::Tracer
140
- include ::ScoutApm::Instruments::ActiveRecordFinderMethodsInstruments
141
- end
52
+ if Utils::KlassHelper.defined?("ActiveRecord::FinderMethods")
53
+ ::ActiveRecord::FinderMethods.module_eval do
54
+ include ::ScoutApm::Tracer
55
+ include ::ScoutApm::Instruments::ActiveRecordFinderMethodsInstruments
142
56
  end
143
57
  end
144
58
 
@@ -147,19 +61,14 @@ module ScoutApm
147
61
  req = ScoutApm::RequestManager.lookup
148
62
  layer = req.current_layer
149
63
  if layer && layer.type == "ActiveRecord"
150
- layer.annotate_layer({
151
- :class_name => payload[:class_name],
152
- :record_count => payload[:record_count]
153
- })
154
- elsif layer
155
- logger.debug("Expected layer type: ActiveRecord, got #{layer && layer.type}")
64
+ layer.annotate_layer(payload)
156
65
  else
157
- # noop, no layer at all. We're probably ignoring this req.
66
+ ScoutApm::Agent.instance.logger.debug("Expected layer type: ActiveRecord, got #{layer && layer.type}")
158
67
  end
159
68
  end
160
69
  end
161
70
  rescue
162
- logger.warn "ActiveRecord instrumentation exception: #{$!.message}"
71
+ ScoutApm::Agent.instance.logger.warn "ActiveRecord instrumentation exception: #{$!.message}"
163
72
  end
164
73
  end
165
74
 
@@ -172,11 +81,9 @@ module ScoutApm
172
81
  # to the real SQL, and an AR generated "name" for the Query
173
82
  #
174
83
  ################################################################################
175
- #
176
- # Note, if you change this instrumentation, you also need to change ActiveRecordInstruments.
177
- module ActiveRecordAliasMethodInstruments
84
+ module ActiveRecordInstruments
178
85
  def self.included(instrumented_class)
179
- ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
86
+ ScoutApm::Agent.instance.logger.info "Instrumenting #{instrumented_class.inspect}"
180
87
  instrumented_class.class_eval do
181
88
  unless instrumented_class.method_defined?(:log_without_scout_instruments)
182
89
  alias_method :log_without_scout_instruments, :log
@@ -189,63 +96,12 @@ module ScoutApm
189
96
  # Extract data from the arguments
190
97
  sql, name = args
191
98
  metric_name = Utils::ActiveRecordMetricName.new(sql, name)
192
- desc = SqlList.new(sql)
99
+ desc = Utils::SqlSanitizer.new(sql)
193
100
 
194
101
  # Get current ScoutApm context
195
102
  req = ScoutApm::RequestManager.lookup
196
103
  current_layer = req.current_layer
197
104
 
198
- # If we call #log, we have a real query to run, and we've already
199
- # gotten through the cache gatekeeper. Since we want to only trace real
200
- # queries, and not repeated identical queries that just hit cache, we
201
- # mark layer as ignorable initially in #find_by_sql, then only when we
202
- # know it's a real database call do we mark it back as usable.
203
- #
204
- # This flag is later used in SlowRequestConverter to skip adding ignorable layers
205
- current_layer.annotate_layer(:ignorable => false) if current_layer
206
-
207
- # Either: update the current layer and yield, don't start a new one.
208
- if current_layer && current_layer.type == "ActiveRecord"
209
- # TODO: Get rid of call .to_s, need to find this without forcing a previous run of the name logic
210
- if current_layer.name.to_s == Utils::ActiveRecordMetricName::DEFAULT_METRIC
211
- current_layer.name = metric_name
212
- end
213
-
214
- if current_layer.desc.nil?
215
- current_layer.desc = SqlList.new
216
- end
217
- current_layer.desc.merge(desc)
218
-
219
- log_without_scout_instruments(*args, &block)
220
-
221
- # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack.
222
- else
223
- layer = ScoutApm::Layer.new("ActiveRecord", metric_name)
224
- layer.desc = desc
225
- req.start_layer(layer)
226
- begin
227
- log_without_scout_instruments(*args, &block)
228
- ensure
229
- req.stop_layer
230
- end
231
- end
232
- end
233
- end
234
-
235
- module ActiveRecordInstruments
236
- def self.prepended(instrumented_class)
237
- ScoutApm::Agent.instance.context.logger.info "Instrumenting #{instrumented_class.inspect}"
238
- end
239
-
240
- def log(*args, &block)
241
- # Extract data from the arguments
242
- sql, name = args
243
- metric_name = Utils::ActiveRecordMetricName.new(sql, name)
244
- desc = SqlList.new(sql)
245
-
246
- # Get current ScoutApm context
247
- req = ScoutApm::RequestManager.lookup
248
- current_layer = req.current_layer
249
105
 
250
106
  # If we call #log, we have a real query to run, and we've already
251
107
  # gotten through the cache gatekeeper. Since we want to only trace real
@@ -261,14 +117,10 @@ module ScoutApm
261
117
  # TODO: Get rid of call .to_s, need to find this without forcing a previous run of the name logic
262
118
  if current_layer.name.to_s == Utils::ActiveRecordMetricName::DEFAULT_METRIC
263
119
  current_layer.name = metric_name
120
+ current_layer.desc = desc
264
121
  end
265
122
 
266
- if current_layer.desc.nil?
267
- current_layer.desc = SqlList.new
268
- end
269
- current_layer.desc.merge(desc)
270
-
271
- super(*args, &block)
123
+ log_without_scout_instruments(sql, name, &block)
272
124
 
273
125
  # OR: Start a new layer, we didn't pick up instrumentation earlier in the stack.
274
126
  else
@@ -276,7 +128,7 @@ module ScoutApm
276
128
  layer.desc = desc
277
129
  req.start_layer(layer)
278
130
  begin
279
- super(*args, &block)
131
+ log_without_scout_instruments(sql, name, &block)
280
132
  ensure
281
133
  req.stop_layer
282
134
  end
@@ -287,21 +139,12 @@ module ScoutApm
287
139
  ################################################################################
288
140
  # Entry-point of instruments.
289
141
  #
290
- # Instrumentation starts in ActiveRecord::Relation#exec_queries (Rails >=
291
- # 3.2.0) or ActiveRecord::FinderMethods#find_with_assocations (previous
292
- # Rails versions).
293
- #
294
- # ActiveRecord::Querying#find_by_sql is instrumented in all Rails versions
295
- # even though it is also invoked by #exec_queries/#find_by_associations
296
- # because it can be invoked directly from user code (e.g.,
297
- # Post.find_by_sql("SELECT * FROM posts")). The layer started by
298
- # #exec_queries/#find_by_assocations is marked to ignore children, so it
299
- # will not cause duplicate layers in the former case.
142
+ # We instrument both ActiveRecord::Querying#find_by_sql and
143
+ # ActiveRecord::FinderMethods#find_with_associations. These are early in
144
+ # the chain of calls when you're using ActiveRecord.
300
145
  #
301
- # These methods are early in the chain of calls invoked when executing an
302
- # ActiveRecord query, before the cache is consulted. If the query is later
303
- # determined to be a cache miss, `#log` will be invoked, which we also
304
- # instrument, and more details will be filled in (name, sql).
146
+ # Later on, they will call into #log, which we also instrument, at which
147
+ # point, we can fill in additional data gathered at that point (name, sql)
305
148
  #
306
149
  # Caveats:
307
150
  # * We don't have a name for the query yet.
@@ -314,7 +157,7 @@ module ScoutApm
314
157
 
315
158
  module ActiveRecordQueryingInstruments
316
159
  def self.included(instrumented_class)
317
- ScoutApm::Agent.instance.context.logger.info "Instrumenting ActiveRecord::Querying - #{instrumented_class.inspect}"
160
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActiveRecord::Querying - #{instrumented_class.inspect}"
318
161
  instrumented_class.class_eval do
319
162
  unless instrumented_class.method_defined?(:find_by_sql_without_scout_instruments)
320
163
  alias_method :find_by_sql_without_scout_instruments, :find_by_sql
@@ -340,7 +183,7 @@ module ScoutApm
340
183
 
341
184
  module ActiveRecordFinderMethodsInstruments
342
185
  def self.included(instrumented_class)
343
- ScoutApm::Agent.instance.context.logger.info "Instrumenting ActiveRecord::FinderMethods - #{instrumented_class.inspect}"
186
+ ScoutApm::Agent.instance.logger.info "Instrumenting ActiveRecord::FinderMethods - #{instrumented_class.inspect}"
344
187
  instrumented_class.class_eval do
345
188
  unless instrumented_class.method_defined?(:find_with_associations_without_scout_instruments)
346
189
  alias_method :find_with_associations_without_scout_instruments, :find_with_associations
@@ -353,7 +196,6 @@ module ScoutApm
353
196
  req = ScoutApm::RequestManager.lookup
354
197
  layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName::DEFAULT_METRIC)
355
198
  layer.annotate_layer(:ignorable => true)
356
- layer.desc = SqlList.new
357
199
  req.start_layer(layer)
358
200
  req.ignore_children!
359
201
  begin
@@ -364,142 +206,5 @@ module ScoutApm
364
206
  end
365
207
  end
366
208
  end
367
-
368
- module ActiveRecordRelationQueryInstruments
369
- def self.prepended(instrumented_class)
370
- ScoutApm::Agent.instance.context.logger.info "Instrumenting ActiveRecord::Relation#exec_queries - #{instrumented_class.inspect} (prepending)"
371
- end
372
-
373
- def self.included(instrumented_class)
374
- ScoutApm::Agent.instance.context.logger.info "Instrumenting ActiveRecord::Relation#exec_queries - #{instrumented_class.inspect}"
375
- instrumented_class.class_eval do
376
- unless instrumented_class.method_defined?(:exec_queries_without_scout_instruments)
377
- alias_method :exec_queries_without_scout_instruments, :exec_queries
378
- alias_method :exec_queries, :exec_queries_with_scout_instruments
379
- end
380
- end
381
- end
382
-
383
- def exec_queries(*args, &block)
384
- req = ScoutApm::RequestManager.lookup
385
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName::DEFAULT_METRIC)
386
- layer.annotate_layer(:ignorable => true)
387
- layer.desc = SqlList.new
388
- req.start_layer(layer)
389
- req.ignore_children!
390
- begin
391
- if ScoutApm::Environment.instance.supports_module_prepend?
392
- super(*args, &block)
393
- else
394
- exec_queries_without_scout_instruments(*args, &block)
395
- end
396
- ensure
397
- req.acknowledge_children!
398
- req.stop_layer
399
- end
400
- end
401
-
402
- # If prepend is not supported, rename the method and use
403
- # alias_method_style chaining instead
404
- if !ScoutApm::Environment.instance.supports_module_prepend?
405
- alias_method :exec_queries_with_scout_instruments, :exec_queries
406
- remove_method :exec_queries
407
- end
408
- end
409
-
410
- module ActiveRecordUpdateInstruments
411
- def save(*args, &block)
412
- model = self.class.name
413
- operation = self.persisted? ? "Update" : "Create"
414
-
415
- req = ScoutApm::RequestManager.lookup
416
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName.new("", "#{model} #{operation}"))
417
- layer.desc = SqlList.new
418
- req.start_layer(layer)
419
- req.ignore_children!
420
- begin
421
- super(*args, &block)
422
- ensure
423
- req.acknowledge_children!
424
- req.stop_layer
425
- end
426
- end
427
-
428
- def save!(*args, &block)
429
- model = self.class.name
430
- operation = self.persisted? ? "Update" : "Create"
431
-
432
- req = ScoutApm::RequestManager.lookup
433
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName.new("", "#{model} #{operation}"))
434
- req.start_layer(layer)
435
- req.ignore_children!
436
- begin
437
- super(*args, &block)
438
- ensure
439
- req.acknowledge_children!
440
- req.stop_layer
441
- end
442
- end
443
- end
444
-
445
- module ActiveRecordRelationInstruments
446
- def self.included(instrumented_class)
447
- ::ActiveRecord::Relation.class_eval do
448
- alias_method :update_all_without_scout_instruments, :update_all
449
- alias_method :update_all, :update_all_with_scout_instruments
450
-
451
- alias_method :delete_all_without_scout_instruments, :delete_all
452
- alias_method :delete_all, :delete_all_with_scout_instruments
453
-
454
- alias_method :destroy_all_without_scout_instruments, :destroy_all
455
- alias_method :destroy_all, :destroy_all_with_scout_instruments
456
- end
457
- end
458
-
459
- def update_all_with_scout_instruments(*args, &block)
460
- model = self.name
461
-
462
- req = ScoutApm::RequestManager.lookup
463
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName.new("", "#{model} Update"))
464
- req.start_layer(layer)
465
- req.ignore_children!
466
- begin
467
- update_all_without_scout_instruments(*args, &block)
468
- ensure
469
- req.acknowledge_children!
470
- req.stop_layer
471
- end
472
- end
473
-
474
- def delete_all_with_scout_instruments(*args, &block)
475
- model = self.name
476
-
477
- req = ScoutApm::RequestManager.lookup
478
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName.new("", "#{model} Delete"))
479
- req.start_layer(layer)
480
- req.ignore_children!
481
- begin
482
- delete_all_without_scout_instruments(*args, &block)
483
- ensure
484
- req.acknowledge_children!
485
- req.stop_layer
486
- end
487
- end
488
-
489
- def destroy_all_with_scout_instruments(*args, &block)
490
- model = self.name
491
-
492
- req = ScoutApm::RequestManager.lookup
493
- layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName.new("", "#{model} Delete"))
494
- req.start_layer(layer)
495
- req.ignore_children!
496
- begin
497
- destroy_all_without_scout_instruments(*args, &block)
498
- ensure
499
- req.acknowledge_children!
500
- req.stop_layer
501
- end
502
- end
503
- end
504
209
  end
505
210
  end
@@ -0,0 +1,57 @@
1
+ module ScoutApm
2
+ module Instruments
3
+ class DelayedJob
4
+ attr_reader :logger
5
+
6
+ def initialize(logger=ScoutApm::Agent.instance.logger)
7
+ @logger = logger
8
+ @installed = false
9
+ end
10
+
11
+ def installed?
12
+ @installed
13
+ end
14
+
15
+ def install
16
+ @installed = true
17
+ if defined?(::Delayed::Worker)
18
+ ::Delayed::Worker.class_eval do
19
+ include ScoutApm::Tracer
20
+ include ScoutApm::Instruments::DelayedJobInstruments
21
+ alias run_without_scout_instruments run
22
+ alias run run_with_scout_instruments
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ module DelayedJobInstruments
29
+ def run_with_scout_instruments(job)
30
+ scout_method_name = method_from_handler(job.handler)
31
+ queue = job.queue
32
+ latency = (Time.now.to_f - job.created_at.to_f) * 1000
33
+
34
+ ScoutApm::Agent.instance.store.track_one!("Queue", queue, 0, {:extra_metrics => {:latency => latency}})
35
+ req = ScoutApm::RequestManager.lookup
36
+ req.job!
37
+ req.start_layer( ScoutApm::Layer.new("Job", scout_method_name) )
38
+
39
+ begin
40
+ run_without_scout_instruments(job)
41
+ rescue
42
+ req.error!
43
+ raise
44
+ ensure
45
+ req.stop_layer
46
+ end
47
+ end
48
+
49
+ def method_from_handler(handler)
50
+ job_handler = YAML.load(handler)
51
+ klass = job_handler.object.name
52
+ method = job_handler.method_name
53
+ "#{klass}##{method}"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,29 +1,25 @@
1
1
  module ScoutApm
2
2
  module Instruments
3
3
  class Elasticsearch
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
16
+ @installed = true
17
+
20
18
  if defined?(::Elasticsearch) &&
21
19
  defined?(::Elasticsearch::Transport) &&
22
20
  defined?(::Elasticsearch::Transport::Client)
23
21
 
24
- @installed = true
25
-
26
- logger.info "Instrumenting Elasticsearch"
22
+ ScoutApm::Agent.instance.logger.info "Instrumenting Elasticsearch"
27
23
 
28
24
  ::Elasticsearch::Transport::Client.class_eval do
29
25
  include ScoutApm::Tracer
@@ -1,26 +1,22 @@
1
1
  module ScoutApm
2
2
  module Instruments
3
3
  class Grape
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?(::Grape) && defined?(::Grape::Endpoint)
21
- @installed = true
16
+ @installed = true
22
17
 
23
- logger.info "Instrumenting Grape::Endpoint"
18
+ if defined?(::Grape) && defined?(::Grape::Endpoint)
19
+ ScoutApm::Agent.instance.logger.info "Instrumenting Grape::Endpoint"
24
20
 
25
21
  ::Grape::Endpoint.class_eval do
26
22
  include ScoutApm::Instruments::GrapeEndpointInstruments
@@ -37,13 +33,14 @@ module ScoutApm
37
33
  request = ::Grape::Request.new(env || args.first)
38
34
  req = ScoutApm::RequestManager.lookup
39
35
 
40
- path = ScoutApm::Agent.instance.context.config.value("uri_reporting") == 'path' ? request.path : request.fullpath
36
+ path = ScoutApm::Agent.instance.config.value("uri_reporting") == 'path' ? request.path : request.fullpath
41
37
  req.annotate_request(:uri => path)
42
38
 
43
39
  # IP Spoofing Protection can throw an exception, just move on w/o remote ip
44
40
  req.context.add_user(:ip => request.ip) rescue nil
45
41
 
46
42
  req.set_headers(request.headers)
43
+ req.web!
47
44
 
48
45
  begin
49
46
  name = ["Grape",
@@ -53,7 +50,7 @@ module ScoutApm
53
50
  self.options[:path].first,
54
51
  ].compact.map{ |n| n.to_s }.join("/")
55
52
  rescue => e
56
- logger.info("Error getting Grape Endpoint Name. Error: #{e.message}. Options: #{self.options.inspect}")
53
+ ScoutApm::Agent.instance.logger.info("Error getting Grape Endpoint Name. Error: #{e.message}. Options: #{self.options.inspect}")
57
54
  name = "Grape/Unknown"
58
55
  end
59
56
 
@@ -1,37 +1,30 @@
1
1
  module ScoutApm
2
2
  module Instruments
3
3
  class HttpClient
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?(::HTTPClient)
21
- @installed = true
16
+ @installed = true
22
17
 
23
- logger.info "Instrumenting HTTPClient"
18
+ if defined?(::HTTPClient)
19
+ ScoutApm::Agent.instance.logger.info "Instrumenting HTTPClient"
24
20
 
25
21
  ::HTTPClient.class_eval do
26
22
  include ScoutApm::Tracer
27
23
 
28
24
  def request_with_scout_instruments(*args, &block)
29
-
30
25
  method = args[0].to_s
31
26
  url = args[1]
32
-
33
- max_length = ScoutApm::Agent.instance.context.config.value('instrument_http_url_length')
34
- url = url && url.to_s[0..(max_length - 1)]
27
+ url = url && url.to_s[0..99]
35
28
 
36
29
  self.class.instrument("HTTP", method, :desc => url) do
37
30
  request_without_scout_instruments(*args, &block)