Class: CanvasStatsd::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_statsd/counter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, blocked_names = []) ⇒ Counter



16
17
18
19
20
21
# File 'lib/canvas_statsd/counter.rb', line 16

def initialize(key, blocked_names=[])
  @blocked_names = blocked_names
  @key = key
  @tls_key = "statsd.#{key}"
  self.class.register(self)
end

Instance Attribute Details

#blocked_namesObject (readonly)

Returns the value of attribute blocked_names.



14
15
16
# File 'lib/canvas_statsd/counter.rb', line 14

def blocked_names
  @blocked_names
end

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/canvas_statsd/counter.rb', line 13

def key
  @key
end

Class Method Details

.countersObject



4
5
6
# File 'lib/canvas_statsd/counter.rb', line 4

def counters
  @counters ||= {}
end

.register(counter) ⇒ Object



8
9
10
# File 'lib/canvas_statsd/counter.rb', line 8

def register(counter)
  counters[counter.key] = counter
end

Instance Method Details

#accepted_name?(name) ⇒ Boolean



39
40
41
# File 'lib/canvas_statsd/counter.rb', line 39

def accepted_name?(name)
  !blocked_names.include?(name)
end

#countObject



35
36
37
# File 'lib/canvas_statsd/counter.rb', line 35

def count
  Thread.current[@tls_key]
end

#finalize_count(cookie) ⇒ Object



31
32
33
# File 'lib/canvas_statsd/counter.rb', line 31

def finalize_count(cookie)
  Thread.current[@tls_key] - cookie
end

#startObject



23
24
25
# File 'lib/canvas_statsd/counter.rb', line 23

def start
  Thread.current[@tls_key] ||= 0
end

#track(name) ⇒ Object



27
28
29
# File 'lib/canvas_statsd/counter.rb', line 27

def track(name)
  Thread.current[@tls_key] += 1 if Thread.current[@tls_key] && accepted_name?(name)
end