Class: Labkit::Metrics::Registry
- Inherits:
-
Object
- Object
- Labkit::Metrics::Registry
- Defined in:
- lib/labkit/metrics/registry.rb
Overview
A thin wrapper around Prometheus::Client::Registry. It provides a thread-safe way to register metrics with the Prometheus registry.
Constant Summary collapse
- INIT_REGISTRY_MUTEX =
Mutex.new
- REGISTER_MUTEX =
Mutex.new
Class Method Summary collapse
-
.get(metric_name) ⇒ Prometheus::Client::Metric?
Returns the metric for the given name from the Prometheus registry.
-
.reset! ⇒ Object
Cleans up the Prometheus registry and resets it to a new state.
-
.safe_register(metric_type, name, *args) ⇒ Prometheus::Client::Metric
Registers a metric with the Prometheus registry in a thread-safe manner.
Class Method Details
.get(metric_name) ⇒ Prometheus::Client::Metric?
Returns the metric for the given name from the Prometheus registry.
50 51 52 |
# File 'lib/labkit/metrics/registry.rb', line 50 def get(metric_name) wrapped_registry.get(metric_name) end |
.reset! ⇒ Object
Cleans up the Prometheus registry and resets it to a new state.
38 39 40 41 42 43 44 |
# File 'lib/labkit/metrics/registry.rb', line 38 def reset! INIT_REGISTRY_MUTEX.synchronize do Prometheus::Client.cleanup! Prometheus::Client.reset! @registry = nil end end |
.safe_register(metric_type, name, *args) ⇒ Prometheus::Client::Metric
Registers a metric with the Prometheus registry in a thread-safe manner. If the metric already exists, it returns the existing metric. If the metric does not exist, it creates a new one. Each metric-name is only registered once for a type (counter, gauge, histogram, summary), even if multiple threads attempt to register the same metric simultaneously.
31 32 33 34 35 |
# File 'lib/labkit/metrics/registry.rb', line 31 def safe_register(metric_type, name, *args) REGISTER_MUTEX.synchronize do get(name) || wrapped_registry.method(metric_type).call(name, *args) end end |