Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/fluent/plugin/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <metric> #{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
Expand Down
50 changes: 50 additions & 0 deletions spec/fluent/plugin/prometheus/histogram_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +26 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHOULD: eq cannot catch the le change in lib/fluent/plugin/prometheus.rb, because [1.0] == [1] is true in Ruby. Please use eql and add the cases from that comment. With this PR as is, the two new examples fail:

Suggested change
it 'keeps integer and decimal buckets' do
expect(buckets_of('0.1, 1, 5, 10')).to eq([0.1, 1, 5, 10])
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in be8b6c5. The examples use eql, and the two new ones fail on the previous commit and pass now. bundle exec rspec passes (184 examples).


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 <metric> histogram_buckets must be numbers/)
end
end
Loading