Module: NewRelic::Agent::StatsEngine::MetricStats

Included in:
NewRelic::Agent::StatsEngine
Defined in:
lib/new_relic/agent/stats_engine/metric_stats.rb

Instance Method Summary collapse

Instance Method Details

#clear_statsObject

Remove all stats. For test code only.



101
102
103
104
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 101

def clear_stats 
  stats_hash.clear
  NewRelic::Agent::BusyCalculator.reset
end

#get_custom_stats(metric_name, stat_class) ⇒ Object

This version allows a caller to pass a stat class to use



20
21
22
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 20

def get_custom_stats(metric_name, stat_class)
  stats_hash[metric_name] ||= stat_class.new
end

#get_stats(metric_name, use_scope = true, scoped_metric_only = false) ⇒ Object

If use_scope is true, two chained metrics are created, one with scope and one without If scoped_metric_only is true, only a scoped metric is created (used by rendering metrics which by definition are per controller only)



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

def get_stats(metric_name, use_scope = true, scoped_metric_only = false)
  
  if scoped_metric_only
    spec = NewRelic::MetricSpec.new metric_name, scope_name
    stats = stats_hash[spec] ||= NewRelic::MethodTraceStats.new 
  else  
    stats = stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new 
    if use_scope && scope_name
      spec = NewRelic::MetricSpec.new metric_name, scope_name
      scoped_stats = stats_hash[spec] ||= NewRelic::ScopedMethodTraceStats.new(stats) 
      stats = scoped_stats
    end
  end
  stats
end

#get_stats_no_scope(metric_name) ⇒ Object



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

def get_stats_no_scope(metric_name)
  stats_hash[metric_name] ||= NewRelic::MethodTraceStats.new 
end

#harvest_timeslice_data(previous_timeslice_data, metric_ids) ⇒ Object

Harvest the timeslice data. First recombine current statss with any previously unsent metrics, clear out stats cache, and return the current stats.


Note: this is not synchronized. There is still some risk in this and we will revisit later to see if we can make this more robust without sacrificing efficiency. +++



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 55

def harvest_timeslice_data(previous_timeslice_data, metric_ids)
  timeslice_data = {}
  poll harvest_samplers
  stats_hash.keys.each do | metric_spec |
    
    
    # get a copy of the stats collected since the last harvest, and clear
    # the stats inside our hash table for the next time slice.
    stats = stats_hash[metric_spec]
    
    # we have an optimization for unscoped metrics
    if !(metric_spec.is_a? NewRelic::MetricSpec)
      metric_spec = NewRelic::MetricSpec.new metric_spec
    end
    
    if stats.nil? 
      raise "Nil stats for #{metric_spec.name} (#{metric_spec.scope})"
    end
    
    stats_copy = stats.clone
    stats.reset
    
    # if the previous timeslice data has not been reported (due to an error of some sort)
    # then we need to merge this timeslice with the previously accumulated - but not sent
    # data
    previous_metric_data = previous_timeslice_data[metric_spec]
    stats_copy.merge! previous_metric_data.stats unless previous_metric_data.nil?
    stats_copy.round!
    
    # don't bother collecting and reporting stats that have zero-values for this timeslice.
    # significant performance boost and storage savings.
    unless stats_copy.is_reset?

      id = metric_ids[metric_spec]
      metric_spec_for_transport = id ? nil : metric_spec 
      
      metric_data = NewRelic::MetricData.new(metric_spec_for_transport, stats_copy, id)
      
      timeslice_data[metric_spec] = metric_data
    end
  end
  
  timeslice_data
end

#lookup_stat(metric_name) ⇒ Object

The stats hash hashes either a metric name for an unscoped metric, or a metric_spec for a scoped metric value.



6
7
8
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 6

def lookup_stat(metric_name)
  stats_hash[metric_name]
end

#lookup_stats(metric_name, scope_name = nil) ⇒ Object



42
43
44
45
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 42

def lookup_stats(metric_name, scope_name = nil)
  stats_hash[NewRelic::MetricSpec.new(metric_name, scope_name)] ||
  stats_hash[metric_name]
end

#metricsObject



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

def metrics
  stats_hash.keys.map(&:to_s)
end

#reset_statsObject

Reset each of the stats, such as when a new passenger instance starts up.



107
108
109
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 107

def reset_stats 
  stats_hash.values.each { |s| s.reset }
end

#stats_hashObject



111
112
113
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 111

def stats_hash
  @stats_hash ||= {}
end