Class: InstStatsd::BlockTracking

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.track(key, category: nil, statsd: InstStatsd::Statsd, only: nil, tags: {}, short_stat: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inst_statsd/block_tracking.rb', line 22

def track(key, category: nil, statsd: InstStatsd::Statsd, only: nil, tags: {}, short_stat: nil)
  return yield if mask && mask !~ key
  return yield if negative_mask && negative_mask =~ key

  cookies = if only
              Array(only).map { |name| [name, Counter.counters[name].start] }
            else
              Counter.counters.map { |(name, counter)| [name, counter.start] }
            end
  block_stat = InstStatsd::BlockStat.new(key, statsd, tags: tags, short_stat: short_stat)
  stack(category).push(block_stat) if category

  result = nil
  elapsed = Benchmark.realtime do
    result = yield
  end
  # to be consistent with ActionPack, measure in milliseconds
  elapsed *= 1000

  block_stat.stats = cookies.to_h { |(name, cookie)| [name, Counter.counters[name].finalize_count(cookie)] }
  block_stat.stats["total"] = elapsed
  # we need to make sure to report exclusive timings, even if nobody called us re-entrantly
  block_stat.subtract_exclusives({}) if category
  block_stat.report
  logger&.log(block_stat, "STATSD #{key}")
  # -1 is ourselves; we want to subtract from the block above us
  stack(category)[-2].subtract_exclusives(block_stat.stats) if category && stack(category)[-2]

  result
ensure
  stack(category).pop if category && stack(category).last == block_stat
end