From 8fb4c919f02b98f57d19eeb6e587895537603e19 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:12:26 +0000 Subject: [PATCH 1/2] Parse histogram buckets such as .005 and 1e-3 as numbers The buckets parameter turned any value which did not look like "digits.digits" into an integer with String#to_i. ".005, .01, .025" became [0, 0, 0], so every observation fell into the +Inf bucket, and "1e-3" became 1. A value which is not a number became 0 instead of being refused. Parse a bucket with Integer or Float, and raise ConfigError for a value which is not a number. Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> --- lib/fluent/plugin/prometheus.rb | 5 ++- .../plugin/prometheus/histogram_spec.rb | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 spec/fluent/plugin/prometheus/histogram_spec.rb diff --git a/lib/fluent/plugin/prometheus.rb b/lib/fluent/plugin/prometheus.rb index 75ffb9c..4cde46d 100644 --- a/lib/fluent/plugin/prometheus.rb +++ b/lib/fluent/plugin/prometheus.rb @@ -656,7 +656,10 @@ def initialize(element, registry, labels, opts = {}) begin if element['buckets'] buckets = element['buckets'].split(/,/).map(&:strip).map do |e| - e[/\A\d+.\d+\Z/] ? e.to_f : e.to_i + # keep an integer as is, so that its `le` label stays "1", not "1.0" + e.match?(/\A\d+\z/) ? Integer(e, 10) : Float(e) + rescue ArgumentError + raise ConfigError, "buckets in #{element['name']} must be numbers: #{element['buckets']}" end @histogram = registry.histogram(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys, buckets: buckets) else diff --git a/spec/fluent/plugin/prometheus/histogram_spec.rb b/spec/fluent/plugin/prometheus/histogram_spec.rb new file mode 100644 index 0000000..6872eda --- /dev/null +++ b/spec/fluent/plugin/prometheus/histogram_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Fluent::Plugin::Prometheus::Histogram do + let(:registry) { ::Prometheus::Client::Registry.new } + + def build_histogram(buckets) + element = Fluent::Config::Element.new( + 'metric', '', + { + 'name' => 'histogram_buckets', + 'type' => 'histogram', + 'desc' => 'Something foo.', + 'key' => 'foo', + 'buckets' => buckets, + }, + [] + ) + Fluent::Plugin::Prometheus::Histogram.new(element, registry, {}, {}) + end + + def buckets_of(buckets) + build_histogram(buckets) + registry.get(:histogram_buckets).buckets + end + + it 'keeps integer and decimal buckets' do + expect(buckets_of('0.1, 1, 5, 10')).to eq([0.1, 1, 5, 10]) + end + + it 'parses decimal buckets without a leading zero' do + expect(buckets_of('.005, .01, .025')).to eq([0.005, 0.01, 0.025]) + end + + it 'parses buckets in exponent notation' do + expect(buckets_of('1e-3, 1e-2, 1.5e2')).to eq([0.001, 0.01, 150.0]) + end + + it 'refuses a bucket which is not a number' do + expect { build_histogram('0.1, abc') } + .to raise_error(Fluent::ConfigError, /buckets in histogram_buckets must be numbers/) + end +end From be8b6c50c6077222afa2d54dd253e323eee26ec9 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:16:30 +0000 Subject: [PATCH 2/2] Keep the le labels of existing integer buckets The previous change parsed every all-digit bucket as an Integer. On master, buckets of three or more digits such as 100 matched the unescaped "digits.digits" regexp and became floats, and signed integers such as -5 stayed integers. Their le labels changed from "100.0" to "100" and from "-5" to "-5.0", which creates new series on Prometheus 2. Keep the old result when it equals the parsed Float value, and use eql in the specs so that a change between 1 and 1.0 fails them. Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> --- lib/fluent/plugin/prometheus.rb | 6 ++++-- spec/fluent/plugin/prometheus/histogram_spec.rb | 14 +++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/fluent/plugin/prometheus.rb b/lib/fluent/plugin/prometheus.rb index 4cde46d..73a4740 100644 --- a/lib/fluent/plugin/prometheus.rb +++ b/lib/fluent/plugin/prometheus.rb @@ -656,8 +656,10 @@ def initialize(element, registry, labels, opts = {}) begin if element['buckets'] buckets = element['buckets'].split(/,/).map(&:strip).map do |e| - # keep an integer as is, so that its `le` label stays "1", not "1.0" - e.match?(/\A\d+\z/) ? Integer(e, 10) : Float(e) + # keep the old result if it is right, so that existing `le` labels such as "100.0" stay the same + value = Float(e) + legacy = e[/\A\d+.\d+\Z/] ? e.to_f : e.to_i + legacy == value ? legacy : value rescue ArgumentError raise ConfigError, "buckets in #{element['name']} must be numbers: #{element['buckets']}" end diff --git a/spec/fluent/plugin/prometheus/histogram_spec.rb b/spec/fluent/plugin/prometheus/histogram_spec.rb index 6872eda..468e1e6 100644 --- a/spec/fluent/plugin/prometheus/histogram_spec.rb +++ b/spec/fluent/plugin/prometheus/histogram_spec.rb @@ -24,15 +24,23 @@ def buckets_of(buckets) end it 'keeps integer and decimal buckets' do - expect(buckets_of('0.1, 1, 5, 10')).to eq([0.1, 1, 5, 10]) + expect(buckets_of('0.1, 1, 5, 10')).to eql([0.1, 1, 5, 10]) + end + + it 'keeps buckets of three or more digits as floats, as before' do + expect(buckets_of('10, 100, 1000')).to eql([10, 100.0, 1000.0]) + end + + it 'keeps signed integer buckets as integers' do + expect(buckets_of('-5, 0, +5')).to eql([-5, 0, 5]) end it 'parses decimal buckets without a leading zero' do - expect(buckets_of('.005, .01, .025')).to eq([0.005, 0.01, 0.025]) + expect(buckets_of('.005, .01, .025')).to eql([0.005, 0.01, 0.025]) end it 'parses buckets in exponent notation' do - expect(buckets_of('1e-3, 1e-2, 1.5e2')).to eq([0.001, 0.01, 150.0]) + expect(buckets_of('1e-3, 1e-2, 1.5e2')).to eql([0.001, 0.01, 150.0]) end it 'refuses a bucket which is not a number' do