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,184 +0,0 @@
1
- module ScoutApm
2
- module LayerConverters
3
- class TraceConverter < ConverterBase
4
- ###################
5
- # Converter API #
6
- ###################
7
-
8
-
9
- def record!
10
- @points = context.slow_request_policy.score(request)
11
-
12
- # Let the store know we're here, and if it wants our data, it will call
13
- # back into #call
14
- @store.track_trace!(self)
15
-
16
- nil # not returning anything in the layer results ... not used
17
- end
18
-
19
- #####################
20
- # ScoreItemSet API #
21
- #####################
22
- def name; request.unique_name; end
23
- def score; @points; end
24
-
25
- # Unconditionally attempts to convert this into a DetailedTrace object.
26
- # Can return nil if the request didn't have any scope_layer or if `timeline_traces` aren't enabled.
27
- def call
28
- return nil unless scope_layer
29
- return nil unless context.config.value('timeline_traces')
30
-
31
- # Since this request is being stored, update the needed counters
32
- context.slow_request_policy.stored!(request)
33
-
34
- # record the change in memory usage
35
- mem_delta = ScoutApm::Instruments::Process::ProcessMemory.new(context).rss_to_mb(@request.capture_mem_delta!)
36
-
37
- transaction_id = request.transaction_id
38
- revision = context.environment.git_revision.sha
39
- start_instant = request.root_layer.start_time
40
- stop_instant = request.root_layer.stop_time
41
- type = if request.web?
42
- "Web"
43
- elsif request.job?
44
- "Job"
45
- else
46
- "Unknown"
47
- end
48
-
49
- # Create request tags
50
- #
51
- tags = {
52
- :allocations => request.root_layer.total_allocations,
53
- :mem_delta => mem_delta,
54
- }.merge(request.context.to_flat_hash)
55
-
56
- host = context.environment.hostname
57
- path = request.annotations[:uri] || ""
58
- code = "" # User#index for instance
59
-
60
- spans = create_spans(request.root_layer)
61
- if limited?
62
- tags[:"scout.reached_span_cap"] = true
63
- end
64
-
65
- DetailedTrace.new(
66
- transaction_id,
67
- revision,
68
- host,
69
- start_instant,
70
- stop_instant,
71
- type,
72
-
73
- path,
74
- code,
75
-
76
- spans,
77
- tags
78
-
79
- # total_score = 0,
80
- # percentile_score = 0,
81
- # age_score = 0,
82
- # memory_delta_score = 0,
83
- # memory_allocations_score = 0
84
- )
85
- end
86
-
87
- # Returns an array of span objects. Uses recursion to get all children
88
- # wired up w/ correct parent_ids
89
- def create_spans(layer, parent_id = nil)
90
- span_id = ScoutApm::Utils::SpanId.new.to_s
91
-
92
- start_instant = layer.start_time
93
- stop_instant = layer.stop_time
94
- operation = layer.legacy_metric_name
95
- tags = {
96
- :start_allocations => layer.allocations_start,
97
- :stop_allocations => layer.allocations_stop,
98
- }
99
- if layer.desc
100
- tags[:desc] = layer.desc.to_s
101
- end
102
- if layer.annotations && layer.annotations[:record_count]
103
- tags["db.record_count"] = layer.annotations[:record_count]
104
- end
105
- if layer.annotations && layer.annotations[:class_name]
106
- tags["db.class_name"] = layer.annotations[:class_name]
107
- end
108
- if layer.backtrace
109
- tags[:backtrace] = backtrace_parser(layer.backtrace) rescue nil
110
- end
111
-
112
- # Collect up self, and all children into result array
113
- result = []
114
- result << DetailedTraceSpan.new(
115
- span_id.to_s,
116
- parent_id.to_s,
117
- start_instant,
118
- stop_instant,
119
- operation,
120
- tags)
121
-
122
- layer.children.each do |child|
123
- # Don't create spans from limited layers. These don't have start/stop times and our processing can't
124
- # handle these yet.
125
- unless over_span_limit?(result) || child.is_a?(LimitedLayer)
126
- result += create_spans(child, span_id)
127
- end
128
- end
129
-
130
- return result
131
- end
132
-
133
- # Take an array of ruby backtrace lines and split it into an array of hashes like:
134
- # ["/Users/cschneid/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/irb/workspace.rb:86:in `eval'", ...]
135
- # turns into:
136
- # [ {
137
- # "file": "app/controllers/users_controller.rb",
138
- # "line": 10,
139
- # "function": "index"
140
- # },
141
- # ]
142
- def backtrace_parser(lines)
143
- bt = ScoutApm::Utils::BacktraceParser.new(lines).call
144
-
145
- bt.map do |line|
146
- match = line.match(/(.*):(\d+):in `(.*)'/)
147
- {
148
- "file" => match[1],
149
- "line" => match[2],
150
- "function" => match[3],
151
- }
152
- end
153
-
154
- end
155
-
156
- ################################################################################
157
- # Limit Handling
158
- ################################################################################
159
-
160
- # To prevent huge traces from being generated, we stop collecting
161
- # spans as we go beyond some reasonably large count.
162
- MAX_SPANS = 1500
163
-
164
- def over_span_limit?(spans)
165
- if spans.size > MAX_SPANS
166
- log_over_span_limit
167
- @limited = true
168
- else
169
- false
170
- end
171
- end
172
-
173
- def log_over_span_limit
174
- unless limited?
175
- context.logger.debug "Not recording additional spans for #{name}. Over the span limit."
176
- end
177
- end
178
-
179
- def limited?
180
- !! @limited
181
- end
182
- end
183
- end
184
- end
@@ -1,126 +0,0 @@
1
- module ScoutApm
2
- # A LimitedLayer is a lossy-compression approach to fall back on once we max out
3
- # the number of detailed layer objects we store. See LayerChildrenSet for the
4
- # logic on when that change over happens
5
- #
6
- # QUESTION: What do we do if we attempt to merge an item that has children?
7
- class LimitedLayer
8
- attr_reader :type
9
-
10
- def initialize(type)
11
- @type = type
12
-
13
- @total_call_time = 0
14
- @total_exclusive_time = 0
15
- @total_allocations = 0
16
- @total_exclusive_allocations = 0
17
- @total_layers = 0
18
- end
19
-
20
- def absorb(layer)
21
- @total_layers += 1
22
-
23
- @total_call_time += layer.total_call_time
24
- @total_exclusive_time += layer.total_exclusive_time
25
-
26
- @total_allocations += layer.total_allocations
27
- @total_exclusive_allocations += layer.total_exclusive_allocations
28
- end
29
-
30
- def total_call_time
31
- @total_call_time
32
- end
33
-
34
- def total_exclusive_time
35
- @total_exclusive_time
36
- end
37
-
38
- def total_allocations
39
- @total_allocations
40
- end
41
-
42
- def total_exclusive_allocations
43
- @total_exclusive_allocations
44
- end
45
-
46
- def count
47
- @total_layers
48
- end
49
-
50
- # This is the old style name. This function is used for now, but should be
51
- # removed, and the new type & name split should be enforced through the
52
- # app.
53
- def legacy_metric_name
54
- "#{type}/Limited"
55
- end
56
-
57
- def children
58
- Set.new
59
- end
60
-
61
- def annotations
62
- nil
63
- end
64
-
65
- def to_s
66
- "<LimitedLayer type=#{type} count=#{count}>"
67
- end
68
-
69
- def limited?
70
- true
71
- end
72
-
73
- ######################################################
74
- # Stub out some methods with static default values #
75
- ######################################################
76
- def subscopable?
77
- false
78
- end
79
-
80
- def desc
81
- nil
82
- end
83
-
84
- def backtrace
85
- nil
86
- end
87
-
88
-
89
- #######################################################################
90
- # Many methods don't make any sense on a limited layer. Raise errors #
91
- # aggressively for now to detect mistaken calls #
92
- #######################################################################
93
-
94
- def add_child
95
- raise "Should never call add_child on a limited_layer"
96
- end
97
-
98
- def record_stop_time!(*)
99
- raise "Should never call record_stop_time! on a limited_layer"
100
- end
101
-
102
- def record_allocations!
103
- raise "Should never call record_allocations! on a limited_layer"
104
- end
105
-
106
- def desc=(*)
107
- raise "Should never call desc on a limited_layer"
108
- end
109
-
110
- def annotate_layer(*)
111
- raise "Should never call annotate_layer on a limited_layer"
112
- end
113
-
114
- def subscopable!
115
- raise "Should never call subscopable! on a limited_layer"
116
- end
117
-
118
- def capture_backtrace!
119
- raise "Should never call capture_backtrace on a limited_layer"
120
- end
121
-
122
- def caller_array
123
- raise "Should never call caller_array on a limited_layer"
124
- end
125
- end
126
- end
@@ -1,158 +0,0 @@
1
- # Pass in the "root" of the application you're using.
2
- # - Rails.root
3
- # - `Dir.pwd`
4
- #
5
- # Currently Valid opts:
6
- # :force => Boolean - used to reinitialize logger.
7
- # :log_file_path => String - explicitly set what file to send the log to
8
- # :stdout => true - explicitly force the log to write to stdout (if set, ignore log_file_path)
9
- # :stderr => true - explicitly force the log to write to stderr (if set, ignore log_file_path)
10
- # :logger_class => Class or String - a class to use as the underlying logger. Defaults to Ruby's Logger. See notes
11
- # :log_level => symbol, string, or integer - defaults to INFO level
12
- #
13
- # The :logger_class option
14
- # - allows any class to be used as the underlying logger. Currently requires to respond to:
15
- # - debug, info, warn, error, fatal in both string and block form.
16
- # - debug?, info?, warn?, error?, fatal?
17
- # - #level= with a number (0 = debug, 1 = info, 2= warn, 3=error, 4=fatal)
18
- # - #formatter= that takes a Ruby Logger::Formatter class. This method must be here, but the value may be ignored
19
- #
20
- #config.value('log_level').downcase
21
- #
22
- module ScoutApm
23
- class Logger
24
- attr_reader :log_destination
25
-
26
- def initialize(environment_root, opts={})
27
- @opts = opts
28
- @environment_root = environment_root
29
-
30
- @log_destination = determine_log_destination
31
- @logger = build_logger
32
- self.log_level = log_level_from_opts
33
- @logger.formatter = build_formatter
34
- end
35
-
36
- # Delegate calls to the underlying logger
37
- def debug(*args, &block); @logger.debug(*args, &block); end
38
- def info(*args, &block); @logger.info(*args, &block); end
39
- def warn(*args, &block); @logger.warn(*args, &block); end
40
- def error(*args, &block); @logger.error(*args, &block); end
41
- def fatal(*args, &block); @logger.fatal(*args, &block); end
42
-
43
- def debug?; @logger.debug?; end
44
- def info?; @logger.info?; end
45
- def warn?; @logger.warn?; end
46
- def error?; @logger.error?; end
47
- def fatal?; @logger.fatal?; end
48
-
49
- def log_level=(level)
50
- @logger.level = log_level_from_opts(level)
51
- end
52
-
53
- def log_level
54
- @logger.level
55
- end
56
-
57
- def log_file_path
58
- @opts.fetch(:log_file_path, "#{@environment_root}/log") || "#{@environment_root}/log"
59
- end
60
-
61
- def stdout?
62
- @opts[:stdout] || (@opts[:log_file_path] || "").upcase == "STDOUT"
63
- end
64
-
65
- def stderr?
66
- @opts[:stderr] || (@opts[:log_file_path] || "").upcase == "STDERR"
67
- end
68
-
69
- private
70
-
71
- def build_logger
72
- logger_class.new(@log_destination)
73
- end
74
-
75
- def logger_class
76
- klass = @opts.fetch(:logger_class, ::Logger)
77
- case klass
78
- when String
79
- result = KlassHelper.lookup(klass)
80
- if result == :missing_class
81
- ::Logger
82
- else
83
- result
84
- end
85
- when Class
86
- klass
87
- else
88
- ::Logger
89
- end
90
- end
91
-
92
- def build_formatter
93
- if stdout? || stderr?
94
- TaggedFormatter.new
95
- else
96
- DefaultFormatter.new
97
- end
98
- end
99
-
100
- def log_level_from_opts(explicit=nil)
101
- candidate = explicit || (@opts[:log_level] || "").downcase
102
-
103
- case candidate
104
- when "debug" then ::Logger::DEBUG
105
- when "info" then ::Logger::INFO
106
- when "warn" then ::Logger::WARN
107
- when "error" then ::Logger::ERROR
108
- when "fatal" then ::Logger::FATAL
109
- when ::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR, ::Logger::FATAL then candidate
110
- else ::Logger::INFO
111
- end
112
- end
113
-
114
- def determine_log_destination
115
- case true
116
- when stdout?
117
- STDOUT
118
- when stderr?
119
- STDERR
120
- when validate_path(@opts[:log_file])
121
- @opts[:log_file]
122
- when validate_path("#{log_file_path}/scout_apm.log")
123
- "#{log_file_path}/scout_apm.log"
124
- else
125
- # Safe fallback
126
- STDOUT
127
- end
128
- end
129
-
130
- # Check if this path is ok for a log file.
131
- # Does it exist?
132
- # Is it writable?
133
- def validate_path(candidate)
134
- return false if candidate.nil?
135
-
136
- directory = File.dirname(candidate)
137
- File.writable?(directory)
138
- end
139
-
140
- class DefaultFormatter < ::Logger::Formatter
141
- def call(severity, time, progname, msg)
142
- # since STDOUT isn't exclusive like the scout_apm.log file, apply a prefix.
143
- # XXX: Pass in context to the formatter
144
- "[#{Utils::Time.to_s(time)} #{ScoutApm::Agent.instance.context.environment.hostname} (#{$$})] #{severity} : #{msg}\n"
145
- end
146
- end
147
-
148
- # since STDOUT & STDERR isn't only used for ScoutApm logging, apply a
149
- # prefix to make it easily greppable
150
- class TaggedFormatter < DefaultFormatter
151
- TAG = "[Scout] "
152
-
153
- def call(severity, time, progname, msg)
154
- TAG + super
155
- end
156
- end
157
- end
158
- end
@@ -1,47 +0,0 @@
1
- module ScoutApm
2
- class PeriodicWork
3
- attr_reader :context
4
-
5
- def initialize(context)
6
- @context = context
7
- @reporting = ScoutApm::Reporting.new(context)
8
- end
9
-
10
- # Expected to be called many times over the life of the agent
11
- def run
12
- ScoutApm::Debug.instance.call_periodic_hooks
13
- @reporting.process_metrics
14
- clean_old_percentiles
15
-
16
- if context.config.value('auto_instruments')
17
- log_autoinstrument_significant_counts rescue nil
18
- end
19
- end
20
-
21
- private
22
-
23
- def log_autoinstrument_significant_counts
24
- # Ex key/value -
25
- # "/Users/dlite/projects/scout/apm/app/controllers/application_controller.rb"=>[[0.0, 689], [1.0, 16]]
26
- hists = context.auto_instruments_layer_histograms.as_json
27
- hists_summary = hists.map { |file, buckets|
28
- total = buckets.map(&:last).inject(0) { |sum, count| sum + count }
29
- significant = (buckets.last.last / total.to_f).round(2)
30
- [
31
- file,
32
- {:total => total, :significant => significant}
33
- ]
34
- }.to_h
35
- context.logger.debug("AutoInstrument Significant Layer Histograms: #{hists_summary.pretty_inspect}")
36
- end
37
-
38
- # XXX: Move logic into a RequestHistogramsByTime class that can keep the timeout logic in it
39
- def clean_old_percentiles
40
- context.
41
- request_histograms_by_time.
42
- keys.
43
- select {|timestamp| timestamp.age_in_seconds > 60 * 10 }.
44
- each {|old_timestamp| context.request_histograms_by_time.delete(old_timestamp) }
45
- end
46
- end
47
- end
@@ -1,26 +0,0 @@
1
- module ScoutApm
2
- module Rack
3
- def self.install!
4
- ScoutApm::Agent.instance.start(:skip_app_server_check => true)
5
- ScoutApm::Agent.instance.start_background_worker
6
- end
7
-
8
- def self.transaction(endpoint_name, env)
9
- req = ScoutApm::RequestManager.lookup
10
- req.annotate_request(:uri => env["REQUEST_PATH"]) rescue nil
11
- req.context.add_user(:ip => env["REMOTE_ADDR"]) rescue nil
12
-
13
- layer = ScoutApm::Layer.new('Controller', endpoint_name)
14
- req.start_layer(layer)
15
-
16
- begin
17
- yield
18
- rescue
19
- req.error!
20
- raise
21
- ensure
22
- req.stop_layer
23
- end
24
- end
25
- end
26
- end
@@ -1,27 +0,0 @@
1
- module ScoutApm
2
- module Remote
3
- class Message
4
- attr_reader :type
5
- attr_reader :command
6
- attr_reader :args
7
-
8
- def initialize(type, command, *args)
9
- @type = type
10
- @command = command
11
- @args = args
12
- end
13
-
14
- def self.decode(msg)
15
- Marshal.load(msg)
16
- end
17
-
18
- def encode
19
- Marshal.dump(self)
20
- rescue
21
- ScoutApm::Agent.instance.logger.info("Failed Marshalling Remote::Message")
22
- ScoutApm::Agent.instance.logger.info(ScoutApm::Utils::MarshalLogging.new(self).dive) rescue nil
23
- raise
24
- end
25
- end
26
- end
27
- end
@@ -1,57 +0,0 @@
1
- module ScoutApm
2
- module Remote
3
- class Recorder
4
- attr_reader :logger
5
- attr_reader :remote_agent_host
6
- attr_reader :remote_agent_port
7
-
8
- def initialize(remote_agent_host, remote_agent_port, logger)
9
- @remote_agent_host = remote_agent_host
10
- @remote_agent_port = remote_agent_port
11
- @logger = logger
12
- end
13
-
14
- def start
15
- # nothing to do
16
- self
17
- end
18
-
19
- def stop
20
- # nothing to do
21
- end
22
-
23
- def record!(request)
24
- begin
25
- t1 = Time.now
26
- # Mark this request as recorded, so the next lookup on this thread, it
27
- # can be recreated
28
- request.recorded!
29
-
30
- # Only send requests that we actually want. Incidental http &
31
- # background thread stuff can just be dropped
32
- unless request.job? || request.web?
33
- return
34
- end
35
-
36
- request.prepare_to_dump!
37
- message = ScoutApm::Remote::Message.new('record', 'record!', request)
38
- encoded = message.encode
39
- logger.debug "Remote Agent: Posting a message of length: #{encoded.length}"
40
- post(encoded)
41
- t2 = Time.now
42
-
43
- logger.debug("Remote Recording took: #{t2.to_f - t1.to_f} seconds")
44
- rescue => e
45
- logger.debug "Remote: Error while sending to collector: #{e.inspect}, #{e.backtrace.join("\n")}"
46
- end
47
- end
48
-
49
- def post(encoded_message)
50
- http = Net::HTTP.new(remote_agent_host, remote_agent_port)
51
- request = Net::HTTP::Post.new("/users")
52
- request.body = encoded_message
53
- response = http.request(request)
54
- end
55
- end
56
- end
57
- end
@@ -1,49 +0,0 @@
1
- module ScoutApm
2
- module Remote
3
- class Router
4
- attr_reader :logger
5
- attr_reader :routes
6
-
7
- # If/When we add different types, this signature should change to a hash
8
- # of {type => Object}, rather than building it in the initializer here.
9
- #
10
- # Keys of routes should be strings
11
- def initialize(recorder, logger)
12
- @routes = {
13
- 'record' => recorder
14
- }
15
-
16
- @logger = logger
17
- end
18
-
19
- # A message is a 2 element array [:type, :command, [args]].
20
- # For this first creation, this should be ['record', 'record', [TrackedRequest]] (the args arg should always be an array, even w/ only 1 item)
21
- #
22
- # Where
23
- # type: ['recorder']
24
- # command: any function supported on that type of object
25
- # args: any array of arguments
26
- #
27
- # Raises on unknown message
28
- #
29
- # Returns whatever the recipient object returns
30
- def handle(msg)
31
- message = Remote::Message.decode(msg)
32
- assert_type(message)
33
- call_route(message)
34
- end
35
-
36
- private
37
-
38
- def assert_type(message)
39
- if ! routes.keys.include?(message.type.to_s)
40
- raise "Unknown type: #{message.type.to_s}"
41
- end
42
- end
43
-
44
- def call_route(message)
45
- routes[message.type].send(message.command, *message.args)
46
- end
47
- end
48
- end
49
- end