Class: OneApm::Collector::StatsHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/one_apm/collector/stats_engine/stats_hash.rb

Defined Under Namespace

Classes: StatsHashLookupError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(started_at = Time.now) ⇒ StatsHash

Returns a new instance of StatsHash.



27
28
29
30
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 27

def initialize(started_at=Time.now)
  @started_at = started_at.to_f
  super() { |hash, key| hash[key] = OneApm::Metrics::Stats.new }
end

Instance Attribute Details

#harvested_atObject

Returns the value of attribute harvested_at.



25
26
27
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 25

def harvested_at
  @harvested_at
end

#started_atObject

Returns the value of attribute started_at.



25
26
27
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 25

def started_at
  @started_at
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 41

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

#marshal_dumpObject



32
33
34
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 32

def marshal_dump
  [@started_at, Hash[self]]
end

#marshal_load(data) ⇒ Object



36
37
38
39
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 36

def marshal_load(data)
  @started_at = data.shift
  self.merge!(data.shift)
end

#merge!(other) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 75

def merge!(other)
  if other.is_a?(StatsHash) && other.started_at < @started_at
    @started_at = other.started_at
  end
  other.each do |key, val|
    merge_or_insert(key, val)
  end
  self
end

#merge_or_insert(metric_spec, stats) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 96

def merge_or_insert(metric_spec, stats)
  if self.has_key?(metric_spec)
    self[metric_spec].merge!(stats)
  else
    self[metric_spec] = stats
  end
end

#merge_transaction_metrics!(txn_metrics, scope) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 85

def merge_transaction_metrics!(txn_metrics, scope)
  txn_metrics.each_unscoped do |name, stats|
    spec = OneApm::MetricSpec.new(name)
    merge_or_insert(spec, stats)
  end
  txn_metrics.each_scoped do |name, stats|
    spec = OneApm::MetricSpec.new(name, scope)
    merge_or_insert(spec, stats)
  end
end

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/one_apm/collector/stats_engine/stats_hash.rb', line 51

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

      stats = OneApm::Metrics::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] = OneApm::Metrics::Stats.new }
      end
    end

    stats.record(value, aux, &blk)
  end
end