From 9fdbff3f917a1bc8b1ac6d09a84da44bd169c855 Mon Sep 17 00:00:00 2001 From: Andreas Wachowski Date: Mon, 11 Dec 2023 13:47:18 +0100 Subject: [PATCH] Add support for gauge aggregation setting (#6) 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 --- lib/yabeda/prometheus/mmap/adapter.rb | 18 +++++++++++++++++- spec/yabeda/prometheus/mmap_spec.rb | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/yabeda/prometheus/mmap/adapter.rb b/lib/yabeda/prometheus/mmap/adapter.rb index 378b1b2..ea15db7 100644 --- a/lib/yabeda/prometheus/mmap/adapter.rb +++ b/lib/yabeda/prometheus/mmap/adapter.rb @@ -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 @@ -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 diff --git a/spec/yabeda/prometheus/mmap_spec.rb b/spec/yabeda/prometheus/mmap_spec.rb index 0cf4f5c..2ded663 100644 --- a/spec/yabeda/prometheus/mmap_spec.rb +++ b/spec/yabeda/prometheus/mmap_spec.rb @@ -13,7 +13,8 @@ gauge :gauge, comment: 'Gauge', - tags: [:gtag] + tags: [:gtag], + aggregation: :sum histogram :histogram, comment: 'Histogram', @@ -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 } )