Skip to content

Commit e6233a5

Browse files
added multiple group labeling for metrics and support to stop emitting metrics
1 parent dc5ffb1 commit e6233a5

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ When a group reaches its limit and as long as it is not reset, a warning
129129
message with the current log rate of the group is emitted repeatedly. This is
130130
the delay between every repetition.
131131

132+
#### group\_emit\_metrics
133+
134+
Default: `false`.
135+
136+
When a group reaches its limit, metrics will be emitted for the logs being dropped if this value is true . This metrics can be scraped like any other metrics emitted in prometheus format. `podname` is a additional label available to identify the throttled groups. \\
137+
Metrics for the filter is
138+
- `fluentd_throttle_rate_limit_exceeded`
139+
132140
## License
133141

134142
Apache License, Version 2.0

fluent-plugin-throttle.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "maxitest"
2626
spec.add_development_dependency "single_cov"
2727

28+
spec.add_runtime_dependency "prometheus-client", '>= 4.2.2'
2829
spec.add_runtime_dependency "fluentd", "~> 1.1"
2930
end

lib/fluent/plugin/filter_throttle.rb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ class ThrottleFilter < Filter
3737
DESC
3838
config_param :group_warning_delay_s, :integer, :default => 10
3939

40+
desc <<~DESC
41+
Whether to emit a metric when the rate limit is exceeded. The metric is fluentd_throttle_rate_limit_exceeded
42+
DESC
43+
config_param :group_emit_metrics, :bool, :default => false
44+
4045
Group = Struct.new(
4146
:rate_count,
4247
:rate_last_reset,
4348
:aprox_rate,
4449
:bucket_count,
4550
:bucket_last_reset,
4651
:last_warning,
47-
:rate_count_last_exceeded)
52+
:rate_count_last)
4853

4954
def initialize
5055
super
@@ -55,7 +60,8 @@ def configure(conf)
5560
super
5661

5762
@group_key_paths = group_key.map { |key| key.split(".") }
58-
63+
@group_key_symbols = (group_key.map {|str| str.gsub(/[^a-zA-Z0-9_]/,'_')}).map(&:to_sym)
64+
5965
raise "group_bucket_period_s must be > 0" \
6066
unless @group_bucket_period_s > 0
6167

@@ -107,7 +113,7 @@ def filter(tag, time, record)
107113
# compute and store rate/s at most every second
108114
counter.aprox_rate = (counter.rate_count / since_last_rate_reset).round()
109115
counter.rate_count = 0
110-
counter.rate_count_last_exceeded = 0
116+
counter.rate_count_last = 0
111117
counter.rate_last_reset = now
112118
end
113119

@@ -163,10 +169,15 @@ def extract_group(record)
163169
end
164170

165171
def log_rate_limit_exceeded(now, group, counter)
166-
metric = @metrics[:throttle_rate_limit_exceeded]
167-
log.debug("current rate",counter.rate_count,"current metric",metric.get(labels: @base_labels.merge(podname: group)))
168-
metric.increment(by: counter.rate_count - counter.rate_count_last_exceeded, labels: @base_labels.merge(podname: group))
169-
counter.rate_count_last_exceeded = counter.rate_count
172+
# Check if metrics are enabled
173+
if @group_emit_metrics
174+
groupped_label = @group_key_symbols.zip(group).to_h
175+
metric = @metrics[:throttle_rate_limit_exceeded]
176+
log.debug("current rate",counter.rate_count,"current metric",metric.get(labels: @base_labels.merge(groupped_label)))
177+
metric.increment(by: counter.rate_count - counter.rate_count_last, labels: @base_labels.merge(groupped_label))
178+
counter.rate_count_last = counter.rate_count
179+
end
180+
170181
emit = counter.last_warning == nil ? true \
171182
: (now - counter.last_warning) >= @group_warning_delay_s
172183
if emit
@@ -194,10 +205,12 @@ def log_items(now, group, counter)
194205
end
195206

196207
def get_counter(name, docstring)
197-
if @registry.exist?(name)
198-
@registry.get(name)
199-
else
200-
@registry.counter(name, docstring: docstring, labels: @base_labels.keys + [:podname])
208+
if @group_emit_metrics
209+
if @registry.exist?(name)
210+
@registry.get(name)
211+
else
212+
@registry.counter(name, docstring: docstring, labels: @base_labels.keys + @group_key_symbols)
213+
end
201214
end
202215
end
203216
end

0 commit comments

Comments
 (0)