refactor(server/logger): batch log submissions

Create a temporary table and submit logs in a single batch after 500ms.
Includes some micro-optimisation and a new convar to set logging service.
This commit is contained in:
Linden
2022-10-09 00:24:49 +11:00
parent b05594e77f
commit f5d0deeb21
2 changed files with 45 additions and 25 deletions

View File

@@ -5,7 +5,7 @@ root = true
[*] [*]
end_of_line = lf end_of_line = lf
insert_final_newline = false insert_final_newline = true
charset = utf-8 charset = utf-8
trim_trailing_whitespace = false trim_trailing_whitespace = false
indent_size = 2 indent_size = 2

View File

@@ -1,30 +1,50 @@
local key = GetConvar('datadog:key', '') local service = GetConvar('ox:logger', 'datadog')
local hostname = GetConvar('sv_projectName', 'fxserver')
local buffer
local bufferSize = 0
if key ~= '' then local function badResponse(endpoint, response)
local site = ('https://http-intake.logs.%s/api/v2/logs'):format(GetConvar('datadog:site', 'datadoghq.com')) print(('unable to submit logs to %s\n%s'):format(endpoint, json.encode(response, { indent = true })))
local resourceName = GetCurrentResourceName() end
key = key:gsub("[\'\"]", '')
function lib.logger(source, event, message, ...) if service == 'datadog' then
local data = json.encode({ local key = GetConvar('datadog:key', ''):gsub("[\'\"]", '')
hostname = resourceName,
service = event,
message = message,
ddsource = tostring(source),
ddtags = string.strjoin(',', string.tostringall(...))
})
PerformHttpRequest(site, function(status, _, _, response) if key ~= '' then
if status ~= 202 then local endpoint = ('https://http-intake.logs.%s/api/v2/logs'):format(GetConvar('datadog:site', 'datadoghq.com'))
-- Thanks, I hate it
response = json.decode(response:sub(10)).errors[1] local headers = {
print(('unable to submit logs to %s\n%s'):format(site, json.encode(response, {indent=true}))) ['Content-Type'] = 'application/json',
end ['DD-API-KEY'] = key,
end, 'POST', data, { }
['Content-Type'] = 'application/json',
['DD-API-KEY'] = key function lib.logger(source, event, message, ...)
}) if not buffer then
end buffer = {}
SetTimeout(500, function()
PerformHttpRequest(endpoint, function(status, _, _, response)
if status ~= 202 then
badResponse(endpoint, json.decode(response:sub(10)).errors[1])
end
end, 'POST', json.encode(buffer), headers)
buffer = nil
bufferSize = 0
end)
end
bufferSize += 1
buffer[bufferSize] = {
hostname = hostname,
service = event,
message = message,
resource = cache.resource,
ddsource = tostring(source),
ddtags = string.strjoin(',', string.tostringall(...))
}
end
end
end end
return lib.logger or function() end return lib.logger or function() end