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

Use read_lock #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/prometheus/client/data_stores/synchronized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def increment(labels:, by: 1)
end

def get(labels:)
synchronize do
@rwlock.with_read_lock do
@internal_store[labels]
end
end

def all_values
synchronize { @internal_store.dup }
@rwlock.with_read_lock do
@internal_store.dup
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/prometheus/client/data_stores/synchronized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@
metric_settings: { some_setting: true })
end.to raise_error(Prometheus::Client::DataStores::Synchronized::InvalidStoreSettingsError)
end

it '#set an #get' do
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
it '#set an #get' do
it '#set and #get' do

metric_store.set(labels: { name: 'test' }, val: 1)
expect(metric_store.get(labels: { name: 'test' })).to eq(1.0)
end

it '#set and #values' do
metric_store.set(labels: { name: 'test1' }, val: 1)
metric_store.set(labels: { name: 'test2' }, val: 2)

expect(metric_store.all_values).to eq({ { name: 'test1' } => 1.0, { name: 'test2' } => 2.0 })
end
end