Class: Async::Utilization::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/async/utilization/registry.rb

Overview

Registry for emitting utilization metrics.

The registry tracks values directly and notifies a registered observer when values change. The observer (like Observer) can write to its backend.

Registries should be explicitly created and passed to components that need them. In service contexts, registries are typically created via the evaluator and shared across components within the same service instance.

When an observer is added, it is immediately notified of all current values so it can sync its state. When values change, the observer is notified.

Examples:

Create a registry and emit metrics:

registry = Async::Utilization::Registry.new

total_requests = registry.metric(:total_requests)
total_requests.increment

active_requests = registry.metric(:active_requests)
active_requests.track do
	# Handle request - auto-decrements when block completes
end

# Add shared memory observer when supervisor connects
# Observer will be notified of all current values automatically
schema = Async::Utilization::Schema.build(
	total_requests: :u64,
	active_requests: :u32
)
observer = Async::Utilization::Observer.open(schema, "/path/to/shm", 4096, 0)
registry.observer = observer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Initialize a new registry.



44
45
46
47
48
49
# File 'lib/async/utilization/registry.rb', line 44

def initialize
	@observer = nil
	@metrics = {}
	
	@guard = Mutex.new
end

Instance Attribute Details

#observerObject

Returns the value of attribute observer.



52
53
54
# File 'lib/async/utilization/registry.rb', line 52

def observer
  @observer
end

Instance Method Details

#metric(field) ⇒ Object

Get a cached metric reference for a field.

Returns a Metric instance that caches all details needed for fast writes. Metrics are cached per field and invalidated when the observer changes.



90
91
92
93
94
95
96
# File 'lib/async/utilization/registry.rb', line 90

def metric(field)
	field = field.to_sym
	
	@guard.synchronize do
		@metrics[field] ||= Metric.for(field, @observer)
	end
end

#namespace(name) ⇒ Object

Get a namespace view of this registry.



102
103
104
# File 'lib/async/utilization/registry.rb', line 102

def namespace(name)
	Namespace.new(self, name)
end

#The registered observer.=(registeredobserver. = (value)) ⇒ Object



52
# File 'lib/async/utilization/registry.rb', line 52

attr :observer

#valuesObject

Get the current values for all metrics.



57
58
59
60
61
# File 'lib/async/utilization/registry.rb', line 57

def values
	@metrics.transform_values do |metric|
		metric.value
	end
end