Skip to content

Commit 1d565c0

Browse files
andseljsvd
andauthored
Removal of Ruby bridge classes for Gauge and Counter (#17858)
- Update `Collector#push` to avoid dynamic dispatch of method invocation, explicitly invoke set on Gauge and increment on Counter. - Remove Ruby classes Gauge and Counter replacing by existing Java classes LongCounter and LazyDelegatingGauge which was wrapped by Ruby ones. Co-authored-by: João Duarte <[email protected]>
1 parent 6b8d090 commit 1d565c0

File tree

8 files changed

+38
-100
lines changed

8 files changed

+38
-100
lines changed

logstash-core/lib/logstash/instrument/collector.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ def initialize
4545
def push(namespaces_path, key, type, *metric_type_params)
4646
begin
4747
metric_proxy = get(namespaces_path, key, type)
48-
return metric_proxy.execute(*metric_type_params) if metric_proxy.respond_to?(:execute)
49-
50-
logger.error("Collector: Cannot record metric action #{type}@#{metric_type_params.join('/')} on <#{metric_proxy}> at path #{namespaces_path.join('/')}/#{key}")
48+
_, metric_arg = metric_type_params
49+
case type
50+
when :gauge
51+
return metric_proxy.set(metric_arg)
52+
when :counter
53+
return metric_proxy.increment if metric_arg.nil?
54+
return metric_proxy.increment(metric_arg)
55+
else
56+
logger.error("Collector: Cannot record metric action #{type}@#{metric_type_params.join('/')} on <#{metric_proxy}> at path #{namespaces_path.join('/')}/#{key}")
57+
end
5158
rescue MetricStore::NamespacesExpectedError => e
5259
logger.error("Collector: Cannot record metric", :exception => e)
5360
rescue NameError => e

logstash-core/lib/logstash/instrument/metric_type.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require "logstash/instrument/metric_type/counter"
19-
require "logstash/instrument/metric_type/gauge"
20-
2118
module LogStash module Instrument
2219
module MetricType
2320
# Use the string to generate a concrete class for this metrics
@@ -28,8 +25,8 @@ module MetricType
2825
# @raise [NameError] If the class is not found
2926
def self.create(type, namespaces, key)
3027
case type
31-
when :counter then return LogStash::Instrument::MetricType::Counter.new(namespaces, key)
32-
when :gauge then return LogStash::Instrument::MetricType::Gauge.new(namespaces, key)
28+
when :counter then return org.logstash.instrument.metrics.counter.LongCounter.new(key.to_s)
29+
when :gauge then return org.logstash.instrument.metrics.gauge.LazyDelegatingGauge.new(key.to_s)
3330
when :uptime then return org.logstash.instrument.metrics.UptimeMetric.new(key.to_s)
3431
when :timer then return org.logstash.instrument.metrics.timer.TimerMetric::create(key.to_s)
3532
else fail NameError, "Unknown Metric Type `#{type}`"

logstash-core/lib/logstash/instrument/metric_type/counter.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

logstash-core/lib/logstash/instrument/metric_type/gauge.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

logstash-core/spec/logstash/instrument/collector_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
let(:key) { :my_key }
6363

6464
{
65-
counter: LogStash::Instrument::MetricType::Counter,
66-
gauge: LogStash::Instrument::MetricType::Gauge,
65+
counter: org.logstash.instrument.metrics.counter.LongCounter,
66+
gauge: org.logstash.instrument.metrics.gauge.LazyDelegatingGauge,
6767
uptime: org.logstash.instrument.metrics.UptimeMetric,
6868
timer: org.logstash.instrument.metrics.timer.TimerMetric,
6969
}.each do |type, type_specific_implementation|

logstash-core/spec/logstash/instrument/metric_store_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
describe LogStash::Instrument::MetricStore do
2121
let(:namespaces) { [:root, :pipelines, :pipeline_01] }
2222
let(:key) { :events_in }
23-
let(:counter) { LogStash::Instrument::MetricType::Counter.new(namespaces, key) }
23+
let(:counter) { org.logstash.instrument.metrics.counter.LongCounter.new(key.to_s) }
2424

2525
context "when the metric object doesn't exist" do
2626
it "store the object" do
@@ -33,7 +33,7 @@
3333
end
3434

3535
context "when the metric object exist in the namespace" do
36-
let(:new_counter) { LogStash::Instrument::MetricType::Counter.new(namespaces, key) }
36+
let(:new_counter) { org.logstash.instrument.metrics.counter.LongCounter.new(key.to_s) }
3737

3838
it "return the object" do
3939
subject.fetch_or_store(namespaces, key, counter)
@@ -53,18 +53,18 @@
5353
context "retrieving events" do
5454
let(:metric_events) {
5555
[
56-
[[:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch"], :event_in, :increment],
57-
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_in, :increment],
58-
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_out, :increment],
59-
[[:node, :sashimi, :pipelines, :pipeline02], :processed_events_out, :increment],
56+
[[:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch"], :event_in],
57+
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_in],
58+
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_out],
59+
[[:node, :sashimi, :pipelines, :pipeline02], :processed_events_out],
6060
]
6161
}
6262

6363
before :each do
6464
# Lets add a few metrics in the store before trying to find them
65-
metric_events.each do |namespaces, metric_key, action|
66-
metric = subject.fetch_or_store(namespaces, metric_key, LogStash::Instrument::MetricType::Counter.new(namespaces, metric_key))
67-
metric.execute(action)
65+
metric_events.each do |namespaces, metric_key|
66+
metric = subject.fetch_or_store(namespaces, metric_key, org.logstash.instrument.metrics.counter.LongCounter.new(metric_key.to_s))
67+
metric.increment
6868
end
6969
end
7070

@@ -96,7 +96,7 @@
9696

9797
it "allow to retrieve a specific metrics" do
9898
metrics = subject.get(:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch", :event_in)
99-
expect(metrics).to match(a_hash_including(:node => a_hash_including(:sashimi => a_hash_including(:pipelines => a_hash_including(:pipeline01 => a_hash_including(:plugins => a_hash_including(:"logstash-output-elasticsearch" => a_hash_including(:event_in => be_kind_of(LogStash::Instrument::MetricType::Counter)))))))))
99+
expect(metrics).to match(a_hash_including(:node => a_hash_including(:sashimi => a_hash_including(:pipelines => a_hash_including(:pipeline01 => a_hash_including(:plugins => a_hash_including(:"logstash-output-elasticsearch" => a_hash_including(:event_in => be_kind_of(org.logstash.instrument.metrics.counter.LongCounter)))))))))
100100
end
101101

102102
context "with filtered keys" do
@@ -142,7 +142,7 @@
142142

143143
it "allow to retrieve a specific metrics" do
144144
metrics = subject.get_with_path("node/sashimi/pipelines/pipeline01/plugins/logstash-output-elasticsearch/event_in")
145-
expect(metrics).to match(a_hash_including(:node => a_hash_including(:sashimi => a_hash_including(:pipelines => a_hash_including(:pipeline01 => a_hash_including(:plugins => a_hash_including(:"logstash-output-elasticsearch" => a_hash_including(:event_in => be_kind_of(LogStash::Instrument::MetricType::Counter)))))))))
145+
expect(metrics).to match(a_hash_including(:node => a_hash_including(:sashimi => a_hash_including(:pipelines => a_hash_including(:pipeline01 => a_hash_including(:plugins => a_hash_including(:"logstash-output-elasticsearch" => a_hash_including(:event_in => be_kind_of(org.logstash.instrument.metrics.counter.LongCounter)))))))))
146146
end
147147

148148
context "with filtered keys" do
@@ -260,18 +260,18 @@
260260
describe "#prune" do
261261
let(:metric_events) {
262262
[
263-
[[:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch"], :event_in, :increment],
264-
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_in, :increment],
265-
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_out, :increment],
266-
[[:node, :sashimi, :pipelines, :pipeline02], :processed_events_out, :increment],
263+
[[:node, :sashimi, :pipelines, :pipeline01, :plugins, :"logstash-output-elasticsearch"], :event_in],
264+
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_in],
265+
[[:node, :sashimi, :pipelines, :pipeline01], :processed_events_out],
266+
[[:node, :sashimi, :pipelines, :pipeline02], :processed_events_out],
267267
]
268268
}
269269

270270
before :each do
271271
# Lets add a few metrics in the store before trying to find them
272-
metric_events.each do |namespaces, metric_key, action|
273-
metric = subject.fetch_or_store(namespaces, metric_key, LogStash::Instrument::MetricType::Counter.new(namespaces, metric_key))
274-
metric.execute(action)
272+
metric_events.each do |namespaces, metric_key|
273+
metric = subject.fetch_or_store(namespaces, metric_key, org.logstash.instrument.metrics.counter.LongCounter.new(metric_key.to_s))
274+
metric.increment
275275
end
276276
end
277277

logstash-core/spec/logstash/instrument/metric_type/counter_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require "logstash/instrument/metric_type/counter"
18+
require "logstash/json"
1919
require "spec_helper"
2020

21-
describe LogStash::Instrument::MetricType::Counter do
22-
let(:namespaces) { [:root, :pipelines, :pipeline_01] }
21+
describe org.logstash.instrument.metrics.counter.LongCounter do
2322
let(:key) { :mykey }
2423

25-
subject { LogStash::Instrument::MetricType::Counter.new(namespaces, key) }
24+
subject { described_class.new(key.to_s) }
2625

2726
describe "#increment" do
2827
it "increment the counter" do

logstash-core/spec/logstash/instrument/metric_type/gauge_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require "logstash/instrument/metric_type/gauge"
1918
require "logstash/json"
2019
require "spec_helper"
2120

22-
describe LogStash::Instrument::MetricType::Gauge do
23-
let(:namespaces) { [:root, :pipelines, :pipeline_01] }
21+
describe org.logstash.instrument.metrics.gauge.LazyDelegatingGauge do
2422
let(:key) { :mykey }
2523
let(:value) { "hello" }
2624

27-
subject { described_class.new(namespaces, key) }
25+
subject { described_class.new(key.to_s) }
2826

2927
before :each do
30-
subject.execute(:set, value)
28+
subject.set(value)
3129
end
3230

3331
describe "#execute" do

0 commit comments

Comments
 (0)