Class: Tracekit::Metrics::Counter

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

Overview

Counter metric - monotonically increasing value Used for tracking totals like request counts, error counts, etc.

Instance Method Summary collapse

Constructor Details

#initialize(name, tags, registry) ⇒ Counter

Returns a new instance of Counter.



8
9
10
11
12
13
14
# File 'lib/tracekit/metrics/counter.rb', line 8

def initialize(name, tags, registry)
  @name = name
  @tags = tags || {}
  @registry = registry
  @value = 0.0
  @mutex = Mutex.new
end

Instance Method Details

#add(value) ⇒ Object

Adds a value to the counter

Parameters:

  • value (Numeric)

    Value to add (must be non-negative)

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/tracekit/metrics/counter.rb', line 23

def add(value)
  raise ArgumentError, "Counter values must be non-negative" if value < 0

  @mutex.synchronize do
    @value += value
    @registry.record_metric(@name, "counter", @value, @tags)
  end
end

#incObject

Increments the counter by 1



17
18
19
# File 'lib/tracekit/metrics/counter.rb', line 17

def inc
  add(1.0)
end