Class: ScoutApm::MetricStats

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

Overview

Stats that are associated with each instrumented method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scoped = false) ⇒ MetricStats

Returns a new instance of MetricStats.



10
11
12
13
14
15
16
17
18
# File 'lib/scout_apm/metric_stats.rb', line 10

def initialize(scoped = false)
  @scoped = scoped
  self.call_count = 0
  self.total_call_time = 0.0
  self.total_exclusive_time = 0.0
  self.min_call_time = 0.0
  self.max_call_time = 0.0
  self.sum_of_squares = 0.0
end

Instance Attribute Details

#call_countObject

Returns the value of attribute call_count.



3
4
5
# File 'lib/scout_apm/metric_stats.rb', line 3

def call_count
  @call_count
end

#max_call_timeObject

Returns the value of attribute max_call_time.



5
6
7
# File 'lib/scout_apm/metric_stats.rb', line 5

def max_call_time
  @max_call_time
end

#min_call_timeObject

Returns the value of attribute min_call_time.



4
5
6
# File 'lib/scout_apm/metric_stats.rb', line 4

def min_call_time
  @min_call_time
end

#sum_of_squaresObject

Returns the value of attribute sum_of_squares.



8
9
10
# File 'lib/scout_apm/metric_stats.rb', line 8

def sum_of_squares
  @sum_of_squares
end

#total_call_timeObject

Returns the value of attribute total_call_time.



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

def total_call_time
  @total_call_time
end

#total_exclusive_timeObject

Returns the value of attribute total_exclusive_time.



7
8
9
# File 'lib/scout_apm/metric_stats.rb', line 7

def total_exclusive_time
  @total_exclusive_time
end

Instance Method Details

#combine!(other) ⇒ Object

combines data from another MetricStats object



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

def combine!(other)
  self.call_count += other.call_count
  self.total_call_time += other.total_call_time
  self.total_exclusive_time += other.total_exclusive_time
  self.min_call_time = other.min_call_time if self.min_call_time.zero? or other.min_call_time < self.min_call_time
  self.max_call_time = other.max_call_time if other.max_call_time > self.max_call_time
  self.sum_of_squares += other.sum_of_squares
  self
end

#to_json(*a) ⇒ Object

To avoid conflicts with different JSON libaries handle JSON ourselves. Time-based metrics are converted to milliseconds from seconds.



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

def to_json(*a)
   %Q[{"total_exclusive_time":#{total_exclusive_time*1000},"min_call_time":#{min_call_time*1000},"call_count":#{call_count},"sum_of_squares":#{sum_of_squares*1000},"total_call_time":#{total_call_time*1000},"max_call_time":#{max_call_time*1000}}]
end

#update!(call_time, exclusive_time) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scout_apm/metric_stats.rb', line 20

def update!(call_time,exclusive_time)
  # If this metric is scoped inside another, use exclusive time for min/max and sum_of_squares. Non-scoped metrics
  # (like controller actions) track the total call time.
  t = (@scoped ? exclusive_time : call_time)
  self.min_call_time = t if self.call_count == 0 or t < min_call_time
  self.max_call_time = t if self.call_count == 0 or t > max_call_time
  self.call_count +=1
  self.total_call_time += call_time
  self.total_exclusive_time += exclusive_time
  self.sum_of_squares += (t * t)
  self
end