Skip to content

Commit 4756428

Browse files
perf(loggly): hoist per-request closure out of log phase (#13648)
1 parent 7b90388 commit 4756428

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

apisix/plugins/loggly.lua

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ local function send_bulk_over_http(message, metadata, conf)
309309
end
310310

311311

312-
local handle_http_payload
313-
314-
local function handle_log(entries)
312+
local function handle_log(entries, conf)
315313
local metadata = plugin.plugin_metadata(plugin_name)
316314
core.log.info("metadata: ", core.json.delay_encode(metadata))
317315

@@ -330,11 +328,12 @@ local function handle_log(entries)
330328
return false, err, i
331329
end
332330
end
333-
else
334-
return handle_http_payload(entries, metadata)
331+
return true
335332
end
336333

337-
return true
334+
-- loggly bulk endpoint expects entries concatenated in newline("\n")
335+
local message = tab_concat(entries, "\n")
336+
return send_bulk_over_http(message, metadata, conf)
338337
end
339338

340339

@@ -344,17 +343,16 @@ function _M.log(conf, ctx)
344343
return
345344
end
346345

347-
handle_http_payload = function (entries, metadata)
348-
-- loggly bulk endpoint expects entries concatenated in newline("\n")
349-
local message = tab_concat(entries, "\n")
350-
return send_bulk_over_http(message, metadata, conf)
351-
end
352-
353346
if batch_processor_manager:add_entry(conf, log_data) then
354347
return
355348
end
356349

357-
batch_processor_manager:add_entry_to_new_processor(conf, log_data, ctx, handle_log)
350+
-- bind conf once per batch processor instead of per request
351+
local function func(entries)
352+
return handle_log(entries, conf)
353+
end
354+
355+
batch_processor_manager:add_entry_to_new_processor(conf, log_data, ctx, func)
358356
end
359357

360358

t/plugin/loggly.t

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,75 @@ true
908908
true
909909
--- no_error_log
910910
[alert]
911+
912+
913+
914+
=== TEST 22: http bulk sends each batch with its own conf (regression)
915+
--- http_config
916+
server {
917+
listen 10420;
918+
919+
location ~ ^/loggly/bulk/(?<token>[^/]+)/tag/bulk$ {
920+
content_by_lua_block {
921+
ngx.req.read_body()
922+
local headers = ngx.req.get_headers()
923+
ngx.log(ngx.ERR, "loggly-recv token: ", ngx.var.token,
924+
" tags: ", require("toolkit.json").encode(headers["X-LOGGLY-TAG"]))
925+
ngx.say("ok")
926+
}
927+
}
928+
}
929+
--- config
930+
location /t {
931+
content_by_lua_block {
932+
local t = require("lib.test_admin").test
933+
934+
local code = t('/apisix/admin/plugin_metadata/loggly', ngx.HTTP_PUT, [[{
935+
"host": "127.0.0.1:10420/loggly",
936+
"protocol": "http"
937+
}]])
938+
if code >= 300 then ngx.say("metadata failed: ", code); return end
939+
940+
local code = t('/apisix/admin/routes/1', ngx.HTTP_PUT, [[{
941+
"plugins": {
942+
"loggly": {
943+
"customer_token": "token-a",
944+
"tags": ["aaa"],
945+
"inactive_timeout": 1
946+
}
947+
},
948+
"upstream": {"nodes": {"127.0.0.1:1980": 1}, "type": "roundrobin"},
949+
"host": "127.0.0.1",
950+
"uri": "/route_a"
951+
}]])
952+
if code >= 300 then ngx.say("route_a failed: ", code); return end
953+
954+
local code = t('/apisix/admin/routes/2', ngx.HTTP_PUT, [[{
955+
"plugins": {
956+
"loggly": {
957+
"customer_token": "token-b",
958+
"tags": ["bbb"],
959+
"inactive_timeout": 1
960+
}
961+
},
962+
"upstream": {"nodes": {"127.0.0.1:1980": 1}, "type": "roundrobin"},
963+
"host": "127.0.0.1",
964+
"uri": "/route_b"
965+
}]])
966+
if code >= 300 then ngx.say("route_b failed: ", code); return end
967+
968+
-- buffer one entry per config before either batch flushes, so the
969+
-- old shared closure would send both with the last conf's token
970+
t("/route_a", ngx.HTTP_GET)
971+
t("/route_b", ngx.HTTP_GET)
972+
ngx.say("done")
973+
}
974+
}
975+
--- wait: 3
976+
--- response_body
977+
done
978+
--- error_log
979+
loggly-recv token: token-a tags: "aaa"
980+
loggly-recv token: token-b tags: "bbb"
981+
--- no_error_log
982+
[alert]

0 commit comments

Comments
 (0)