Skip to content

Commit 2484585

Browse files
committed
Convert pushgateway client to keyword arguments
1 parent 100b55e commit 2484585

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

lib/prometheus/client/push.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ class Push
2121

2222
attr_reader :job, :instance, :gateway, :path
2323

24-
def initialize(job, instance = nil, gateway = nil)
25-
unless job
26-
raise ArgumentError, "job cannot be nil"
27-
end
24+
def initialize(job:, instance: nil, gateway: DEFAULT_GATEWAY)
25+
raise ArgumentError, "job cannot be nil" if job.nil?
2826

2927
@mutex = Mutex.new
3028
@job = job

spec/prometheus/client/push_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
describe Prometheus::Client::Push do
66
let(:gateway) { 'http://localhost:9091' }
77
let(:registry) { Prometheus::Client.registry }
8-
let(:push) { Prometheus::Client::Push.new('test-job', nil, gateway) }
8+
let(:push) { Prometheus::Client::Push.new(job: 'test-job', gateway: gateway) }
99

1010
describe '.new' do
1111
it 'returns a new push instance' do
1212
expect(push).to be_a(Prometheus::Client::Push)
1313
end
1414

1515
it 'uses localhost as default Pushgateway' do
16-
push = Prometheus::Client::Push.new('test-job')
16+
push = Prometheus::Client::Push.new(job: 'test-job')
1717

1818
expect(push.gateway).to eql('http://localhost:9091')
1919
end
2020

2121
it 'allows to specify a custom Pushgateway' do
22-
push = Prometheus::Client::Push.new('test-job', nil, 'http://pu.sh:1234')
22+
push = Prometheus::Client::Push.new(job: 'test-job', gateway: 'http://pu.sh:1234')
2323

2424
expect(push.gateway).to eql('http://pu.sh:1234')
2525
end
2626

27-
it 'raises an ArgumentError if the job is not provided' do
27+
it 'raises an ArgumentError if the job is nil' do
2828
expect do
29-
Prometheus::Client::Push.new(nil)
29+
Prometheus::Client::Push.new(job: nil)
3030
end.to raise_error ArgumentError
3131
end
3232

3333
it 'raises an ArgumentError if the given gateway URL is invalid' do
3434
['inva.lid:1233', 'http://[invalid]'].each do |url|
3535
expect do
36-
Prometheus::Client::Push.new('test-job', nil, url)
36+
Prometheus::Client::Push.new(job: 'test-job', gateway: url)
3737
end.to raise_error ArgumentError
3838
end
3939
end
@@ -65,19 +65,19 @@
6565

6666
describe '#path' do
6767
it 'uses the default metrics path if no instance value given' do
68-
push = Prometheus::Client::Push.new('test-job')
68+
push = Prometheus::Client::Push.new(job: 'test-job')
6969

7070
expect(push.path).to eql('/metrics/job/test-job')
7171
end
7272

7373
it 'uses the full metrics path if an instance value is given' do
74-
push = Prometheus::Client::Push.new('bar-job', 'foo')
74+
push = Prometheus::Client::Push.new(job: 'bar-job', instance: 'foo')
7575

7676
expect(push.path).to eql('/metrics/job/bar-job/instance/foo')
7777
end
7878

7979
it 'escapes non-URL characters' do
80-
push = Prometheus::Client::Push.new('bar job', 'foo <my instance>')
80+
push = Prometheus::Client::Push.new(job: 'bar job', instance: 'foo <my instance>')
8181

8282
expected = '/metrics/job/bar+job/instance/foo+%3Cmy+instance%3E'
8383
expect(push.path).to eql(expected)

0 commit comments

Comments
 (0)