Skip to content

Commit

Permalink
Add support for gauge aggregation setting (#6)
Browse files Browse the repository at this point in the history
In prometheus-client-mmap `Prometheus::Client::Registry#gauge` has a fourth parameter `multiprocess_mode` which handles the aggregation.

See:
* https://gitlab.com/gitlab-org/ruby/gems/prometheus-client-mmap/-/blob/v0.17.0/lib/prometheus/client/registry.rb#L43
* https://gitlab.com/gitlab-org/ruby/gems/prometheus-client-mmap/-/blob/v0.17.0/lib/prometheus/client/gauge.rb#L12

Not all settings already used by yabeda plugins are supported by prometheus-mmap gem (namely `most_recent`), so map metric aggregation setting from Yabeda to Prometheus-mmap names in best effort.

---------

Co-authored-by: Andrey Novikov <[email protected]>
  • Loading branch information
andreaswachowski and Envek authored Dec 11, 2023
1 parent 03ef470 commit 9fdbff3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/yabeda/prometheus/mmap/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def register_gauge!(metric)
registry.gauge(
build_name(metric),
metric.comment,
build_tags(metric.tags)
build_tags(metric.tags),
gauge_aggregation_mode(metric.aggregation)
)
end

Expand Down Expand Up @@ -86,6 +87,21 @@ def debug!
end
end

private

def gauge_aggregation_mode(yabeda_mode)
case yabeda_mode
when nil, :most_recent # TODO: Switch to most_recent when supported: https://gitlab.com/gitlab-org/ruby/gems/prometheus-client-mmap/-/issues/36
:all
when :min, :max, :all, :liveall
yabeda_mode
when :sum
:livesum
else
raise ArgumentError, "Unsupported gauge aggregation mode #{yabeda_mode.inspect}"
end
end

Yabeda.register_adapter(:prometheus, new)
end
end
Expand Down
22 changes: 21 additions & 1 deletion spec/yabeda/prometheus/mmap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

gauge :gauge,
comment: 'Gauge',
tags: [:gtag]
tags: [:gtag],
aggregation: :sum

histogram :histogram,
comment: 'Histogram',
Expand All @@ -32,9 +33,28 @@
expect(Yabeda.test_counter.values).to eq(
{ { ctag: :'ctag-value' } => 1 }
)
end
end

context 'gauge' do
it do
expect(Yabeda.test_gauge.values).to eq(
{ { gtag: :'gtag-value' } => 123 }
)
end

it 'passes aggregation to multiprocess_mode' do
expect(
Yabeda
.adapters[:prometheus].registry
.instance_variable_get(:@metrics)[:test_gauge]
.instance_variable_get(:@multiprocess_mode)
).to eq(:livesum)
end
end

context 'histogram' do
it do
expect(Yabeda.test_histogram.values).to eq(
{ { htag: :'htag-value' } => 7 }
)
Expand Down

0 comments on commit 9fdbff3

Please sign in to comment.