diff --git a/lib/fluent/plugin/prometheus.rb b/lib/fluent/plugin/prometheus.rb index 75ffb9c..73a4740 100644 --- a/lib/fluent/plugin/prometheus.rb +++ b/lib/fluent/plugin/prometheus.rb @@ -656,7 +656,12 @@ 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 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 @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..468e1e6 --- /dev/null +++ b/spec/fluent/plugin/prometheus/histogram_spec.rb @@ -0,0 +1,50 @@ +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 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 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 eql([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