Class: InstStatsd::BlockStat

Inherits:
Object
  • Object
show all
Defined in:
lib/inst_statsd/block_stat.rb

Direct Known Subclasses

RequestStat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(common_key, statsd = InstStatsd::Statsd, tags: {}, short_stat: nil) ⇒ BlockStat

Returns a new instance of BlockStat.



11
12
13
14
15
16
17
18
# File 'lib/inst_statsd/block_stat.rb', line 11

def initialize(common_key, statsd=InstStatsd::Statsd, tags: {}, short_stat: nil)
  self.common_key = common_key
  @tags = tags
  @short_stat = short_stat
  @short_stat ||= common_key
  @statsd = statsd
  @stats = {}
end

Instance Attribute Details

#common_key ⇒ Object

Returns the value of attribute common_key.



7
8
9
# File 'lib/inst_statsd/block_stat.rb', line 7

def common_key
  @common_key
end

#short_stat ⇒ Object

Returns the value of attribute short_stat.



8
9
10
# File 'lib/inst_statsd/block_stat.rb', line 8

def short_stat
  @short_stat
end

#stats ⇒ Object

Returns the value of attribute stats.



6
7
8
# File 'lib/inst_statsd/block_stat.rb', line 6

def stats
  @stats
end

#tags ⇒ Object

Returns the value of attribute tags.



9
10
11
# File 'lib/inst_statsd/block_stat.rb', line 9

def tags
  @tags
end

Instance Method Details

#exclusive_stats ⇒ Object



28
29
30
31
# File 'lib/inst_statsd/block_stat.rb', line 28

def exclusive_stats
  return nil unless @exclusives
  stats.map { |key, value| [key, value - (@exclusives[key] || 0.0)] }.to_h
end

#report ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/inst_statsd/block_stat.rb', line 33

def report
  if common_key
    stats.each do |(key, value)|
      @statsd.timing("#{common_key}.#{key}", value, tags: @tags, short_stat: "#{@short_stat}.#{key}")
    end
    exclusive_stats&.each do |(key, value)|
      @statsd.timing("#{common_key}.exclusive.#{key}", value, tags: @tags, short_stat: "#{@short_stat}.exclusive.#{key}")
    end
  end
end

#subtract_exclusives(stats) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/inst_statsd/block_stat.rb', line 20

def subtract_exclusives(stats)
  @exclusives ||= {}
  stats.each do |(key, value)|
    @exclusives[key] ||= 0.0
    @exclusives[key] += value
  end
end