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.



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

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_namesObject (readonly)

Returns the value of attribute blocked_names.



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

def blocked_names
  @blocked_names
end

#keyObject (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

.countersObject



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)


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

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

#countObject



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

def count
  Thread.current[@tls_key]
end

#finalize_count(cookie) ⇒ Object



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

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

#startObject



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

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

#track(name) ⇒ Object



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

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