Class: ScoutApm::TrackedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/tracked_request.rb

Constant Summary collapse

BACKTRACE_THRESHOLD =

the minimum threshold in seconds to record the backtrace for a metric.

0.5
BACKTRACE_BLACKLIST =
["Controller", "Job"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrackedRequest

Returns a new instance of TrackedRequest.



44
45
46
47
48
49
50
51
52
53
# File 'lib/scout_apm/tracked_request.rb', line 44

def initialize
  @layers = []
  @call_counts = Hash.new { |h, k| h[k] = CallSet.new }
  @annotations = {}
  @ignoring_children = false
  @context = Context.new
  @root_layer = nil
  @stackprof = nil
  @error = false
end

Instance Attribute Details

#annotationsObject (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_countsObject

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.



40
41
42
# File 'lib/scout_apm/tracked_request.rb', line 40

def call_counts
  @call_counts
end

#contextObject (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

#headersObject (readonly)

Headers as recorded by rails Can be nil if we never reach a Rails Controller



31
32
33
# File 'lib/scout_apm/tracked_request.rb', line 31

def headers
  @headers
end

#request_typeObject (readonly)

What kind of request is this? A trace of a web request, or a background job? Use job! and web! to set, and job? and web? to query



35
36
37
# File 'lib/scout_apm/tracked_request.rb', line 35

def request_type
  @request_type
end

#root_layerObject (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

#stackprofObject (readonly)

Nil until the request is finalized, at which point it will hold the entire raw stackprof output for this request



27
28
29
# File 'lib/scout_apm/tracked_request.rb', line 27

def stackprof
  @stackprof
end

Instance Method Details

#acknowledge_children!Object



234
235
236
# File 'lib/scout_apm/tracked_request.rb', line 234

def acknowledge_children!
  @ignoring_children = false
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.



147
148
149
# File 'lib/scout_apm/tracked_request.rb', line 147

def annotate_request(hsh)
  @annotations.merge!(hsh)
end

#capture_backtrace?(layer) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scout_apm/tracked_request.rb', line 82

def capture_backtrace?(layer)
  # 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 (web? || job?)

  # 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_counts[layer.name].capture_backtrace?

  # Don't capture otherwise
  false
end

#error!Object

This request had an exception. Mark it down as an error



152
153
154
# File 'lib/scout_apm/tracked_request.rb', line 152

def error!
  @error = true
end

#error?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/scout_apm/tracked_request.rb', line 156

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/scout_apm/tracked_request.rb', line 113

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.

Do not forget to turn if off when leaving a layer, it is the instrumentation’s task to do that.



230
231
232
# File 'lib/scout_apm/tracked_request.rb', line 230

def ignore_children!
  @ignoring_children = true
end

#ignoring_children?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/scout_apm/tracked_request.rb', line 238

def ignoring_children?
  @ignoring_children
end

#job!Object



164
165
166
# File 'lib/scout_apm/tracked_request.rb', line 164

def job!
  @request_type = "job"
end

#job?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/scout_apm/tracked_request.rb', line 168

def job?
  request_type == "job"
end

#record!Object

Convert this request to the appropriate structure, then report it into the peristent Store object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/scout_apm/tracked_request.rb', line 186

def record!
  @recorded = true

  metrics = LayerConverters::MetricConverter.new(self).call
  ScoutApm::Agent.instance.store.track!(metrics)

  slow, slow_metrics = LayerConverters::SlowRequestConverter.new(self).call
  ScoutApm::Agent.instance.store.track_slow_transaction!(slow)
  ScoutApm::Agent.instance.store.track!(slow_metrics)

  error_metrics = LayerConverters::ErrorConverter.new(self).call
  ScoutApm::Agent.instance.store.track!(error_metrics)

  queue_time_metrics = LayerConverters::RequestQueueTimeConverter.new(self).call
  ScoutApm::Agent.instance.store.track!(queue_time_metrics)

  job = LayerConverters::JobConverter.new(self).call
  ScoutApm::Agent.instance.store.track_job!(job)

  slow_job = LayerConverters::SlowJobConverter.new(self).call
  ScoutApm::Agent.instance.store.track_slow_job!(slow_job)
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

Returns:

  • (Boolean)


212
213
214
# File 'lib/scout_apm/tracked_request.rb', line 212

def recorded?
  @recorded
end

#set_headers(headers) ⇒ Object



160
161
162
# File 'lib/scout_apm/tracked_request.rb', line 160

def set_headers(headers)
  @headers = headers
end

#start_layer(layer) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/scout_apm/tracked_request.rb', line 55

def start_layer(layer)
  if ignoring_children?
    return
  end

  start_request(layer) unless @root_layer
  update_call_counts!(layer)
  @layers[-1].add_child(layer) if @layers.any?
  @layers.push(layer)
end

#start_request(layer) ⇒ Object

Run at the beginning of the whole request

  • Capture the first layer as the root_layer

  • Start Stackprof (disabling to avoid conflicts if stackprof is included as middleware since we aren’t sending this up to server now)



121
122
123
124
# File 'lib/scout_apm/tracked_request.rb', line 121

def start_request(layer)
  @root_layer = layer unless @root_layer # capture root layer
  #StackProf.start(:mode => :wall, :interval => ScoutApm::Agent.instance.config.value("stackprof_interval"))
end

#stop_layerObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scout_apm/tracked_request.rb', line 66

def stop_layer
  return if ignoring_children?

  layer = @layers.pop
  layer.record_stop_time!

  if capture_backtrace?(layer)
    layer.capture_backtrace!
  end

  if finalized?
    stop_request
  end
end

#stop_requestObject

Run at the end of the whole request

  • Collect stackprof info

  • Send the request off to be stored



130
131
132
133
134
135
136
# File 'lib/scout_apm/tracked_request.rb', line 130

def stop_request
  # ScoutApm::Agent.instance.logger.debug("stop_request: #{annotations[:uri]}" )
  #StackProf.stop # disabling to avoid conflicts if stackprof is included as middleware since we aren't sending this up to server now
  #@stackprof = StackProf.results

  record!
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.



103
104
105
# File 'lib/scout_apm/tracked_request.rb', line 103

def update_call_counts!(layer)
  @call_counts[layer.name].update!(layer.desc)
end

#web!Object



172
173
174
# File 'lib/scout_apm/tracked_request.rb', line 172

def web!
  @request_type = "web"
end

#web?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/scout_apm/tracked_request.rb', line 176

def web?
  request_type == "web"
end