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.



244
245
246
247
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 244

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



95
96
97
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 95

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)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 101

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



90
91
92
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 90

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. +++



238
239
240
241
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 238

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



118
119
120
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 118

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



83
84
85
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 83

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

#record_supportability_metrics(value, *metrics) ⇒ Object

Helper method for recording supportability metrics consistently



143
144
145
146
147
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 143

def record_supportability_metrics(value, *metrics)
  metrics.each do |metric|
      yield(value, get_stats_no_scope("Supportability/#{metric}"))
  end
end

#record_supportability_metrics_count(value, *metrics) ⇒ Object

Helper for recording a straight value into the count



136
137
138
139
140
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 136

def record_supportability_metrics_count(value, *metrics)
  record_supportability_metrics(value, *metrics) do |value, metric|
    metric.call_count = value
  end
end

#record_supportability_metrics_timed(metrics) ⇒ Object

Helper method for timing supportability metrics



124
125
126
127
128
129
130
131
132
133
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 124

def record_supportability_metrics_timed(metrics)
  start_time = Time.now
  yield
  end_time = Time.now
  duration = (end_time - start_time).to_f
ensure
  record_supportability_metrics(duration, metrics) do |value, metric|
    metric.record_data_point(value)
  end
end

#reset_statsObject

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



250
251
252
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 250

def reset_stats
  stats_hash.reset
end

#stats_hashObject

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



256
257
258
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 256

def stats_hash
  @stats_hash ||= SynchronizedHash.new
end