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

Improve path handling in pushgateway client #220

Merged
merged 5 commits into from
Jun 9, 2021
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
13 changes: 8 additions & 5 deletions lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'thread'
require 'net/http'
require 'uri'
require 'cgi'
require 'erb'

require 'prometheus/client'
require 'prometheus/client/formats/text'
Expand All @@ -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?
Sinjo marked this conversation as resolved.
Show resolved Hide resolved

@mutex = Mutex.new
@job = job
@instance = instance
Expand Down Expand Up @@ -68,10 +71,10 @@ def parse(url)
end

def build_path(job, instance)
if instance
format(INSTANCE_PATH, CGI::escape(job), CGI::escape(instance))
if instance && !instance.empty?
format(INSTANCE_PATH, ERB::Util::url_encode(job), ERB::Util::url_encode(instance))
else
format(PATH, CGI::escape(job))
format(PATH, ERB::Util::url_encode(job))
end
end

Expand Down
34 changes: 26 additions & 8 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,21 +71,27 @@

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 default metrics path if an empty instance value is given' do
push = Prometheus::Client::Push.new(job: 'bar-job', instance: '')

expect(push.path).to eql('/metrics/job/bar-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'
expected = '/metrics/job/bar%20job/instance/foo%20%3Cmy%20instance%3E'
expect(push.path).to eql(expected)
end
end
Expand Down