寻找一种更好的方法来计算 API 的 5 分钟请求率

Looking for a better way to count the 5 minute request rate of an API

我正在尝试使用 AWS CloudWatch Logs Insights 计算 API 后 5 分钟内来自每个 IP 地址的请求数。

这是我想要获取的数据的示例。

时间范围:01:05 - 01:10

Request Source Number of Requests
53.240.51.81 314
36.241.227.134 237
201.72.45.51 201

时间范围:01:10 - 01:15

Request Source Number of Requests
43.142.151.81 334
36.131.237.174 230
161.72.15.52 198

时间范围:01:15 - 01:20

Request Source Number of Requests
31.132.141.91 334
39.138.217.174 230
191.76.15.42 198

...

这是我的查询:

fields @timestamp, @message 
| filter (@message like "/my_api") 
| parse @message '* - * [*] * * * "*" * * * "*" "*" "*"' as remote_addr, remote_user, time_local, http_method, path, http_version, header, status_code, request_length, body_bytes_sent, http_referer, http_user_agent, http_x_forwarded_for
| stats count() as requestCount by http_x_forwarded_for 
| sort requestCount desc

上面查询的问题是只统计了我指定时间范围内的请求数

因此,如果我想获取过去 24 小时内的数据,我必须指定 Logs Insights 的时间范围 24 * 60 / 5 = 288 次。并且运行查询了288次。

执行我上面描述的步骤会非常耗时。

我想要获取数据的原因是我想使用 AWS WAF 来设置我的 API 的速率限制。

在我应用速率限制之前,我需要知道我的API在平时的请求率。

针对我的 CloudTrail 事件日志尝试了类似的查询,bin() 就是您想要的:

fields @timestamp, @message
| stats count(*) by eventName, bin(5m)
| sort desc
| limit 20