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

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

Overview

Handles methods related to actual Metric collection

Instance Method Summary collapse

Instance Method Details

#apply_rules_to_metric_data(rules_engine, stats_hash) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 116

def apply_rules_to_metric_data(rules_engine, stats_hash)
  renamed_stats = NewRelic::Agent::StatsHash.new
  stats_hash.each do |spec, stats|
    new_name = rules_engine.rename(spec.name)
    new_spec = NewRelic::MetricSpec.new(new_name, spec.scope)
    renamed_stats[new_spec].merge!(stats)
  end
  renamed_stats
end

#clear_statsObject

For use by test code only.



146
147
148
149
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 146

def clear_stats
  reset_stats
  NewRelic::Agent::BusyCalculator.reset
end

#coerce_to_metric_spec_array(metric_names_or_specs, scope) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 131

def coerce_to_metric_spec_array(metric_names_or_specs, scope)
  specs = []
  Array(metric_names_or_specs).map do |name_or_spec|
    case name_or_spec
    when String
      specs << NewRelic::MetricSpec.new(name_or_spec)
      specs << NewRelic::MetricSpec.new(name_or_spec, scope) if scope
    when NewRelic::MetricSpec
      specs << name_or_spec
    end
  end
  specs
end

#default_scopeObject



126
127
128
129
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 126

def default_scope
  txn = NewRelic::Agent::TransactionInfo.get
  txn.transaction_name_set? && txn.transaction_name
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)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 34

def get_stats(metric_name, use_scope = true, scoped_metric_only = false, scope = nil)
  scope ||= scope_name if use_scope
  stats = nil
  with_stats_lock do
    if scoped_metric_only
      stats = @stats_hash[NewRelic::MetricSpec.new(metric_name, scope)]
    else
      unscoped_spec = NewRelic::MetricSpec.new(metric_name)
      unscoped_stats = @stats_hash[unscoped_spec]
      if scope && scope != metric_name
        scoped_spec = NewRelic::MetricSpec.new(metric_name, scope)
        scoped_stats = @stats_hash[scoped_spec]
        stats = NewRelic::Agent::ChainedStats.new(scoped_stats, unscoped_stats)
      else
        stats = unscoped_stats
      end
    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



28
29
30
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 28

def get_stats_no_scope(metric_name)
  get_stats(metric_name, false)
end

#harvest_timeslice_data(old_stats_hash, rules_engine = RulesEngine.new) ⇒ Object

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



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

def harvest_timeslice_data(old_stats_hash, rules_engine=RulesEngine.new)
  poll harvest_samplers
  snapshot = reset_stats
  snapshot = apply_rules_to_metric_data(rules_engine, snapshot)
  snapshot.merge!(old_stats_hash)
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



57
58
59
60
61
62
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 57

def lookup_stats(metric_name, scope_name = '')
  spec = NewRelic::MetricSpec.new(metric_name, scope_name)
  with_stats_lock do
    @stats_hash.has_key?(spec) ? @stats_hash[spec] : nil
  end
end

#merge!(other_stats_hash) ⇒ Object

merge data from previous harvests into this stats engine



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

def merge!(other_stats_hash)
  with_stats_lock do
    @stats_hash.merge!(other_stats_hash)
  end
end

#metricsObject

Returns all of the metric names of all the stats in the engine. For use by test code only.



153
154
155
156
157
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 153

def metrics
  with_stats_lock do
    @stats_hash.keys.map { |spec| spec.to_s }
  end
end

#record_metric(metric_names_or_specs, value = nil, options = {}, &blk) ⇒ Object

Lookup and write to the named metric in a single call.

This method is thead-safe, and is preferred to the lookup / modify method pairs (e.g. get_stats + record_data_point)



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 12

def record_metric(metric_names_or_specs, value=nil, options={}, &blk)
  defaults = {
    :scoped => false,
    :scope => default_scope
  }
  options = defaults.merge(options)
  effective_scope = options[:scoped] && options[:scope]
  specs = coerce_to_metric_spec_array(metric_names_or_specs, effective_scope)
  with_stats_lock do
    @stats_hash.record(specs, value, &blk)
  end
end

#record_supportability_metrics(value, *metrics) ⇒ Object

Helper method for recording supportability metrics consistently



84
85
86
87
88
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 84

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



77
78
79
80
81
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 77

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/new_relic/agent/stats_engine/metric_stats.rb', line 65

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



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

def reset_stats
  with_stats_lock do
    old = @stats_hash
    @stats_hash = StatsHash.new
    old
  end
end