overextended / ox_lib

A FiveM resource and script library for Lua and JS.
https://overextended.dev/ox_lib
GNU Lesser General Public License v3.0
282 stars 378 forks source link

fix(imports/logger): Loki not indexing varargs #583

Closed SeaLife closed 1 month ago

SeaLife commented 1 month ago

Hey!

i dont know if this is expected behaviour and the loki provider probably needs more rework but i played around with it a bit and currently its submitting the data in a wrong format. The code i used to submit logs (test):

RegisterCommand('t_log', function(source, args, rawCommand)
    lib.logger(-1, 'LogTest', 'Test 123', 'test123:test123', 'rpname:Lena Meier', 'job:police')
end, true)

(before) image (after my changes) image

I tried the following:

RegisterCommand('t_log', function(source, args, rawCommand)
    lib.logger(-1, 'LogTest', 'Test 123', { rpname = "Lena Meier", job = "police" })
end, true)

Which ended up in this: image

Probably this need some more rework, i think submitting the meta data in this format key:value is inconvenient. But this is out-of-scope for this "fix" 🗡️

Cata-a1138 commented 1 month ago

Hello, I would like to report a situation where the code is not working properly in my environment. loki version 2.8.11 promtail version 2.8.11

Test code lib.logger(-1, 'LogTest', 'Test 123', 'test123:test123', 'rpname:Lena Meier', 'job:police')

postBody {"streams":[{"stream":{"server":"[QBCore Framework] QBServer","resource":"pg_test","event":"LogTest"},"values":[["1716564324000000000","Test 123",{"job":"police","test123":"test123","rpname":"Lena Meier"}]]}]}

PerformHttpRequest callback function [400,null,[],"HTTP 400: loghttp.PushRequest.Streams: []*loghttp.Stream: unmarshalerDecoder: Value is string, but can't find closing '\"' symbol, error found in #10 byte of ...|Meier\"}]]}]}|..., bigger context ...|ice\",\"test123\":\"test123\",\"rpname\":\"Lena Meier\"}]]}]}|...\n"]

Code to print data

local postBody = json.encode({streams = tempBuffer})
print(postBody)
PerformHttpRequest(endpoint, function(...)
    print(json.encode({...}))
end, 'POST', postBody, headers)
SeaLife commented 1 month ago

Hello, I would like to report a situation where the code is not working properly in my environment. loki version 2.8.11 promtail version 2.8.11

Test code lib.logger(-1, 'LogTest', 'Test 123', 'test123:test123', 'rpname:Lena Meier', 'job:police')

postBody {"streams":[{"stream":{"server":"[QBCore Framework] QBServer","resource":"pg_test","event":"LogTest"},"values":[["1716564324000000000","Test 123",{"job":"police","test123":"test123","rpname":"Lena Meier"}]]}]}

PerformHttpRequest callback function [400,null,[],"HTTP 400: loghttp.PushRequest.Streams: []*loghttp.Stream: unmarshalerDecoder: Value is string, but can't find closing '"' symbol, error found in #10 byte of ...|Meier"}]]}]}|..., bigger context ...|ice","test123":"test123","rpname":"Lena Meier"}]]}]}|...\n"]

Code to print data

local postBody = json.encode({streams = tempBuffer})
print(postBody)
PerformHttpRequest(endpoint, function(...)
    print(json.encode({...}))
end, 'POST', postBody, headers)

image

The generated JSON works by posting it via HTTPie directly to loki. I think this is not related to this?

Im using Loki 3.0.0, maybe there is a difference in the API? image

SeaLife commented 1 month ago

Just investigated a bit around the API and it seems that they added posting meta variables in the values-Part in 3.0.0 and therefore this fails on 2.8.x

https://grafana.com/docs/loki/v2.8.x/api/#push-log-entries-to-loki https://grafana.com/docs/loki/latest/reference/loki-http-api/#ingest-logs

If possible, can you upgrade to Loki 3.0.0 ?

Can you check if the error persist on ox_lib v3.21.0 ?

@Cata-a1138

Cata-a1138 commented 1 month ago

Just tested in loki 3.0.0 and ox_lib v3.21.0, get the same results as you, thank you for your reply.

Cata-a1138 commented 1 month ago

And I found the reason why you get "table":0x7fe593f2ca50". That is, { rpname = "Lena Meier", job = "police" } in string.tostringall(...) is then converted to a string, so json.encode doesn't work as expected. I have opened a pull request to solve this problem. #588