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

Add Gauge#set_to_current_time #287

Merged
merged 1 commit into from
Jun 13, 2023
Merged
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
4 changes: 4 additions & 0 deletions lib/prometheus/client/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def set(value, labels: {})
@store.set(labels: label_set_for(labels), val: value)
end

def set_to_current_time(labels: {})
@store.set(labels: label_set_for(labels), val: Time.now.to_f)
end

# Increments Gauge value by 1 or adds the given value to the Gauge.
# (The value can be negative, resulting in a decrease of the Gauge.)
def increment(by: 1, labels: {})
Expand Down
1 change: 1 addition & 0 deletions prometheus-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |s|

s.add_development_dependency 'benchmark-ips'
s.add_development_dependency 'concurrent-ruby'
s.add_development_dependency 'timecop'
end
24 changes: 24 additions & 0 deletions spec/prometheus/client/gauge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@
end
end

describe '#set_to_current_time' do
it 'it sets the gauge to the current Unix epoch time' do
Timecop.freeze(Time.at(12345.1)) do
expect do
gauge.set_to_current_time
end.to change { gauge.get }.from(0).to(12345.1)
end
end

context "with a an expected label set" do
let(:expected_labels) { [:test] }

it 'sets a metric value for a given label set' do
Timecop.freeze(Time.at(12345.1)) do
expect do
expect do
gauge.set_to_current_time(labels: { test: 'value' })
end.to change { gauge.get(labels: { test: 'value' }) }.from(0).to(12345.1)
end.to_not change { gauge.get(labels: { test: 'other' }) }
end
end
end
end

describe '#increment' do
before do
gauge.set(0, labels: RSpec.current_example.metadata[:labels] || {})
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: UTF-8

require 'simplecov'
require 'timecop'

RSpec.configure do |c|
c.warnings = true
Expand All @@ -9,3 +10,5 @@
SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter

SimpleCov.start

Timecop.safe_mode = true