You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/05/19 05:46:16 UTC

[apisix] branch master updated: fix: wrong log format for splunk-hec-logging (#9478)

This is an automated email from the ASF dual-hosted git repository.

monkeydluffy 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 8af5b70dc fix: wrong log format for splunk-hec-logging (#9478)
8af5b70dc is described below

commit 8af5b70dc7cd0f41f36662b83355e4ee043ac9b5
Author: Tristan <ji...@foxmail.com>
AuthorDate: Fri May 19 13:46:11 2023 +0800

    fix: wrong log format for splunk-hec-logging (#9478)
---
 apisix/plugins/splunk-hec-logging.lua | 10 +++++-
 t/plugin/splunk-hec-logging.t         | 59 +++++++++++++++++++++++++++++++----
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/apisix/plugins/splunk-hec-logging.lua b/apisix/plugins/splunk-hec-logging.lua
index 8de8be6ec..d7c97107e 100644
--- a/apisix/plugins/splunk-hec-logging.lua
+++ b/apisix/plugins/splunk-hec-logging.lua
@@ -21,6 +21,9 @@ local ngx_now         = ngx.now
 local http            = require("resty.http")
 local log_util        = require("apisix.utils.log-util")
 local bp_manager_mod  = require("apisix.utils.batch-processor-manager")
+local table_insert    = core.table.insert
+local table_concat    = core.table.concat
+local ipairs          = ipairs
 
 
 local DEFAULT_SPLUNK_HEC_ENTRY_SOURCE = "apache-apisix-splunk-hec-logging"
@@ -127,10 +130,15 @@ local function send_to_splunk(conf, entries)
 
     local http_new = http.new()
     http_new:set_timeout(conf.endpoint.timeout * 1000)
+    local t = {}
+    for _, e in ipairs(entries) do
+        table_insert(t, core.json.encode(e))
+    end
+
     local res, err = http_new:request_uri(conf.endpoint.uri, {
         ssl_verify = conf.ssl_verify,
         method = "POST",
-        body = core.json.encode(entries),
+        body = table_concat(t),
         headers = request_headers,
     })
 
diff --git a/t/plugin/splunk-hec-logging.t b/t/plugin/splunk-hec-logging.t
index 78d6e9689..58fba496a 100644
--- a/t/plugin/splunk-hec-logging.t
+++ b/t/plugin/splunk-hec-logging.t
@@ -287,9 +287,9 @@ passed
         local data = ngx.req.get_body_data()
         ngx.log(ngx.WARN, data)
         data = decode(data)
-        assert(data[1].event.client_ip == "127.0.0.1")
-        assert(data[1].source == "apache-apisix-splunk-hec-logging")
-        assert(data[1].host == core.utils.gethostname())
+        assert(data.event.client_ip == "127.0.0.1")
+        assert(data.source == "apache-apisix-splunk-hec-logging")
+        assert(data.host == core.utils.gethostname())
         ngx.say('{}')
     end
 --- request
@@ -361,9 +361,9 @@ passed
         local data = ngx.req.get_body_data()
         ngx.log(ngx.WARN, data)
         data = decode(data)
-        assert(data[1].event.vip == "127.0.0.1")
-        assert(data[1].source == "apache-apisix-splunk-hec-logging")
-        assert(data[1].host == core.utils.gethostname())
+        assert(data.event.vip == "127.0.0.1")
+        assert(data.source == "apache-apisix-splunk-hec-logging")
+        assert(data.host == core.utils.gethostname())
         ngx.say('{}')
     end
 --- request
@@ -375,3 +375,50 @@ hello world
 the mock backend is hit
 --- no_error_log
 [error]
+
+
+
+=== TEST 11: set route test batched data
+--- 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, {
+                uri = "/hello",
+                upstream = {
+                    type = "roundrobin",
+                    nodes = {
+                        ["127.0.0.1:1980"] = 1
+                    }
+                },
+                plugins = {
+                    ["splunk-hec-logging"] = {
+                        endpoint = {
+                            uri = "http://127.0.0.1:18088/services/collector",
+                            token = "BD274822-96AA-4DA6-90EC-18940FB2414C"
+                        },
+                        batch_max_size = 3,
+                        inactive_timeout = 1
+                    }
+                }
+            })
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 12: hit
+--- pipelined_requests eval
+["GET /hello", "GET /hello", "GET /hello"]
+--- wait: 2
+--- response_body eval
+["hello world\n", "hello world\n", "hello world\n"]
+--- no_error_log
+[error]