Skip to content

Commit c43586e

Browse files
authored
metrics: add getter method automatically (#4978)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: This PR aim to refactor preparing the getter method of metrics. In addition, the methods are defined automatically, it prevents we forget to add getter. **Docs Changes**: TODO: Seems we need to create a doc for plugin helper metrics. **Release Note**: Same as the title. --------- Signed-off-by: Shizuo Fujita <[email protected]>
1 parent cc02dec commit c43586e

File tree

8 files changed

+23
-72
lines changed

8 files changed

+23
-72
lines changed

lib/fluent/plugin/bare_output.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ def process(tag, es)
4040
raise NotImplementedError, "BUG: output plugins MUST implement this method"
4141
end
4242

43-
def num_errors
44-
@num_errors_metrics.get
45-
end
46-
47-
def emit_count
48-
@emit_count_metrics.get
49-
end
50-
51-
def emit_size
52-
@emit_size_metrics.get
53-
end
54-
55-
def emit_records
56-
@emit_records_metrics.get
57-
end
58-
5943
def initialize
6044
super
6145
@counter_mutex = Mutex.new

lib/fluent/plugin/buffer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ def initialize
196196
@mutex = Mutex.new
197197
end
198198

199+
# The metrics_create method defines getter methods named stage_byte_size and queue_byte_size.
200+
# For compatibility, stage_size, stage_size=, queue_size, and queue_size= are still available.
199201
def stage_size
200202
@stage_size_metrics.get
201203
end

lib/fluent/plugin/filter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ def initialize
4141
@enable_size_metrics = false
4242
end
4343

44-
def emit_records
45-
@emit_records_metrics.get
46-
end
47-
48-
def emit_size
49-
@emit_size_metrics.get
50-
end
51-
5244
def configure(conf)
5345
super
5446

lib/fluent/plugin/input.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ def initialize
3737
@enable_size_metrics = false
3838
end
3939

40-
def emit_records
41-
@emit_records_metrics.get
42-
end
43-
44-
def emit_size
45-
@emit_size_metrics.get
46-
end
47-
4840
def configure(conf)
4941
super
5042

lib/fluent/plugin/multi_output.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ def initialize
5656
@enable_size_metrics = false
5757
end
5858

59-
def num_errors
60-
@num_errors_metrics.get
61-
end
62-
63-
def emit_count
64-
@emit_count_metrics.get
65-
end
66-
67-
def emit_size
68-
@emit_size_metrics.get
69-
end
70-
71-
def emit_records
72-
@emit_records_metrics.get
73-
end
74-
7559
def statistics
7660
stats = {
7761
'num_errors' => @num_errors_metrics.get,

lib/fluent/plugin/output.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,6 @@ def expired?
171171
# output_enqueue_thread_waiting: for test of output.rb itself
172172
attr_accessor :retry_for_error_chunk # if true, error flush will be retried even if under_plugin_development is true
173173

174-
def num_errors
175-
@num_errors_metrics.get
176-
end
177-
178-
def emit_count
179-
@emit_count_metrics.get
180-
end
181-
182-
def emit_size
183-
@emit_size_metrics.get
184-
end
185-
186-
def emit_records
187-
@emit_records_metrics.get
188-
end
189-
190-
def write_count
191-
@write_count_metrics.get
192-
end
193-
194-
def rollback_count
195-
@rollback_count_metrics.get
196-
end
197-
198174
def initialize
199175
super
200176
@counter_mutex = Mutex.new

lib/fluent/plugin_helper/metrics.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def metrics_create(namespace: "fluentd", subsystem: "metrics", name:, help_text:
7272

7373
@_metrics["#{@plugin_type_or_id}_#{namespace}_#{subsystem}_#{name}"] = metrics
7474

75+
# define the getter method for the calling instance.
76+
singleton_class.module_eval do
77+
unless method_defined?(name)
78+
define_method(name) { metrics.get }
79+
end
80+
end
81+
7582
metrics
7683
end
7784

test/plugin_helper/test_metrics.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,18 @@ def configure(conf)
134134
assert i2.terminated?
135135
assert i3.terminated?
136136
end
137+
138+
test 'can create getter method by metrics name' do
139+
@d = d = Dummy.new
140+
141+
assert_raise(NoMethodError) do
142+
d.foobarbaz
143+
end
144+
145+
metrics = d.metrics_create(namespace: "fluentd_test", subsystem: "unit-test", name: "foobarbaz", help_text: "metrics testing")
146+
metrics.inc
147+
148+
assert_equal(1, d.foobarbaz)
149+
assert_equal(1, metrics.get)
150+
end
137151
end

0 commit comments

Comments
 (0)