Class: NewRelic::Agent::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/stats.rb

Direct Known Subclasses

SqlTrace

Constant Summary collapse

SKIP_MARSHALLING =
[:@lock]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



17
18
19
20
# File 'lib/new_relic/agent/stats.rb', line 17

def initialize
  @lock = Mutex.new
  reset
end

Instance Attribute Details

#call_countObject Also known as: apdex_s



10
11
12
# File 'lib/new_relic/agent/stats.rb', line 10

def call_count
  @call_count
end

#max_call_timeObject



12
13
14
# File 'lib/new_relic/agent/stats.rb', line 12

def max_call_time
  @max_call_time
end

#min_call_timeObject



11
12
13
# File 'lib/new_relic/agent/stats.rb', line 11

def min_call_time
  @min_call_time
end

#sum_of_squaresObject



15
16
17
# File 'lib/new_relic/agent/stats.rb', line 15

def sum_of_squares
  @sum_of_squares
end

#total_call_timeObject Also known as: apdex_t



13
14
15
# File 'lib/new_relic/agent/stats.rb', line 13

def total_call_time
  @total_call_time
end

#total_exclusive_timeObject Also known as: apdex_f



14
15
16
# File 'lib/new_relic/agent/stats.rb', line 14

def total_exclusive_time
  @total_exclusive_time
end

Instance Method Details

#==(other) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/new_relic/agent/stats.rb', line 125

def ==(other)
  other.class == self.class &&
    (
      @min_call_time == other.min_call_time &&
      @max_call_time == other.max_call_time &&
      @total_call_time == other.total_call_time &&
      @total_exclusive_time == other.total_exclusive_time &&
      @sum_of_squares == other.sum_of_squares &&
      @call_count == other.call_count
    )
end

#hash_merge(hash) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/new_relic/agent/stats.rb', line 52

def hash_merge(hash)
  @call_count = hash[:count] if hash[:count]
  @total_call_time = hash[:total] if hash[:total]
  @total_exclusive_time = hash[:total] if hash[:total]
  @min_call_time = hash[:min] if hash[:min]
  @max_call_time = hash[:max] if hash[:max]
  @sum_of_squares = hash[:sum_of_squares] if hash[:sum_of_squares]
  self
end

#increment_count(value = 1) ⇒ Object

increments the call_count by one



112
113
114
# File 'lib/new_relic/agent/stats.rb', line 112

def increment_count(value = 1)
  @lock.synchronize { @call_count += value }
end

#inspect_fullObject

Concerned about implicit usage of inspect relying on stats format, so putting back a version to get full inspection as separate method



118
119
120
121
122
123
# File 'lib/new_relic/agent/stats.rb', line 118

def inspect_full
  variables = instance_variables.map do |ivar|
    "#{ivar.to_s}=#{instance_variable_get(ivar).inspect}"
  end.join(' ')
  "#<NewRelic::Agent::Stats #{variables}>"
end

#is_reset?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/new_relic/agent/stats.rb', line 31

def is_reset?
  call_count == 0 && total_call_time == 0.0 && total_exclusive_time == 0.0
end

#marshal_dumpObject

Override marshalling methods to exclude @lock from being included in marshalled data



159
160
161
162
163
164
165
# File 'lib/new_relic/agent/stats.rb', line 159

def marshal_dump
  instance_variables.each_with_object({}) do |name, instance_copy|
    next if SKIP_MARSHALLING.include?(name)

    instance_copy[name] = instance_variable_get(name)
  end
end

#marshal_load(marshalled_data) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/new_relic/agent/stats.rb', line 167

def marshal_load(marshalled_data)
  marshalled_data.each do |name, value|
    instance_variable_set(name, value) unless SKIP_MARSHALLING.include?(name)
  end
  # since the lock is excluded when marshalling, create a new lock when loading marshalled data
  @lock = Mutex.new
end

#merge(other_stats) ⇒ Object



35
36
37
38
# File 'lib/new_relic/agent/stats.rb', line 35

def merge(other_stats)
  stats = self.clone
  stats.merge!(other_stats)
end

#merge!(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/new_relic/agent/stats.rb', line 40

def merge!(other)
  @lock.synchronize do
    @min_call_time = other.min_call_time if min_time_less?(other)
    @max_call_time = other.max_call_time if other.max_call_time > max_call_time
    @total_call_time += other.total_call_time
    @total_exclusive_time += other.total_exclusive_time
    @sum_of_squares += other.sum_of_squares
    @call_count += other.call_count
  end
  self
end

#record(value = nil, aux = nil, &blk) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/new_relic/agent/stats.rb', line 77

def record(value = nil, aux = nil, &blk)
  if blk
    yield(self)
  else
    case value
    when Numeric
      aux ||= value
      self.record_data_point(value, aux)
    when :apdex_s, :apdex_t, :apdex_f
      self.record_apdex(value, aux)
    when NewRelic::Agent::Stats
      self.merge!(value)
    end
  end
end

#record_apdex(bucket, apdex_t) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/new_relic/agent/stats.rb', line 142

def record_apdex(bucket, apdex_t)
  @lock.synchronize do
    case bucket
    when :apdex_s then @call_count += 1
    when :apdex_t then @total_call_time += 1
    when :apdex_f then @total_exclusive_time += 1
    end
    if apdex_t
      @min_call_time = apdex_t
      @max_call_time = apdex_t
    else
      ::NewRelic::Agent.logger.warn("Attempted to set apdex_t to #{apdex_t.inspect}, backtrace = #{caller.join("\n")}")
    end
  end
end

#record_data_point(value, exclusive_time = value) ⇒ Object Also known as: trace_call

record a single data point into the statistical gatherer. The gatherer will aggregate all data points collected over a specified period and upload its data to the NewRelic server



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/new_relic/agent/stats.rb', line 96

def record_data_point(value, exclusive_time = value)
  @lock.synchronize do
    @call_count += 1
    @total_call_time += value
    @min_call_time = value if value < @min_call_time || @call_count == 1
    @max_call_time = value if value > @max_call_time
    @total_exclusive_time += exclusive_time

    @sum_of_squares += (value * value)
  end
  self
end

#resetObject



22
23
24
25
26
27
28
29
# File 'lib/new_relic/agent/stats.rb', line 22

def reset
  @call_count = 0
  @total_call_time = 0.0
  @total_exclusive_time = 0.0
  @min_call_time = 0.0
  @max_call_time = 0.0
  @sum_of_squares = 0.0
end

#to_json(*_) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/new_relic/agent/stats.rb', line 62

def to_json(*_)
  {
    'call_count' => call_count.to_i,
    'min_call_time' => min_call_time.to_f,
    'max_call_time' => max_call_time.to_f,
    'total_call_time' => total_call_time.to_f,
    'total_exclusive_time' => total_exclusive_time.to_f,
    'sum_of_squares' => sum_of_squares.to_f
  }.to_json(*_)
end

#to_sObject



73
74
75
# File 'lib/new_relic/agent/stats.rb', line 73

def to_s
  "[#{'%2i' % call_count.to_i} calls #{'%.4f' % total_call_time.to_f}s / #{'%.4f' % total_exclusive_time.to_f}s ex]"
end