Class: ScoutApm::TrackedRequest
- Inherits:
-
Object
- Object
- ScoutApm::TrackedRequest
- Defined in:
- lib/scout_apm/tracked_request.rb
Constant Summary collapse
- REQUEST_TYPES =
When we see these layers, it means a real request is going through the system. We toggle a flag to turn on some slightly more expensive instrumentation (backtrace collection and the like) that would be too expensive in situations where the framework is constantly churning. We see that on Sidekiq.
["Controller", "Job"]
- BACKTRACE_BLACKLIST =
["Controller", "Job"]
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
As we go through a request, instrumentation can mark more general data into the Request Known Keys: :uri - the full URI requested by the user :queue_latency - how long a background Job spent in the queue before starting processing.
-
#call_counts ⇒ Object
This maintains a lookup hash of Layer names and call counts.
-
#context ⇒ Object
readonly
Context is application defined extra information.
-
#headers ⇒ Object
readonly
Headers as recorded by rails Can be nil if we never reach a Rails Controller.
-
#instant_key ⇒ Object
if there’s an instant_key, pass the transaction trace on for immediate reporting (in addition to the usual background aggregation) this is set in the controller instumentation (ActionControllerRails3Rails4 according).
-
#recorder ⇒ Object
readonly
An object that responds to ‘record!(TrackedRequest)` to store this tracked request.
-
#root_layer ⇒ Object
readonly
The first layer registered with this request.
Instance Method Summary collapse
- #acknowledge_children! ⇒ Object
-
#annotate_request(hsh) ⇒ Object
As we learn things about this request, we can add data here.
-
#backtrace_threshold ⇒ Object
Grab backtraces more aggressively when running in dev trace mode.
- #capture_backtrace?(layer) ⇒ Boolean
- #capture_mem_delta! ⇒ Object
-
#current_layer ⇒ Object
Grab the currently running layer.
-
#ensure_background_worker ⇒ Object
Ensure the background worker thread is up & running - a fallback if other detection doesn’t achieve this at boot.
-
#error! ⇒ Object
This request had an exception.
- #error? ⇒ Boolean
-
#finalized? ⇒ Boolean
Are we finished with this request? We’re done if we have no layers left after popping one off.
-
#ignore_children! ⇒ Object
Enable this when you would otherwise double track something interesting.
-
#ignore_request! ⇒ Object
At any point in the request, calling code or instrumentation can call ‘ignore_request!` to immediately stop recording any information about new layers, and delete any existing layer info.
- #ignoring_children? ⇒ Boolean
- #ignoring_recorded? ⇒ Boolean
- #ignoring_request? ⇒ Boolean
- #ignoring_start_layer ⇒ Object
- #ignoring_stop_layer ⇒ Object
-
#initialize(agent_context, store) ⇒ TrackedRequest
constructor
A new instance of TrackedRequest.
- #instant? ⇒ Boolean
-
#job? ⇒ Boolean
This request is a job transaction iff it has a ‘Job’ layer Use this only during recording.
- #layer_finder ⇒ Object
- #logger ⇒ Object
-
#mem_usage ⇒ Object
This may be in bytes or KB based on the OSX.
-
#prepare_to_dump! ⇒ Object
Actually go fetch & make-real any lazily created data.
- #real_request! ⇒ Object
-
#real_request? ⇒ Boolean
Have we seen a “controller” or “job” layer so far?.
-
#record! ⇒ Object
Convert this request to the appropriate structure, then report it into the peristent Store object.
-
#recorded! ⇒ Object
Persist the Request.
-
#recorded? ⇒ Boolean
Have we already persisted this request? Used to know when we should just create a new one (don’t attempt to add data to an already-recorded request).
-
#restore_from_dump! ⇒ Object
Go re-fetch the store based on what the Agent’s official one is.
- #set_headers(headers) ⇒ Object
- #start_layer(layer) ⇒ Object
-
#start_request(layer) ⇒ Object
Run at the beginning of the whole request.
- #stop_layer ⇒ Object
-
#stop_request ⇒ Object
Run at the end of the whole request.
- #stopping? ⇒ Boolean
-
#unique_name ⇒ Object
Only call this after the request is complete.
-
#update_call_counts!(layer) ⇒ Object
Maintains a lookup Hash of call counts by layer name.
-
#web? ⇒ Boolean
This request is a web transaction iff it has a ‘Controller’ layer Use this only during recording.
Constructor Details
#initialize(agent_context, store) ⇒ TrackedRequest
Returns a new instance of TrackedRequest.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/scout_apm/tracked_request.rb', line 48 def initialize(agent_context, store) @agent_context = agent_context @store = store #this is passed in so we can use a real store (normal operation) or fake store (instant mode only) @layers = [] @call_set = Hash.new { |h, k| h[k] = CallSet.new } @annotations = {} @ignoring_children = 0 @context = Context.new(agent_context) @root_layer = nil @error = false @stopping = false @instant_key = nil @mem_start = mem_usage @recorder = agent_context.recorder @real_request = false ignore_request! if @recorder.nil? end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
As we go through a request, instrumentation can mark more general data into the Request Known Keys:
:uri - the full URI requested by the user
:queue_latency - how long a background Job spent in the queue before starting processing
23 24 25 |
# File 'lib/scout_apm/tracked_request.rb', line 23 def annotations @annotations end |
#call_counts ⇒ Object
This maintains a lookup hash of Layer names and call counts. It’s used to trigger fetching a backtrace on n+1 calls. Note that layer names might not be Strings - can alse be Utils::ActiveRecordMetricName. Also, this would fail for layers with same names across multiple types.
32 33 34 |
# File 'lib/scout_apm/tracked_request.rb', line 32 def call_counts @call_counts end |
#context ⇒ Object (readonly)
Context is application defined extra information. (ie, which user, what is their email/ip, what plan are they on, what locale are they using, etc) See documentation for examples on how to set this from a before_filter
13 14 15 |
# File 'lib/scout_apm/tracked_request.rb', line 13 def context @context end |
#headers ⇒ Object (readonly)
Headers as recorded by rails Can be nil if we never reach a Rails Controller
27 28 29 |
# File 'lib/scout_apm/tracked_request.rb', line 27 def headers @headers end |
#instant_key ⇒ Object
if there’s an instant_key, pass the transaction trace on for immediate reporting (in addition to the usual background aggregation) this is set in the controller instumentation (ActionControllerRails3Rails4 according)
36 37 38 |
# File 'lib/scout_apm/tracked_request.rb', line 36 def instant_key @instant_key end |
#recorder ⇒ Object (readonly)
An object that responds to ‘record!(TrackedRequest)` to store this tracked request
39 40 41 |
# File 'lib/scout_apm/tracked_request.rb', line 39 def recorder @recorder end |
#root_layer ⇒ Object (readonly)
The first layer registered with this request. All other layers will be children of this layer.
17 18 19 |
# File 'lib/scout_apm/tracked_request.rb', line 17 def root_layer @root_layer end |
Instance Method Details
#acknowledge_children! ⇒ Object
385 386 387 388 389 |
# File 'lib/scout_apm/tracked_request.rb', line 385 def acknowledge_children! if @ignoring_children > 0 @ignoring_children -= 1 end end |
#annotate_request(hsh) ⇒ Object
As we learn things about this request, we can add data here. For instance, when we know where Rails routed this request to, we can store that scope info. Or as soon as we know which URI it was directed at, we can store that.
This data is internal to ScoutApm, to add custom information, use the Context api.
225 226 227 |
# File 'lib/scout_apm/tracked_request.rb', line 225 def annotate_request(hsh) @annotations.merge!(hsh) end |
#backtrace_threshold ⇒ Object
Grab backtraces more aggressively when running in dev trace mode
170 171 172 |
# File 'lib/scout_apm/tracked_request.rb', line 170 def backtrace_threshold @agent_context.dev_trace_enabled? ? 0.05 : 0.5 # the minimum threshold in seconds to record the backtrace for a metric. end |
#capture_backtrace?(layer) ⇒ Boolean
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/scout_apm/tracked_request.rb', line 142 def capture_backtrace?(layer) return if ignoring_request? # Never capture backtraces for this kind of layer. The backtrace will # always be 100% framework code. return false if BACKTRACE_BLACKLIST.include?(layer.type) # Only capture backtraces if we're in a real "request". Otherwise we # can spend lot of time capturing backtraces from the internals of # Sidekiq, only to throw them away immediately. return false unless real_request? # Capture any individually slow layer. return true if layer.total_exclusive_time > backtrace_threshold # Capture any layer that we've seen many times. Captures n+1 problems return true if @call_set[layer.name].capture_backtrace? # Don't capture otherwise false end |
#capture_mem_delta! ⇒ Object
180 181 182 |
# File 'lib/scout_apm/tracked_request.rb', line 180 def capture_mem_delta! @mem_delta = mem_usage - @mem_start end |
#current_layer ⇒ Object
Grab the currently running layer. Useful for adding additional data as we learn it. This is useful in ActiveRecord instruments, where we start the instrumentation early, and gradually learn more about the request that actually happened as we go (for instance, the # of records found, or the actual SQL generated).
Returns nil in the case there is no current layer. That would be normal for a completed TrackedRequest
137 138 139 |
# File 'lib/scout_apm/tracked_request.rb', line 137 def current_layer @layers.last end |
#ensure_background_worker ⇒ Object
Ensure the background worker thread is up & running - a fallback if other detection doesn’t achieve this at boot.
323 324 325 326 327 328 |
# File 'lib/scout_apm/tracked_request.rb', line 323 def ensure_background_worker agent = ScoutApm::Agent.instance agent.start rescue => e true end |
#error! ⇒ Object
This request had an exception. Mark it down as an error
230 231 232 |
# File 'lib/scout_apm/tracked_request.rb', line 230 def error! @error = true end |
#error? ⇒ Boolean
234 235 236 |
# File 'lib/scout_apm/tracked_request.rb', line 234 def error? @error end |
#finalized? ⇒ Boolean
Are we finished with this request? We’re done if we have no layers left after popping one off
190 191 192 |
# File 'lib/scout_apm/tracked_request.rb', line 190 def finalized? @layers.none? end |
#ignore_children! ⇒ Object
Enable this when you would otherwise double track something interesting. This came up when we implemented InfluxDB instrumentation, which is more specific, and useful than the fact that InfluxDB happens to use Net::HTTP internally
When enabled, new layers won’t be added to the current Request, and calls to stop_layer will be ignored.
Do not forget to turn if off when leaving a layer, it is the instrumentation’s task to do that.
When you use this in code, be sure to use it in this order:
start_layer ignore_children
-> call
acknowledge_children stop_layer
If you don’t call it in this order, it’s possible to get out of sync, and have an ignored start and an actually-executed stop, causing layers to get out of sync
381 382 383 |
# File 'lib/scout_apm/tracked_request.rb', line 381 def ignore_children! @ignoring_children += 1 end |
#ignore_request! ⇒ Object
At any point in the request, calling code or instrumentation can call ‘ignore_request!` to immediately stop recording any information about new layers, and delete any existing layer info. This class will still exist, and respond to methods as normal, but `record!` won’t be called, and no data will be recorded.
We still need to keep track of the current layer depth (via “reported”, and ready to be recreated for the next request.
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/scout_apm/tracked_request.rb', line 409 def ignore_request! return if @ignoring_request # Set instance variable @ignoring_request = true # Store data we'll need @ignoring_depth = @layers.length # Clear data @layers = [] @root_layer = nil @call_set = nil @annotations = {} @instant_key = nil end |
#ignoring_children? ⇒ Boolean
391 392 393 |
# File 'lib/scout_apm/tracked_request.rb', line 391 def ignoring_children? @ignoring_children > 0 end |
#ignoring_recorded? ⇒ Boolean
438 439 440 |
# File 'lib/scout_apm/tracked_request.rb', line 438 def ignoring_recorded? @ignoring_depth <= 0 end |
#ignoring_request? ⇒ Boolean
426 427 428 |
# File 'lib/scout_apm/tracked_request.rb', line 426 def ignoring_request? @ignoring_request end |
#ignoring_start_layer ⇒ Object
430 431 432 |
# File 'lib/scout_apm/tracked_request.rb', line 430 def ignoring_start_layer @ignoring_depth += 1 end |
#ignoring_stop_layer ⇒ Object
434 435 436 |
# File 'lib/scout_apm/tracked_request.rb', line 434 def ignoring_stop_layer @ignoring_depth -= 1 end |
#instant? ⇒ Boolean
242 243 244 245 246 |
# File 'lib/scout_apm/tracked_request.rb', line 242 def instant? return false if ignoring_request? instant_key end |
#job? ⇒ Boolean
This request is a job transaction iff it has a ‘Job’ layer Use this only during recording
306 307 308 |
# File 'lib/scout_apm/tracked_request.rb', line 306 def job? layer_finder.job != nil end |
#layer_finder ⇒ Object
317 318 319 |
# File 'lib/scout_apm/tracked_request.rb', line 317 def layer_finder @layer_finder ||= LayerConverters::FindLayerByType.new(self) end |
#logger ⇒ Object
442 443 444 |
# File 'lib/scout_apm/tracked_request.rb', line 442 def logger @agent_context.logger end |
#mem_usage ⇒ Object
This may be in bytes or KB based on the OSX. We store this as-is here and only do conversion to MB in Layer Converters. XXX: Move this to environment?
176 177 178 |
# File 'lib/scout_apm/tracked_request.rb', line 176 def mem_usage ScoutApm::Instruments::Process::ProcessMemory.new(@agent_context).rss end |
#prepare_to_dump! ⇒ Object
Actually go fetch & make-real any lazily created data. Clean up any cleverness in objects. Makes this object ready to be Marshal Dumped (or otherwise serialized)
453 454 455 456 457 458 |
# File 'lib/scout_apm/tracked_request.rb', line 453 def prepare_to_dump! @call_set = nil @store = nil @recorder = nil @agent_context = nil end |
#real_request! ⇒ Object
120 121 122 |
# File 'lib/scout_apm/tracked_request.rb', line 120 def real_request! @real_request = true end |
#real_request? ⇒ Boolean
Have we seen a “controller” or “job” layer so far?
125 126 127 |
# File 'lib/scout_apm/tracked_request.rb', line 125 def real_request? @real_request end |
#record! ⇒ Object
Convert this request to the appropriate structure, then report it into the peristent Store object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/scout_apm/tracked_request.rb', line 258 def record! recorded! return if ignoring_request? # If we didn't have store, but we're trying to record anyway, go # figure that out. (this happens in Remote Agent scenarios) restore_from_dump! if @agent_context.nil? # Bail out early if the user asked us to ignore this uri return if @agent_context.ignored_uris.ignore?(annotations[:uri]) converters = [ LayerConverters::Histograms, LayerConverters::MetricConverter, LayerConverters::ErrorConverter, LayerConverters::AllocationMetricConverter, LayerConverters::RequestQueueTimeConverter, LayerConverters::JobConverter, LayerConverters::DatabaseConverter, LayerConverters::SlowJobConverter, LayerConverters::SlowRequestConverter, ] walker = LayerConverters::DepthFirstWalker.new(self.root_layer) converters = converters.map do |klass| instance = klass.new(@agent_context, self, layer_finder, @store) instance.register_hooks(walker) instance end walker.walk converters.each {|i| i.record! } # If there's an instant_key, it means we need to report this right away if web? && instant? converter = converters.find{|c| c.class == LayerConverters::SlowRequestConverter} trace = converter.call ScoutApm::InstantReporting.new(trace, instant_key).call end if web? || job? ensure_background_worker end end |
#recorded! ⇒ Object
Persist the Request
252 253 254 |
# File 'lib/scout_apm/tracked_request.rb', line 252 def recorded! @recorded = true end |
#recorded? ⇒ Boolean
Have we already persisted this request? Used to know when we should just create a new one (don’t attempt to add data to an already-recorded request). See RequestManager
348 349 350 351 352 |
# File 'lib/scout_apm/tracked_request.rb', line 348 def recorded? return ignoring_recorded? if ignoring_request? @recorded end |
#restore_from_dump! ⇒ Object
Go re-fetch the store based on what the Agent’s official one is. Used after hydrating a dumped TrackedRequest
462 463 464 465 466 |
# File 'lib/scout_apm/tracked_request.rb', line 462 def restore_from_dump! @agent_context = ScoutApm::Agent.instance.context @recorder = @agent_context.recorder @store = @agent_context.store end |
#set_headers(headers) ⇒ Object
238 239 240 |
# File 'lib/scout_apm/tracked_request.rb', line 238 def set_headers(headers) @headers = headers end |
#start_layer(layer) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/scout_apm/tracked_request.rb', line 67 def start_layer(layer) # If we're already stopping, don't do additional layers return if stopping? return if ignoring_children? return ignoring_start_layer if ignoring_request? start_request(layer) unless @root_layer if REQUEST_TYPES.include?(layer.type) real_request! end @layers.push(layer) end |
#start_request(layer) ⇒ Object
Run at the beginning of the whole request
-
Capture the first layer as the root_layer
197 198 199 |
# File 'lib/scout_apm/tracked_request.rb', line 197 def start_request(layer) @root_layer = layer unless @root_layer # capture root layer end |
#stop_layer ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/scout_apm/tracked_request.rb', line 83 def stop_layer # If we're already stopping, don't do additional layers return if stopping? return if ignoring_children? return ignoring_stop_layer if ignoring_request? layer = @layers.pop # Safeguard against a mismatch in the layer tracking in an instrument. # This class works under the assumption that start & stop layers are # lined up correctly. If stop_layer gets called twice, when it should # only have been called once you'll end up with this error. if layer.nil? logger.warn("Error stopping layer, was nil. Root Layer: #{@root_layer.inspect}") stop_request return end layer.record_stop_time! layer.record_allocations! @layers[-1].add_child(layer) if @layers.any? # This must be called before checking if a backtrace should be collected as the call count influences our capture logic. # We call `#update_call_counts in stop layer to ensure the layer has a final desc. Layer#desc is updated during the AR instrumentation flow. update_call_counts!(layer) if capture_backtrace?(layer) layer.capture_backtrace! end if finalized? stop_request end end |
#stop_request ⇒ Object
Run at the end of the whole request
-
Send the request off to be stored
204 205 206 207 208 209 210 |
# File 'lib/scout_apm/tracked_request.rb', line 204 def stop_request @stopping = true if recorder recorder.record!(self) end end |
#stopping? ⇒ Boolean
212 213 214 |
# File 'lib/scout_apm/tracked_request.rb', line 212 def stopping? @stopping end |
#unique_name ⇒ Object
Only call this after the request is complete
332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/scout_apm/tracked_request.rb', line 332 def unique_name return nil if ignoring_request? @unique_name ||= begin scope_layer = LayerConverters::FindLayerByType.new(self).scope if scope_layer scope_layer.legacy_metric_name else :unknown end end end |
#update_call_counts!(layer) ⇒ Object
Maintains a lookup Hash of call counts by layer name. Used to determine if we should capture a backtrace.
165 166 167 |
# File 'lib/scout_apm/tracked_request.rb', line 165 def update_call_counts!(layer) @call_set[layer.name].update!(layer.desc) end |
#web? ⇒ Boolean
This request is a web transaction iff it has a ‘Controller’ layer Use this only during recording
312 313 314 |
# File 'lib/scout_apm/tracked_request.rb', line 312 def web? layer_finder.controller != nil end |