Skip to content

Commit

Permalink
Remove instance as a special grouping key label in push client
Browse files Browse the repository at this point in the history
This is in preparation for the introduction of arbitrary labels in the
grouping key. We no longer need to support `instance` as a special case,
and will instead generate a path that combines the job label with
everything passed in a grouping key hash.

Signed-off-by: Chris Sinjakli <chris@sinjakli.co.uk>
  • Loading branch information
Sinjo committed Nov 21, 2021
1 parent 7d9d45f commit 5edbaa6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
16 changes: 5 additions & 11 deletions lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ module Client
class Push
DEFAULT_GATEWAY = 'http://localhost:9091'.freeze
PATH = '/metrics/job/%s'.freeze
INSTANCE_PATH = '/metrics/job/%s/instance/%s'.freeze
SUPPORTED_SCHEMES = %w(http https).freeze

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

def initialize(job:, instance: nil, gateway: DEFAULT_GATEWAY, **kwargs)
def initialize(job:, gateway: DEFAULT_GATEWAY, **kwargs)
raise ArgumentError, "job cannot be nil" if job.nil?
raise ArgumentError, "job cannot be empty" if job.empty?

@mutex = Mutex.new
@job = job
@instance = instance
@gateway = gateway || DEFAULT_GATEWAY
@path = build_path(job, instance)
@path = build_path(job)
@uri = parse("#{@gateway}#{@path}")

@http = Net::HTTP.new(@uri.host, @uri.port)
Expand Down Expand Up @@ -70,12 +68,8 @@ def parse(url)
raise ArgumentError, "#{url} is not a valid URL: #{e}"
end

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

def request(req_class, registry = nil)
Expand Down
16 changes: 2 additions & 14 deletions spec/prometheus/client/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,10 @@
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(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(job: 'bar job', instance: 'foo <my instance>')
push = Prometheus::Client::Push.new(job: '<bar job>')

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

0 comments on commit 5edbaa6

Please sign in to comment.