Skip to content

Commit e067f95

Browse files
committed
Make all registry methods thread safe
The registry is backed by a Hash, which is not guaranteed to be thread safe on all interpreters. For piece of mind, this change synchronizes all accesses to the metrics hash. Another option would have been to use a [thread-safe Hash][1] instead of a Hash but this would have meant adding Ruby Concurrent as a dependency, which I'm assuming we don't want. Ref: #184 (comment) [1]: https://github.com/ruby-concurrency/concurrent-ruby/blob/v1.1.7/lib/concurrent-ruby/concurrent/hash.rb Signed-off-by: Matthieu Prat <matthieuprat@gocardless.com>
1 parent 872a8eb commit e067f95

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/prometheus/client/registry.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def register(metric)
2222
name = metric.name
2323

2424
@mutex.synchronize do
25-
if exist?(name.to_sym)
25+
if @metrics.key?(name.to_sym)
2626
raise AlreadyRegisteredError, "#{name} has already been registered"
2727
end
2828
@metrics[name.to_sym] = metric
@@ -73,15 +73,15 @@ def histogram(name, docstring:, labels: [], preset_labels: {},
7373
end
7474

7575
def exist?(name)
76-
@metrics.key?(name)
76+
@mutex.synchronize { @metrics.key?(name) }
7777
end
7878

7979
def get(name)
80-
@metrics[name.to_sym]
80+
@mutex.synchronize { @metrics[name.to_sym] }
8181
end
8282

8383
def metrics
84-
@metrics.values
84+
@mutex.synchronize { @metrics.values }
8585
end
8686
end
8787
end

0 commit comments

Comments
 (0)