Class: Async::Utilization::Registry
- Inherits:
-
Object
- Object
- Async::Utilization::Registry
- 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.
Instance Attribute Summary collapse
-
#observer ⇒ Object
Returns the value of attribute observer.
Instance Method Summary collapse
-
#initialize ⇒ Registry
constructor
Initialize a new registry.
-
#metric(field) ⇒ Object
Get a cached metric reference for a field.
-
#namespace(name) ⇒ Object
Get a namespace view of this registry.
- #The registered observer.=(registeredobserver. = (value)) ⇒ Object
-
#values ⇒ Object
Get the current values for all metrics.
Constructor Details
#initialize ⇒ Registry
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
#observer ⇒ Object
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 |
#values ⇒ Object
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 |