Class: ScoutApm::MetricStats

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scoped = false) ⇒ MetricStats

Returns a new instance of MetricStats.



13
14
15
16
17
18
19
20
21
# File 'lib/scout_apm/metric_stats.rb', line 13

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.



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

def call_count
  @call_count
end

#latencyObject

Returns the value of attribute latency.



11
12
13
# File 'lib/scout_apm/metric_stats.rb', line 11

def latency
  @latency
end

#max_call_timeObject

Returns the value of attribute max_call_time.



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

def max_call_time
  @max_call_time
end

#min_call_timeObject

Returns the value of attribute min_call_time.



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

def min_call_time
  @min_call_time
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

#sum_of_squaresObject

Returns the value of attribute sum_of_squares.



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

def sum_of_squares
  @sum_of_squares
end

#total_call_timeObject

Returns the value of attribute total_call_time.



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

def total_call_time
  @total_call_time
end

#total_exclusive_timeObject

Returns the value of attribute total_exclusive_time.



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

def total_exclusive_time
  @total_exclusive_time
end

Instance Method Details

#as_jsonObject



59
60
61
62
63
# File 'lib/scout_apm/metric_stats.rb', line 59

def as_json
  json_attributes = [:call_count, :total_call_time, :total_exclusive_time, :min_call_time, :max_call_time]
  # uri, context
  ScoutApm::AttributeArranger.call(self, json_attributes)
end

#combine!(other) ⇒ Object

combines data from another MetricStats object



43
44
45
46
47
48
49
50
51
# File 'lib/scout_apm/metric_stats.rb', line 43

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.



55
56
57
# File 'lib/scout_apm/metric_stats.rb', line 55

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 = call_time, extra_metrics = {}) ⇒ Object

Note, that you must include exclusive_time if you wish to set extra_metrics. A two argument use of this method won’t do that.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scout_apm/metric_stats.rb', line 25

def update!(call_time, exclusive_time=call_time, extra_metrics={})
  # 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)
  if extra_metrics
    self.queue = extra_metrics[:queue] if extra_metrics[:queue]
    self.latency = extra_metrics[:latency] if extra_metrics[:latency]
  end
  self
end