diff --git a/lib/prometheus/client/registry.rb b/lib/prometheus/client/registry.rb index 4bf63aa4..0b2f6e9a 100644 --- a/lib/prometheus/client/registry.rb +++ b/lib/prometheus/client/registry.rb @@ -22,7 +22,7 @@ def register(metric) name = metric.name @mutex.synchronize do - if exist?(name.to_sym) + if @metrics.key?(name.to_sym) raise AlreadyRegisteredError, "#{name} has already been registered" end @metrics[name.to_sym] = metric @@ -73,15 +73,15 @@ def histogram(name, docstring:, labels: [], preset_labels: {}, end def exist?(name) - @metrics.key?(name) + @mutex.synchronize { @metrics.key?(name) } end def get(name) - @metrics[name.to_sym] + @mutex.synchronize { @metrics[name.to_sym] } end def metrics - @metrics.values + @mutex.synchronize { @metrics.values } end end end diff --git a/spec/prometheus/client/registry_spec.rb b/spec/prometheus/client/registry_spec.rb index 32727d00..3c4da190 100644 --- a/spec/prometheus/client/registry_spec.rb +++ b/spec/prometheus/client/registry_spec.rb @@ -33,10 +33,6 @@ mutex = Mutex.new containers = [] - def registry.exist?(*args) - super.tap { sleep(0.01) } - end - Array.new(5) do Thread.new do result = begin