Skip to content

Commit

Permalink
Updating plugin to support latest prometheus ruby client version
Browse files Browse the repository at this point in the history
Signed-off-by: AntoineC44 <[email protected]>
  • Loading branch information
AntoineC44 committed Nov 21, 2020
1 parent 5d08a89 commit ed599a4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
4 changes: 2 additions & 2 deletions fluent-plugin-prometheus.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "fluent-plugin-prometheus"
spec.version = "1.8.4"
spec.version = "1.9.0"
spec.authors = ["Masahiro Sano"]
spec.email = ["[email protected]"]
spec.summary = %q{A fluent plugin that collects metrics and exposes for Prometheus.}
Expand All @@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "fluentd", ">= 1.9.1", "< 2"
spec.add_dependency "prometheus-client", "< 0.10"
spec.add_dependency "prometheus-client", "= 2.1.0"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down
24 changes: 16 additions & 8 deletions lib/fluent/plugin/in_prometheus_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ def configure(conf)
def start
super

@buffer_newest_timekey = @registry.gauge(
@buffer_newest_timekey = get_gauge(
:fluentd_status_buffer_newest_timekey,
'Newest timekey in buffer.')
@buffer_oldest_timekey = @registry.gauge(
@buffer_oldest_timekey = get_gauge(
:fluentd_status_buffer_oldest_timekey,
'Oldest timekey in buffer.')
buffer_queue_length = @registry.gauge(
buffer_queue_length = get_gauge(
:fluentd_status_buffer_queue_length,
'Current buffer queue length.')
buffer_total_queued_size = @registry.gauge(
buffer_total_queued_size = get_gauge(
:fluentd_status_buffer_total_bytes,
'Current total size of queued buffers.')
retry_counts = @registry.gauge(
retry_counts = get_gauge(
:fluentd_status_retry_count,
'Current retry counts.')

Expand All @@ -76,14 +76,14 @@ def update_monitor_info

@monitor_info.each do |name, metric|
if info[name]
metric.set(label, info[name])
metric.set(info[name], labels: label)
end
end

timekeys = info["buffer_timekeys"]
if timekeys && !timekeys.empty?
@buffer_newest_timekey.set(label, timekeys.max)
@buffer_oldest_timekey.set(label, timekeys.min)
@buffer_newest_timekey.set(timekeys.max, labels: label)
@buffer_oldest_timekey.set(timekeys.min, labels: label)
end
end
end
Expand All @@ -95,5 +95,13 @@ def labels(plugin_info)
type: plugin_info["type"],
)
end

def get_gauge(name, docstring)
if @registry.exist?(name)
@registry.get(name)
else
@registry.gauge(name, docstring: docstring, labels: @base_labels.keys + [:plugin_id, :plugin_category, :type])
end
end
end
end
14 changes: 7 additions & 7 deletions lib/fluent/plugin/in_prometheus_output_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ def update_monitor_info
monitor_info.each do |name, metric|
if info[name]
if metric.is_a?(::Prometheus::Client::Gauge)
metric.set(label, info[name])
metric.set(info[name], labels: label)
elsif metric.is_a?(::Prometheus::Client::Counter)
metric.increment(label, info[name] - metric.get(label))
metric.increment(by: info[name] - metric.get(labels: label), labels: label)
end
end
end
Expand All @@ -172,9 +172,9 @@ def update_monitor_info
instance_vars_info.each do |name, metric|
if info['instance_variables'][name]
if metric.is_a?(::Prometheus::Client::Gauge)
metric.set(label, info['instance_variables'][name])
metric.set(info['instance_variables'][name], labels: label)
elsif metric.is_a?(::Prometheus::Client::Counter)
metric.increment(label, info['instance_variables'][name] - metric.get(label))
metric.increment(by: info['instance_variables'][name] - metric.get(labels: label), labels: label)
end
end
end
Expand All @@ -193,7 +193,7 @@ def update_monitor_info
if next_time && start_time
wait = next_time - start_time
end
@metrics[:retry_wait].set(label, wait.to_f)
@metrics[:retry_wait].set(wait.to_f, labels: label)
end
end
end
Expand All @@ -209,15 +209,15 @@ def get_gauge(name, docstring)
if @registry.exist?(name)
@registry.get(name)
else
@registry.gauge(name, docstring)
@registry.gauge(name, docstring: docstring, labels: @base_labels.keys + [:plugin_id, :type])
end
end

def get_gauge_or_counter(name, docstring)
if @registry.exist?(name)
@registry.get(name)
else
@registry.public_send(@gauge_or_counter, name, docstring)
@registry.public_send(@gauge_or_counter, name, docstring: docstring, labels: @base_labels.keys + [:plugin_id, :type])
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/fluent/plugin/in_prometheus_tail_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def update_monitor_info
# Very fragile implementation
pe = watcher.instance_variable_get(:@pe)
label = labels(info, watcher.path)
@metrics[:inode].set(label, pe.read_inode)
@metrics[:position].set(label, pe.read_pos)
@metrics[:inode].set(pe.read_inode, labels: label)
@metrics[:position].set(pe.read_pos, labels: label)
end
end
end
Expand All @@ -91,7 +91,7 @@ def get_gauge(name, docstring)
if @registry.exist?(name)
@registry.get(name)
else
@registry.gauge(name, docstring)
@registry.gauge(name, docstring: docstring, labels: @base_labels.keys + [:plugin_id, :type, :path])
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/fluent/plugin/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def initialize(element, registry, labels)
end

begin
@gauge = registry.gauge(element['name'].to_sym, element['desc'])
@gauge = registry.gauge(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys)
rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
@gauge = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :gauge, element['desc'])
end
Expand All @@ -201,7 +201,7 @@ def instrument(record, expander)
value = @key.call(record)
end
if value
@gauge.set(labels(record, expander), value)
@gauge.set(value, labels: labels(record, expander))
end
end
end
Expand All @@ -210,7 +210,7 @@ class Counter < Metric
def initialize(element, registry, labels)
super
begin
@counter = registry.counter(element['name'].to_sym, element['desc'])
@counter = registry.counter(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys)
rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
@counter = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :counter, element['desc'])
end
Expand All @@ -229,7 +229,7 @@ def instrument(record, expander)
# ignore if record value is nil
return if value.nil?

@counter.increment(labels(record, expander), value)
@counter.increment(by: value, labels: labels(record, expander))
end
end

Expand All @@ -241,7 +241,7 @@ def initialize(element, registry, labels)
end

begin
@summary = registry.summary(element['name'].to_sym, element['desc'])
@summary = registry.summary(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys)
rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
@summary = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :summary, element['desc'])
end
Expand All @@ -254,7 +254,7 @@ def instrument(record, expander)
value = @key.call(record)
end
if value
@summary.observe(labels(record, expander), value)
@summary.observe(value, labels: labels(record, expander))
end
end
end
Expand All @@ -271,9 +271,9 @@ def initialize(element, registry, labels)
buckets = element['buckets'].split(/,/).map(&:strip).map do |e|
e[/\A\d+.\d+\Z/] ? e.to_f : e.to_i
end
@histogram = registry.histogram(element['name'].to_sym, element['desc'], {}, buckets)
@histogram = registry.histogram(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys, buckets: buckets)
else
@histogram = registry.histogram(element['name'].to_sym, element['desc'])
@histogram = registry.histogram(element['name'].to_sym, docstring: element['desc'], labels: @base_labels.keys)
end
rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
@histogram = Fluent::Plugin::Prometheus::Metric.get(registry, element['name'].to_sym, :histogram, element['desc'])
Expand All @@ -287,7 +287,7 @@ def instrument(record, expander)
value = @key.call(record)
end
if value
@histogram.observe(labels(record, expander), value)
@histogram.observe(value, labels: labels(record, expander))
end
end
end
Expand Down
17 changes: 8 additions & 9 deletions spec/fluent/plugin/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,19 @@

it 'instruments counter metric' do
expect(counter.type).to eq(:counter)
expect(counter.get({test_key: 'test_value', key: 'foo1'})).to be_kind_of(Numeric)
expect(counter_with_accessor.get({test_key: 'test_value', key: 'foo6'})).to be_kind_of(Numeric)
expect(counter.get(labels: {test_key: 'test_value', key: 'foo1'})).to be_kind_of(Numeric)
expect(counter_with_accessor.get(labels: {test_key: 'test_value', key: 'foo6'})).to be_kind_of(Numeric)
end

it 'instruments gauge metric' do
expect(gauge.type).to eq(:gauge)
expect(gauge.get({test_key: 'test_value', key: 'foo2'})).to eq(100)
expect(gauge.get(labels: {test_key: 'test_value', key: 'foo2'})).to eq(100)
end

it 'instruments summary metric' do
expect(summary.type).to eq(:summary)
expect(summary.get({test_key: 'test_value', key: 'foo3'})).to be_kind_of(Hash)
expect(summary.get({test_key: 'test_value', key: 'foo3'})[0.99]).to eq(100)
expect(summary_with_accessor.get({test_key: 'test_value', key: 'foo5'})[0.99]).to eq(100)
expect(summary.get(labels: {test_key: 'test_value', key: 'foo3'})).to be_kind_of(Hash)
expect(summary_with_accessor.get(labels: {test_key: 'test_value', key: 'foo5'})["sum"]).to eq(100)
end

it 'instruments histogram metric' do
Expand All @@ -200,8 +199,8 @@
end

expect(histogram.type).to eq(:histogram)
expect(histogram.get({test_key: 'test_value', key: 'foo4'})).to be_kind_of(Hash)
expect(histogram.get({test_key: 'test_value', key: 'foo4'})[10]).to eq(5) # 4 + `es` in before
expect(histogram.get(labels: {test_key: 'test_value', key: 'foo4'})).to be_kind_of(Hash)
expect(histogram.get(labels: {test_key: 'test_value', key: 'foo4'})["10"]).to eq(5) # 4 + `es` in before
end
end

Expand Down Expand Up @@ -231,7 +230,7 @@
expect(counter).to be_kind_of(::Prometheus::Client::Metric)
key, _ = counter.values.find {|k,v| v == 100 }
expect(key).to be_kind_of(Hash)
expect(key[:foo]).to eq(100)
expect(key[:foo]).to eq("100")
end
end

Expand Down

0 comments on commit ed599a4

Please sign in to comment.