This is an automated email from the ASF dual-hosted git repository.
LiteSun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new c9cba08405 fix(tcp-logger): return error when json encoding fails in
batch processor (#14004)
c9cba08405 is described below
commit c9cba084057e5dabf9961332440447f58d65ae5f
Author: Bhuvan Somisetty <[email protected]>
AuthorDate: Thu Nov 12 15:01:00 2026 +0530
fix(tcp-logger): return error when json encoding fails in batch processor
(#14004)
---
apisix/plugins/tcp-logger.lua | 4 ++--
t/plugin/tcp-logger.t | 55 ++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 56 insertions(+), 3 deletions(-)
diff --git a/apisix/plugins/tcp-logger.lua b/apisix/plugins/tcp-logger.lua
index da64de4549..5c561d2eec 100644
--- a/apisix/plugins/tcp-logger.lua
+++ b/apisix/plugins/tcp-logger.lua
@@ -93,7 +93,7 @@ local function send_tcp_data(conf, log_message)
local sock, soc_err = tcp()
if not sock then
- return false, "failed to init the socket" .. soc_err
+ return false, "failed to init the socket: " .. soc_err
end
sock:settimeout(conf.timeout)
@@ -156,7 +156,7 @@ function _M.log(conf, ctx)
end
if not data then
- core.log.error('error occurred while encoding the data: ', err)
+ return false, 'error occurred while encoding the data: ' .. err
end
return send_tcp_data(conf, data)
diff --git a/t/plugin/tcp-logger.t b/t/plugin/tcp-logger.t
index ef676ed3bb..b340436d41 100644
--- a/t/plugin/tcp-logger.t
+++ b/t/plugin/tcp-logger.t
@@ -30,9 +30,10 @@ add_block_preprocessor(sub {
my $extra_init_by_lua = <<_EOC_;
local bp_manager = require("apisix.utils.batch-processor-manager")
local core = require("apisix.core")
+ local json_encode = core.json.encode
local old_f = bp_manager.add_entry_to_new_processor
bp_manager.add_entry_to_new_processor = function(self, conf, entry, ctx,
func)
- ngx.log(ngx.INFO, "batch_entry: ", core.json.encode(entry))
+ ngx.log(ngx.INFO, "batch_entry: ", json_encode(entry))
return old_f(self, conf, entry, ctx, func)
end
_EOC_
@@ -629,3 +630,55 @@ GET /t
GET /t
--- error_log
"body":"opentracing\n"
+
+
+
+=== TEST 18: json encode failure in batch processor callback returns error
gracefully
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local plugin = require("apisix.plugins.tcp-logger")
+ local orig_encode = core.json.encode
+ core.json.encode = function()
+ return nil, "mocked encoding error"
+ end
+
+ local conf = {
+ host = "127.0.0.1",
+ port = 43000,
+ batch_max_size = 1,
+ log_format = {
+ host = "$host",
+ },
+ }
+
+ local ctx = {
+ var = {
+ host = "127.0.0.1",
+ remote_addr = "127.0.0.1",
+ uri = "/t",
+ request_uri = "/t",
+ scheme = "http",
+ request_method = "GET",
+ status = 200,
+ }
+ }
+
+ local ok, call_err = pcall(plugin.log, conf, ctx)
+ ngx.sleep(0.1)
+ core.json.encode = orig_encode
+
+ if not ok then
+ error(call_err)
+ end
+ ngx.say("done")
+ }
+ }
+--- request
+GET /t
+--- response_body
+done
+--- wait: 0.5
+--- error_log
+Batch Processor[log buffer] failed to process entries: error occurred while
encoding the data: mocked encoding error