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.



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

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.



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

def short_stat
  @short_stat
end

#statsObject

Returns the value of attribute stats.



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

def stats
  @stats
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

#exclusive_statsObject



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

def exclusive_stats
  return nil unless @exclusives

  stats.to_h { |key, value| [key, value - (@exclusives[key] || 0.0)] }
end

#reportObject



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

def report
  return unless 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

#subtract_exclusives(stats) ⇒ Object



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

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