class Mmtrix::Agent::StatsHash

Attributes

harvested_at[RW]
started_at[RW]

Public Class Methods

new(started_at=Time.now) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 36
def initialize(started_at=Time.now)
  @started_at = started_at.to_f
  @scoped     = Hash.new { |h, k| h[k] = Mmtrix::Agent::Stats.new }
  @unscoped   = Hash.new { |h, k| h[k] = Mmtrix::Agent::Stats.new }
end

Public Instance Methods

==(other) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 54
def ==(other)
  self.to_h == other.to_h
end
[](key) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 65
def [](key)
  case key
  when String
    @unscoped[key]
  when Mmtrix::MetricSpec
    if key.scope.empty?
      @unscoped[key.name]
    else
      @scoped[key]
    end
  end
end
each() { |k, v| ... } click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 78
def each
  @scoped.each do |k, v|
    yield k, v
  end
  @unscoped.each do |k, v|
    spec = Mmtrix::MetricSpec.new(k)
    yield spec, v
  end
end
empty?() click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 88
def empty?
  @unscoped.empty? && @scoped.empty?
end
handle_stats_lookup_error(key, hash, error) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 122
def handle_stats_lookup_error(key, hash, error)
  # This only happen in the case of a corrupted default_proc
  # Side-step it manually, notice the issue, and carry on....
  Mmtrix::Agent.instance.error_collector. \
    notice_agent_error(StatsHashLookupError.new(error, hash, key))
  stats = Mmtrix::Agent::Stats.new
  hash[key] = stats
  # Try to restore the default_proc so we won't continually trip the error
  if hash.respond_to?(:default_proc=)
    hash.default_proc = Proc.new { |h, k| h[k] = Mmtrix::Agent::Stats.new }
  end
  stats
end
marshal_dump() click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 42
def marshal_dump
  [@started_at, Hash[@scoped], Hash[@unscoped]]
end
marshal_load(data) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 46
def marshal_load(data)
  @started_at = data.shift
  @scoped   = Hash.new { |h, k| h[k] = Mmtrix::Agent::Stats.new }
  @unscoped = Hash.new { |h, k| h[k] = Mmtrix::Agent::Stats.new }
  @scoped.merge!(data.shift)
  @unscoped.merge!(data.shift)
end
merge!(other) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 136
def merge!(other)
  @started_at = other.started_at if other.started_at < @started_at

  other.each do |spec, val|
    if spec.scope.empty?
      merge_or_insert(@unscoped, spec.name, val)
    else
      merge_or_insert(@scoped, spec, val)
    end
  end
  self
end
merge_or_insert(target, name, stats) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 159
def merge_or_insert(target, name, stats)
  if target.has_key?(name)
    target[name].merge!(stats)
  else
    target[name] = stats
  end
end
merge_transaction_metrics!(txn_metrics, scope) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 149
def merge_transaction_metrics!(txn_metrics, scope)
  txn_metrics.each_unscoped do |name, stats|
    merge_or_insert(@unscoped, name, stats)
  end
  txn_metrics.each_scoped do |name, stats|
    spec = Mmtrix::MetricSpec.new(name, scope)
    merge_or_insert(@scoped, spec, stats)
  end
end
record(metric_specs, value=nil, aux=nil, &blk) click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 102
def record(metric_specs, value=nil, aux=nil, &blk)
  Array(metric_specs).each do |metric_spec|
    if metric_spec.scope.empty?
      key = metric_spec.name
      hash = @unscoped
    else
      key = metric_spec
      hash = @scoped
    end

    begin
      stats = hash[key]
    rescue NoMethodError => e
      stats = handle_stats_lookup_error(key, hash, e)
    end

    stats.record(value, aux, &blk)
  end
end
size() click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 92
def size
  @unscoped.size + @scoped.size
end
to_h() click to toggle source
# File lib/mmtrix/agent/stats_engine/stats_hash.rb, line 58
def to_h
  hash = {}
  @scoped.each   { |k, v| hash[k] = v }
  @unscoped.each { |k, v| hash[Mmtrix::MetricSpec.new(k)] = v }
  hash
end