Class: ScoutApm::Layer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, start_time = Time.now) ⇒ Layer

Returns a new instance of Layer.



34
35
36
37
38
39
40
# File 'lib/scout_apm/layer.rb', line 34

def initialize(type, name, start_time = Time.now)
  @type = type
  @name = name
  @start_time = start_time
  @children = [] # In order of calls
  @desc = nil
end

Instance Attribute Details

#backtraceObject (readonly)

If this layer took longer than a fixed amount of time, store the backtrace of where it occurred.



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

def backtrace
  @backtrace
end

#childrenObject (readonly)

An array of children layers, in call order. For instance, if we are in a middleware, there will likely be only a single child, which is another middleware. In a Controller, we may have a handful of children: [ActiveRecord, ActiveRecord, View, HTTP Call].

This useful to get actual time spent in this layer vs. children time



18
19
20
# File 'lib/scout_apm/layer.rb', line 18

def children
  @children
end

#descObject

The description of this layer. Will contain additional details specific to the type of layer. For an ActiveRecord metric, it will contain the SQL run For an outoing HTTP call, it will contain the remote URL accessed Leave blank if there is nothing to note



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

def desc
  @desc
end

#nameObject (readonly)

Name: a more specific name of this single item

Examples: "Rack::Cache", "User#find", "users/index", "users/index.html.erb"


10
11
12
# File 'lib/scout_apm/layer.rb', line 10

def name
  @name
end

#start_timeObject (readonly)

Time objects recording the start & stop times of this layer



21
22
23
# File 'lib/scout_apm/layer.rb', line 21

def start_time
  @start_time
end

#stop_timeObject (readonly)

Time objects recording the start & stop times of this layer



21
22
23
# File 'lib/scout_apm/layer.rb', line 21

def stop_time
  @stop_time
end

#typeObject (readonly)

Type: a general name for the kind of thing being tracked.

Examples: "Middleware", "ActiveRecord", "Controller", "View"


6
7
8
# File 'lib/scout_apm/layer.rb', line 6

def type
  @type
end

Instance Method Details

#add_child(child) ⇒ Object



42
43
44
# File 'lib/scout_apm/layer.rb', line 42

def add_child(child)
  @children << child
end

#child_timeObject



111
112
113
114
115
# File 'lib/scout_apm/layer.rb', line 111

def child_time
  children.
    map { |child| child.total_call_time }.
    inject(0) { |sum, time| sum + time }
end

#legacy_metric_nameObject

This is the old style name. This function is used for now, but should be removed, and the new type & name split should be enforced through the app.



65
66
67
# File 'lib/scout_apm/layer.rb', line 65

def legacy_metric_name
  "#{type}/#{name}"
end

#record_stop_time!(stop_time = Time.now) ⇒ Object



46
47
48
# File 'lib/scout_apm/layer.rb', line 46

def record_stop_time!(stop_time = Time.now)
  @stop_time = stop_time
end

#store_backtrace(bt) ⇒ Object



69
70
71
72
73
# File 'lib/scout_apm/layer.rb', line 69

def store_backtrace(bt)
  return unless bt.is_a? Array
  return unless bt.length > 0
  @backtrace = bt
end

#subscopable!Object



54
55
56
# File 'lib/scout_apm/layer.rb', line 54

def subscopable!
  @subscopable = true
end

#subscopable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/scout_apm/layer.rb', line 58

def subscopable?
  @subscopable
end

#to_sObject

May not be safe to call in every rails app, relies on Time#iso8601



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scout_apm/layer.rb', line 80

def to_s
  name_clause = "#{type}/#{name}"

  total_string = total_call_time == 0 ? nil : "Total: #{total_call_time}"
  self_string = total_exclusive_time == 0 ? nil : "Self: #{total_exclusive_time}"
  timing_string = [total_string, self_string].compact.join(", ")

  time_clause = "(Start: #{start_time.iso8601} / Stop: #{stop_time.try(:iso8601)} [#{timing_string}])"
  desc_clause = "Description: #{desc.inspect}"
  children_clause = "Children: #{children.length}"

  "<Layer: #{name_clause} #{time_clause} #{desc_clause} #{children_clause}>"
end

#total_call_timeObject

Time Calculations



98
99
100
101
102
103
104
105
# File 'lib/scout_apm/layer.rb', line 98

def total_call_time
  if stop_time
    stop_time - start_time
  else
    # Shouldn't have called this yet. Return 0
    0
  end
end

#total_exclusive_timeObject



107
108
109
# File 'lib/scout_apm/layer.rb', line 107

def total_exclusive_time
  total_call_time - child_time
end