Class: NewRelic::Agent::StatsHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/new_relic/agent/stats_engine/stats_hash.rb

Defined Under Namespace

Classes: CorruptedDefaultProcError, StatsMergerError

Instance Method Summary collapse

Constructor Details

#initializeStatsHash

Returns a new instance of StatsHash.



21
22
23
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 21

def initialize
  super { |hash, key| hash[key] = NewRelic::Agent::Stats.new }
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 33

def ==(other)
  Hash[self] == Hash[other]
end

#marshal_dumpObject



25
26
27
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 25

def marshal_dump
  Hash[self]
end

#marshal_load(hash) ⇒ Object



29
30
31
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 29

def marshal_load(hash)
  self.merge!(hash)
end

#merge!(other) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 86

def merge!(other)
  other.each do |key,val|
    begin
      if self.has_key?(key)
        self[key].merge!(val)
      else
        self[key] = val
      end
    rescue => err
      NewRelic::Agent.instance.error_collector. \
        notice_agent_error(StatsMergerError.new(key, self.fetch(key, nil), val, err))
    end
  end
  self
end

#record(metric_specs, value = nil, aux = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 43

def record(metric_specs, value=nil, aux=nil)
  Array(metric_specs).each do |metric_spec|
    stats = nil
    begin
      stats = self[metric_spec]
    rescue => e
      # This only happen in the case of a corrupted default_proc
      # Side-step it manually, notice the issue, and carry on....
      NewRelic::Agent.instance.error_collector. \
        notice_agent_error(CorruptedDefaultProcError.new(self, metric_spec))

      stats = NewRelic::Agent::Stats.new
      self[metric_spec] = stats

      # Try to restore the default_proc so we won't continually trip the error
      if respond_to?(:default_proc=)
        self.default_proc = Proc.new { |hash, key| hash[key] = NewRelic::Agent::Stats.new }
      end
    end

    if block_given?
      yield stats
    else
      case value
      when Numeric
        aux ||= value
        stats.record_data_point(value, aux)
      when :apdex_s, :apdex_t, :apdex_f
        stats.record_apdex(value, aux)
      when NewRelic::Agent::Stats
        stats.merge!(value)
      end
    end
  end
end

#resolve_scopes!(resolved_scope) ⇒ Object



102
103
104
105
106
107
# File 'lib/new_relic/agent/stats_engine/stats_hash.rb', line 102

def resolve_scopes!(resolved_scope)
  placeholder = StatsEngine::SCOPE_PLACEHOLDER.to_s
  each_pair do |spec, stats|
    spec.scope = resolved_scope if spec.scope == placeholder
  end
end