Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all registry methods thread safe #201

Merged

Conversation

matthieuprat
Copy link
Contributor

The registry is backed by a Hash, which is not guaranteed to be thread
safe on all interpreters. For peace of mind, this change synchronizes
all accesses to the metrics hash.

Another option would have been to use a thread-safe Hash 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)

@coveralls
Copy link

coveralls commented Aug 25, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling c3836b2 on matthieuprat:mprat/resgistry-thread-safety into 872a8eb on prometheus:master.

@@ -22,7 +22,7 @@ def register(metric)
name = metric.name

@mutex.synchronize do
if exist?(name.to_sym)
if @metrics.key?(name.to_sym)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change because Mutex is not re-entrant. I guess we could use a Monitor instead but inlining the exist? method actually makes the code slightly more readable in my opinion.

The registry is backed by a Hash, which is not guaranteed to be thread
safe on all interpreters. For peace 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: prometheus#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>
@matthieuprat matthieuprat force-pushed the mprat/resgistry-thread-safety branch from e067f95 to c3836b2 Compare August 25, 2020 14:53
@dmagliola
Copy link
Collaborator

Sorry I somehow missed this for a month and a half!

@dmagliola dmagliola merged commit 726536c into prometheus:master Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants