Skip to content

Commit 8dc02e4

Browse files
authored
Merge pull request #291 from prometheus/sinjo-base64-job-push
Handle `/` in job name in `Prometheus::Client::Push`
2 parents 77e6b7b + 6f7b6cf commit 8dc02e4

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/prometheus/client/push.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HttpClientError < HttpError; end
2323
class HttpServerError < HttpError; end
2424

2525
DEFAULT_GATEWAY = 'http://localhost:9091'.freeze
26-
PATH = '/metrics/job/%s'.freeze
26+
PATH = '/metrics'.freeze
2727
SUPPORTED_SCHEMES = %w(http https).freeze
2828

2929
attr_reader :job, :gateway, :path
@@ -87,7 +87,14 @@ def parse(url)
8787
end
8888

8989
def build_path(job, grouping_key)
90-
path = format(PATH, ERB::Util::url_encode(job))
90+
# Job can't be empty, but it can contain `/`, so we need to base64
91+
# encode it in that case
92+
if job.include?('/')
93+
encoded_job = Base64.urlsafe_encode64(job)
94+
path = "#{PATH}/job@base64/#{encoded_job}"
95+
else
96+
path = "#{PATH}/job/#{ERB::Util::url_encode(job)}"
97+
end
9198

9299
grouping_key.each do |label, value|
93100
if value.include?('/')

spec/prometheus/client/push_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
expect(push.path).to eql('/metrics/job/test-job/foo/bar/baz/qux')
9494
end
9595

96+
it 'encodes the job name in url-safe base64 if it contains `/`' do
97+
push = Prometheus::Client::Push.new(
98+
job: 'foo/test-job',
99+
)
100+
101+
expect(push.path).to eql('/metrics/job@base64/Zm9vL3Rlc3Qtam9i')
102+
end
103+
96104
it 'encodes grouping key label values containing `/` in url-safe base64' do
97105
push = Prometheus::Client::Push.new(
98106
job: 'test-job',

0 commit comments

Comments
 (0)