Skip to content

Commit

Permalink
Convert pushgateway client to keyword arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Sinjakli <chris@sinjakli.co.uk>
  • Loading branch information
Sinjo committed Mar 27, 2021
1 parent 100b55e commit d177e66
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
6 changes: 2 additions & 4 deletions lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ class Push

attr_reader :job, :instance, :gateway, :path

def initialize(job, instance = nil, gateway = nil)
unless job
raise ArgumentError, "job cannot be nil"
end
def initialize(job:, instance: nil, gateway: DEFAULT_GATEWAY)
raise ArgumentError, "job cannot be nil" if job.nil?

@mutex = Mutex.new
@job = job
Expand Down
18 changes: 9 additions & 9 deletions spec/prometheus/client/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@
describe Prometheus::Client::Push do
let(:gateway) { 'http://localhost:9091' }
let(:registry) { Prometheus::Client.registry }
let(:push) { Prometheus::Client::Push.new('test-job', nil, gateway) }
let(:push) { Prometheus::Client::Push.new(job: 'test-job', gateway: gateway) }

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

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

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

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

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

it 'raises an ArgumentError if the job is not provided' do
it 'raises an ArgumentError if the job is nil' do
expect do
Prometheus::Client::Push.new(nil)
Prometheus::Client::Push.new(job: nil)
end.to raise_error ArgumentError
end

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

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

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

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

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

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

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

0 comments on commit d177e66

Please sign in to comment.