Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loki-logger nil context in batch processor #9850

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions apisix/plugins/loki-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,16 @@ function _M.log(conf, ctx)
if batch_processor_manager:add_entry(conf, entry) then
return
end

local labels = conf.log_labels
-- parsing possible variables in label value
for key, value in pairs(labels) do
local new_val, err, n_resolved = core.utils.resolve_var(value, ctx.var)
if not err and n_resolved > 0 then
labels[key] = new_val
end
end
bzp2010 marked this conversation as resolved.
Show resolved Hide resolved
-- generate a function to be executed by the batch processor
local func = function(entries)
local labels = conf.log_labels

-- parsing possible variables in label value
for key, value in pairs(labels) do
local new_val, err, n_resolved = core.utils.resolve_var(value, ctx.var)
if not err and n_resolved > 0 then
labels[key] = new_val
end
end

-- build loki request data
local data = {
streams = {
Expand Down
68 changes: 68 additions & 0 deletions t/plugin/loki-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,71 @@ hello world
}
}
--- error_code: 200
=== TEST 12: setup route (with log_labels as variables)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"loki-logger": {
"endpoint_addrs": ["http://127.0.0.1:3100"],
"tenant_id": "tenant_1",
"log_labels": {
"custom_label": "$remote_addr"
},
"batch_max_size": 1
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 13: hit route
--- request
GET /hello
--- response_body
hello world
=== TEST 14: check loki log (with custom_label)
--- config
location /t {
content_by_lua_block {
local cjson = require("cjson")
local now = ngx.now() * 1000
local data, err = require("lib.grafana_loki").fetch_logs_from_loki(
tostring(now - 3000) .. "000000", -- from
tostring(now) .. "000000", -- to
{ query = [[{custom_label="127.0.0.1"} | json]] }
)
assert(err == nil, "fetch logs error: " .. (err or ""))
assert(data.status == "success", "loki response error: " .. cjson.encode(data))
assert(#data.data.result > 0, "loki log empty: " .. cjson.encode(data))
}
}
--- error_code: 200
Loading