Class: Mammoth::DispatchMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/dispatch_metrics.rb

Overview

Thread-safe in-process counters populated by Mammoth's CDC core observer.

The registry deliberately stores CDC core metric names and tags. Rendering those counters for a specific backend remains an observability concern.

Constant Summary collapse

INSTANCE =

Process-wide dispatch metrics used by the default observer and snapshot.

new

Instance Method Summary collapse

Constructor Details

#initializeDispatchMetrics

Create an empty dispatch metric registry.



10
11
12
13
# File 'lib/mammoth/dispatch_metrics.rb', line 10

def initialize
  @mutex = Mutex.new
  @counters = Hash.new(0)
end

Instance Method Details

#increment(name, tags = {}) ⇒ Integer

Increment a canonical CDC core metric.

Parameters:

  • name (String)

    canonical CDC core metric name

  • tags (Hash) (defaults to: {})

    canonical metric tags

Returns:

  • (Integer)

    updated counter value



23
24
25
26
# File 'lib/mammoth/dispatch_metrics.rb', line 23

def increment(name, tags = {})
  key = [name.to_s.freeze, normalized_tags(tags)]
  @mutex.synchronize { @counters[key] += 1 }
end

#reset!self

Clear all counters.

Intended for process lifecycle management and isolated tests.

Returns:

  • (self)

    cleared registry



44
45
46
47
# File 'lib/mammoth/dispatch_metrics.rb', line 44

def reset!
  @mutex.synchronize { @counters.clear }
  self
end

#snapshotArray<Hash>

Return an immutable point-in-time copy of all counters.

Returns:

  • (Array<Hash>)

    metric entries with name, tags, and value



31
32
33
34
35
36
37
# File 'lib/mammoth/dispatch_metrics.rb', line 31

def snapshot
  @mutex.synchronize do
    @counters.map do |(name, tags), value|
      { name: name, tags: tags.to_h, value: value }
    end
  end
end