Class: TingYun::Agent::Collector::StatsHash

Inherits:
Object
  • Object
show all
Defined in:
lib/ting_yun/agent/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.



38
39
40
41
42
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 38

def initialize(started_at=Time.now)
  @started_at = started_at.to_f
  @scoped = Hash.new { |h, k| h[k] = TingYun::Metrics::Stats.new }
  @unscoped = Hash.new { |h, k| h[k] = TingYun::Metrics::Stats.new }
end

Instance Attribute Details

#harvested_atObject

Returns the value of attribute harvested_at.



36
37
38
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 36

def harvested_at
  @harvested_at
end

#started_atObject

Returns the value of attribute started_at.



36
37
38
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 36

def started_at
  @started_at
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 56

def ==(other)
  self.to_h == other.to_h
end

#[](key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 67

def [](key)
  case key
    when String
      @unscoped[key]
    when TingYun::Metrics::MetricSpec
      if key.scope.empty?
        @unscoped[key.name]
      else
        @scoped[key]
      end
  end
end

#eachObject



80
81
82
83
84
85
86
87
88
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 80

def each
  @scoped.each do |k, v|
    yield k, v
  end
  @unscoped.each do |k, v|
    spec = TingYun::Metrics::MetricSpec.new(k)
    yield spec, v
  end
end

#empty?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 90

def empty?
  @unscoped.empty? && @scoped.empty?
end

#handle_stats_lookup_error(key, hash, error) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 156

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....
  ::TingYun::Agent.instance.error_collector. \
  notice_agent_error(StatsHashLookupError.new(error, key))
  stats = TingYun::Metrics::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] = TingYun::Metrics::Stats.new }
  end
  stats
end

#marshal_dumpObject



44
45
46
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 44

def marshal_dump
  [@started_at, Hash[@scoped], Hash[@unscoped]]
end

#marshal_load(data) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 48

def marshal_load(data)
  @started_at = data.shift
  @scoped = Hash.new { |h, k| h[k] = TingYun::Metrics::Stats.new }
  @unscoped = Hash.new { |h, k| h[k] = TingYun::Metrics::Stats.new }
  @scoped.merge!(data.shift)
  @unscoped.merge!(data.shift)
end

#merge!(other) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 98

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) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 148

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) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 137

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 = TingYun::Metrics::MetricSpec.new(name, scope)
    merge_or_insert(@scoped, spec, stats)
  end
end

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 117

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

#sizeObject



94
95
96
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 94

def size
  @unscoped.size + @scoped.size
end

#to_hObject



60
61
62
63
64
65
# File 'lib/ting_yun/agent/collector/stats_engine/stats_hash.rb', line 60

def to_h
  hash = {}
  @scoped.each { |k, v| hash[k] = v }
  @unscoped.each { |k, v| hash[TingYun::Metrics::MetricSpec.new(k)] = v }
  hash
end