Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class Push

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

def initialize(job, instance = nil, gateway = nil, **kwargs)
def initialize(job:, instance: nil, gateway: DEFAULT_GATEWAY, **kwargs)
raise ArgumentError, "job cannot be nil" if job.nil?
raise ArgumentError, "job cannot be empty" if job.empty?
Comment thread
Sinjo marked this conversation as resolved.

@mutex = Mutex.new
@job = job
@instance = instance
Expand Down
26 changes: 19 additions & 7 deletions spec/prometheus/client/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@
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, open_timeout: 5, read_timeout: 30) }
let(:push) { Prometheus::Client::Push.new(job: 'test-job', gateway: gateway, open_timeout: 5, read_timeout: 30) }

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 nil' do
expect do
Prometheus::Client::Push.new(job: nil)
end.to raise_error ArgumentError
end

it 'raises an ArgumentError if the job is empty' do
expect do
Prometheus::Client::Push.new(job: "")
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 @@ -59,19 +71,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