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

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

Overview

Handles methods related to actual Metric collection

Defined Under Namespace

Modules: Harvest Classes: SynchronizedHash

Instance Method Summary collapse

Methods included from Harvest

#merge_data

Instance Method Details

#clear_statsObject

Remove all stats. For test code only.



169
170
171
172
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 169

def clear_stats
  @stats_hash = SynchronizedHash.new
  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



47
48
49
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 47

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

#get_stats(metric_name, use_scope = true, scoped_metric_only = false, scope = nil) ⇒ 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)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 53

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

#get_stats_no_scope(metric_name) ⇒ Object

a simple accessor for looking up a stat with no scope - returns a new stats object if no stats object for that metric exists yet



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

def get_stats_no_scope(metric_name)
  stats_hash[NewRelic::MetricSpec.new(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. +++



162
163
164
165
166
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 162

def harvest_timeslice_data(previous_timeslice_data, metric_ids)

  poll harvest_samplers
  merge_stats(previous_timeslice_data, metric_ids)
end

#lookup_stats(metric_name, scope_name = '') ⇒ Object

Returns a stat if one exists, otherwise returns nil. If you want auto-initialization, use one of get_stats or get_stats_no_scope



70
71
72
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 70

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

#metricsObject

Returns all of the metric names of all the stats in the engine



35
36
37
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 35

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.



175
176
177
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 175

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

#stats_hashObject

returns a memoized SynchronizedHash that holds the actual instances of Stats keyed off their MetricName



181
182
183
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 181

def stats_hash
  @stats_hash ||= SynchronizedHash.new
end