Open
Description
Under normal operating conditions, metrics are requested from an instance every 10/15 seconds or once per minute, which does not impact performance.
However, if multiple requests per second are sent to the metrics, significant performance degradation begins.
Therefore, it would be beneficial to have the ability to limit the number of requests to metrics. If the limit is exceeded, the server should return a 429 Too Many Requests
response.
Here is a link to this research on the impact of metric requests on performance. Adding a rate limiter for metric requests significantly reduces performance degradation.
Here is a code snippet demonstrating how this was implemented:
local clock = require('clock')
local last_called = {}
local timeout = 1
local too_many_func = function ()
return {
status = 429,
headers = { ['content-type'] = 'text/plain; charset=utf8' },
body = 'Too Many Requests',
}
end
local http_handlers = {
json = function(req)
local json_exporter = require('metrics.plugins.json')
return req:render({ text = json_exporter.export() })
end,
prometheus = function(req)
if last_called[req.peer] == nil then
last_called[req.peer] = clock.monotonic()
elseif clock.monotonic() - last_called[req.peer] < timeout then
return too_many_func()
else
last_called[req.peer] = clock.monotonic()
end
local http_handler = require('metrics.plugins.prometheus').collect_http
return http_handler(req)
end,
}
Metadata
Metadata
Assignees
Labels
No labels