Skip to content

Commit 44c8293

Browse files
committed
scripts: Example Lua function to rate limit by size
Signed-off-by: Nilushan Costa <[email protected]>
1 parent 3b7dbbb commit 44c8293

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

conf/rate_limit_by_size.conf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[SERVICE]
2+
Flush 1
3+
Daemon Off
4+
Log_Level info
5+
Parsers_File parsers.conf
6+
7+
[INPUT]
8+
Name tail
9+
Path /var/log/containers/*.log
10+
Parser docker
11+
Tag kube.*
12+
Mem_Buf_Limit 5MB
13+
14+
[FILTER]
15+
Name kubernetes
16+
Match kube.*
17+
18+
[FILTER]
19+
Name lua
20+
Match kube.*
21+
script rate_limit.lua
22+
call rate_limit_by_size
23+
24+
[OUTPUT]
25+
Name stdout
26+
Match *

scripts/rate_limit.lua

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
--[[
22
This Lua script is to do the rate limiting of logs based on some key. The Throttle filter in fluent-bit doesn't allow to do the rate limiting based on key
3-
4-
sample configuration:
5-
[FILTER]
6-
Name lua
7-
Match kube.*
8-
script rate_limit.lua
9-
call rate_limit
103
]]
114

125
local counter = {}
136
local time = 0
147
local group_key = "docker_id" -- Used to group logs. Groups are rate limited independently.
8+
local rate_limit_field = "log" -- The field in the record whose size is used to determine the rate limit
159
local group_bucket_period_s = 60 -- This is the period of of time in seconds over which group_bucket_limit applies.
1610
local group_bucket_limit = 6000 -- Maximum number logs allowed per groups over the period of group_bucket_period_s.
17-
18-
-- with above values, each and every containers running on the kubernetes will have a limit of 6000 logs for every 60 seconds since contianers have unique kubernetes.docker_id value
11+
local group_bucket_limit_bytes = 30000 -- Maximum size of rate_limit_field in bytes allowed per kubernetes.group_key over the period of group_bucket_period_s.
1912

2013
local function get_current_time(timestamp)
2114
return math.floor(timestamp / group_bucket_period_s)
2215
end
2316

17+
--[[
18+
This function is used to rate limit logs based on the number of logs of kubernetes.group_key.
19+
If the number of logs in a group exceeds group_bucket_limit, the log is dropped.
20+
E.g. With above values for the local variables, each and every containers running on Kubernetes will
21+
have a limit of 6000 logs for every 60 seconds since contianers have unique kubernetes.docker_id value
22+
23+
sample configuration:
24+
[FILTER]
25+
Name lua
26+
Match kube.*
27+
script rate_limit.lua
28+
call rate_limit
29+
]]
2430
function rate_limit(tag, timestamp, record)
2531
local t = os.time()
2632
local current_time = get_current_time(t)
@@ -39,4 +45,40 @@ function rate_limit(tag, timestamp, record)
3945
end
4046
end
4147
return 0, 0, 0 -- keep the log
42-
end
48+
end
49+
50+
--[[
51+
This function is used to rate limit logs based on the size of the content of kubernetes.group_key.
52+
E.g. With above values for the local variables, each and every container running on Kubernetes will
53+
have a limit of 30000 bytes for every 60 seconds.
54+
55+
sample configuration:
56+
[FILTER]
57+
Name lua
58+
Match kube.*
59+
script rate_limit.lua
60+
call rate_limit_by_size
61+
]]
62+
function rate_limit_by_size(tag, timestamp, record)
63+
local t = os.time()
64+
local current_time = get_current_time(t)
65+
if current_time ~= time then
66+
time = current_time
67+
counter = {} -- reset the counter
68+
end
69+
70+
if counter[group_key] == -1 then
71+
return -1, 0, 0 -- Log group already rate limited. Hence drop it.
72+
else
73+
if counter[group_key] == nil then
74+
counter[group_key] = #record[rate_limit_field]
75+
else
76+
counter[group_key] = counter[group_key] + #record[rate_limit_field]
77+
end
78+
if counter[group_key] > group_bucket_limit_bytes then
79+
counter[group_key] = -1 -- value of -1 indicates that this group has been rate limited
80+
return -1, 0, 0 -- drop the log
81+
end
82+
return 0, 0, 0
83+
end
84+
end

0 commit comments

Comments
 (0)