You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: scripts/rate_limit.lua
+52-10Lines changed: 52 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,32 @@
1
1
--[[
2
2
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
10
3
]]
11
4
12
5
localcounter= {}
13
6
localtime=0
14
7
localgroup_key="docker_id" -- Used to group logs. Groups are rate limited independently.
8
+
localrate_limit_field="log" -- The field in the record whose size is used to determine the rate limit
15
9
localgroup_bucket_period_s=60-- This is the period of of time in seconds over which group_bucket_limit applies.
16
10
localgroup_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
+
localgroup_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.
19
12
20
13
localfunctionget_current_time(timestamp)
21
14
returnmath.floor(timestamp/group_bucket_period_s)
22
15
end
23
16
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
+
]]
24
30
functionrate_limit(tag, timestamp, record)
25
31
localt=os.time()
26
32
localcurrent_time=get_current_time(t)
@@ -39,4 +45,40 @@ function rate_limit(tag, timestamp, record)
39
45
end
40
46
end
41
47
return0, 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
0 commit comments