Class: InstStatsd::Counter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, blocked_names = [], tags: {}, short_stat: nil) ⇒ Counter

Returns a new instance of Counter.



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

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

Instance Attribute Details

#blocked_names ⇒ Object (readonly)

Returns the value of attribute blocked_names.



16
17
18
# File 'lib/inst_statsd/counter.rb', line 16

def blocked_names
  @blocked_names
end

#key ⇒ Object (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/inst_statsd/counter.rb', line 15

def key
  @key
end

Class Method Details

.counters ⇒ Object



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

def counters
  @counters ||= {}
end

.register(counter) ⇒ Object



10
11
12
# File 'lib/inst_statsd/counter.rb', line 10

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

Instance Method Details

#accepted_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/inst_statsd/counter.rb', line 41

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

#count ⇒ Object



37
38
39
# File 'lib/inst_statsd/counter.rb', line 37

def count
  Thread.current[@tls_key]
end

#finalize_count(cookie) ⇒ Object



33
34
35
# File 'lib/inst_statsd/counter.rb', line 33

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

#start ⇒ Object



25
26
27
# File 'lib/inst_statsd/counter.rb', line 25

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

#track(name) ⇒ Object



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

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