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.



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

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_keyObject

Returns the value of attribute common_key.



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

def common_key
  @common_key
end

#short_statObject

Returns the value of attribute short_stat.



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

def short_stat
  @short_stat
end

#statsObject

Returns the value of attribute stats.



4
5
6
# File 'lib/inst_statsd/block_stat.rb', line 4

def stats
  @stats
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

#exclusive_statsObject



26
27
28
29
# File 'lib/inst_statsd/block_stat.rb', line 26

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

#reportObject



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

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



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

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