From ca53b572f61dc40c6e5ebeae920f7a08d1970c0b Mon Sep 17 00:00:00 2001 From: Chris Sinjakli Date: Sat, 7 Oct 2023 15:49:04 +0100 Subject: [PATCH 1/2] Stringify non-string job names in push client We do this to all other label values in the client and we should make this one consistent. Right now it's particularly surprising that passing a symbol as the job name results in an error like: ``` undefined method `include?' for :foo:Symbol (NoMethodError) ``` Signed-off-by: Chris Sinjakli --- lib/prometheus/client/push.rb | 2 ++ spec/prometheus/client/push_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/prometheus/client/push.rb b/lib/prometheus/client/push.rb index 1490c82d..44147ae1 100644 --- a/lib/prometheus/client/push.rb +++ b/lib/prometheus/client/push.rb @@ -87,6 +87,8 @@ def parse(url) end def build_path(job, grouping_key) + job = job.to_s + # Job can't be empty, but it can contain `/`, so we need to base64 # encode it in that case if job.include?('/') diff --git a/spec/prometheus/client/push_spec.rb b/spec/prometheus/client/push_spec.rb index 4af55ad5..43890366 100644 --- a/spec/prometheus/client/push_spec.rb +++ b/spec/prometheus/client/push_spec.rb @@ -93,6 +93,14 @@ expect(push.path).to eql('/metrics/job/test-job/foo/bar/baz/qux') end + it 'converts non-string job names to strings' do + push = Prometheus::Client::Push.new( + job: :foo, + ) + + expect(push.path).to eql('/metrics/job/foo') + end + it 'encodes the job name in url-safe base64 if it contains `/`' do push = Prometheus::Client::Push.new( job: 'foo/test-job', From 2f5422c2310e0cc82ba6b837a494a621d3395be2 Mon Sep 17 00:00:00 2001 From: Chris Sinjakli Date: Sat, 7 Oct 2023 15:55:59 +0100 Subject: [PATCH 2/2] Add pending change to README.md Signed-off-by: Chris Sinjakli --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9ec9845..2e015f16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ # Unreleased changes -_None outstanding_ +## Bug fixes + +- [#296](https://github.com/prometheus/client_ruby/pull/296) Stringify non-string job + names in push client: + Previously, an error would be raised if you passed a symbol as the job name, which + is inconsistent with how we handle label values in the rest of the client. This + change converts the job name to a string before trying to use it. # 4.2.1 / 2023-08-04