require 'spec_helper'
require 'fluent/test/driver/filter'
require 'fluent/plugin/filter_prometheus'
require_relative 'shared'
describe Fluent::Plugin::PrometheusFilter do
let(:tag) { 'prometheus.test' }
let(:driver) { Fluent::Test::Driver::Filter.new(Fluent::Plugin::PrometheusFilter).configure(config) }
let(:registry) { ::Prometheus::Client::Registry.new }
before do
allow(Prometheus::Client).to receive(:registry).and_return(registry)
end
describe '#configure' do
it_behaves_like 'output configuration'
end
describe '#run' do
let(:message) { {"foo" => 100, "bar" => 100, "baz" => 100, "qux" => 10} }
context 'simple config' do
let(:config) {
BASE_CONFIG + %(
name simple
type counter
desc Something foo.
key foo
)
}
it 'adds a new counter metric' do
expect(registry.metrics.map(&:name)).not_to eq([:simple])
driver.run(default_tag: tag) { driver.feed(event_time, message) }
expect(registry.metrics.map(&:name)).to eq([:simple])
end
it 'should keep original message' do
driver.run(default_tag: tag) { driver.feed(event_time, message) }
expect(driver.filtered_records.first).to eq(message)
end
end
it_behaves_like 'instruments record'
end
describe '#run with retention' do
let(:message) { { "foo" => 100, "bar" => 100, "baz" => 100, "qux" => 10 } }
context 'config with retention 1' do
let(:config) {
BASE_CONFIG + %(
name simple
type counter
desc Something foo.
key foo
bar ${bar}
baz ${baz}
qux ${qux}
retention 1
retention_check_interval 1
)
}
it 'expires metric after max 2s' do
expect(registry.metrics.map(&:name)).not_to eq([:simple])
driver.run(default_tag: tag) {
driver.feed(event_time, message)
expect(registry.metrics[0].get(labels: { :bar => 100, :baz => 100, :qux => 10 })).to eq(100)
sleep(2)
expect(registry.metrics[0].get(labels: { :bar => 100, :baz => 100, :qux => 10 })).to eq(0.0)
}
end
end
end
describe '#run with topk' do
let(:message1) { { "foo" => 200, "bar" => "a" } }
let(:message2) { { "foo" => 300, "bar" => "b" } }
let(:message3) { { "foo" => 100, "bar" => "c" } }
context 'config with topk 2' do
let(:config) {
BASE_CONFIG + %(
name simple
type counter
desc Something foo.
key foo
bar ${bar}
topk 2
)
}
it 'shows only top 2 metrics' do
expect(registry.metrics.map(&:name)).not_to eq([:simple])
driver.run(default_tag: tag) {
driver.feed(event_time, message1)
driver.feed(event_time, message2)
driver.feed(event_time, message3)
}
expect(registry.metrics[0].values).to eq({
{ :bar => "a" } => 200,
{ :bar => "b" } => 300,
})
end
end
end
describe '#run with reset_after' do
let(:message) { { "foo" => 100 } }
context 'config with reset_after 1' do
let(:config) {
BASE_CONFIG + %(
name simple
type counter
desc Something foo.
key foo
reset_after 1
)
}
it 'resets metric values after max 2s' do
expect(registry.metrics.map(&:name)).not_to eq([:simple])
driver.run(default_tag: tag) {
driver.feed(event_time, message)
sleep(2)
}
expect(registry.metrics[0].values).to eq({ {} => 0 })
end
end
end
end