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/25 01:26:56 UTC

[apisix] branch master updated: fix(skywalking-logger, error-log-logger): support $hostname in skywalking service_instance_name (#9401)

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 212f59bfe fix(skywalking-logger, error-log-logger): support $hostname in skywalking service_instance_name (#9401)
212f59bfe is described below

commit 212f59bfea751766425a388324bd572e43b47ee4
Author: Abhishek Choudhary <sh...@gmail.com>
AuthorDate: Thu May 25 06:56:46 2023 +0530

    fix(skywalking-logger, error-log-logger): support $hostname in skywalking service_instance_name (#9401)
---
 apisix/plugins/error-log-logger.lua    |  7 ++++++-
 apisix/plugins/skywalking-logger.lua   |  7 ++++++-
 t/plugin/error-log-logger-skywalking.t | 31 +++++++++++++++++++++++++++++++
 t/plugin/skywalking-logger.t           | 15 ++++++++++++++-
 4 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/apisix/plugins/error-log-logger.lua b/apisix/plugins/error-log-logger.lua
index a0a364564..5d5c5d2a9 100644
--- a/apisix/plugins/error-log-logger.lua
+++ b/apisix/plugins/error-log-logger.lua
@@ -237,10 +237,15 @@ local function send_to_skywalking(log_message)
     httpc:set_timeout(config.timeout * 1000)
 
     local entries = {}
+    local service_instance_name = config.skywalking.service_instance_name
+    if service_instance_name == "$hostname" then
+        service_instance_name = core.utils.gethostname()
+    end
+
     for i = 1, #log_message, 2 do
         local content = {
             service = config.skywalking.service_name,
-            serviceInstance = config.skywalking.service_instance_name,
+            serviceInstance = service_instance_name,
             endpoint = "",
             body = {
                 text = {
diff --git a/apisix/plugins/skywalking-logger.lua b/apisix/plugins/skywalking-logger.lua
index 1c1ba590a..0d90b9789 100644
--- a/apisix/plugins/skywalking-logger.lua
+++ b/apisix/plugins/skywalking-logger.lua
@@ -132,6 +132,11 @@ function _M.log(conf, ctx)
         end
     end
 
+    local service_instance_name = conf.service_instance_name
+    if service_instance_name == "$hostname" then
+        service_instance_name = core.utils.gethostname()
+    end
+
     local entry = {
         traceContext = trace_context,
         body = {
@@ -140,7 +145,7 @@ function _M.log(conf, ctx)
             }
         },
         service = conf.service_name,
-        serviceInstance = conf.service_instance_name,
+        serviceInstance = service_instance_name,
         endpoint = ctx.var.uri,
     }
 
diff --git a/t/plugin/error-log-logger-skywalking.t b/t/plugin/error-log-logger-skywalking.t
index edb5003c0..da339f68e 100644
--- a/t/plugin/error-log-logger-skywalking.t
+++ b/t/plugin/error-log-logger-skywalking.t
@@ -196,3 +196,34 @@ qr/.*\[\{\"body\":\{\"text\":\{\"text\":\".*this is an info message for test.*\"
 GET /tg
 --- response_body
 passed
+
+
+
+=== TEST 8: put plugin metadata with $hostname and log an error level message
+--- config
+    location /tg {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/error-log-logger',
+                ngx.HTTP_PUT,
+                [[{
+                    "skywalking": {
+                        "endpoint_addr": "http://127.0.0.1:1982/log",
+                        "service_instance_name": "$hostname"
+                    },
+                    "batch_max_size": 15,
+                    "inactive_timeout": 1
+                }]]
+                )
+            ngx.sleep(2)
+            core.log.error("this is an error message for test.")
+        }
+    }
+--- request
+GET /tg
+--- response_body
+--- no_error_log eval
+qr/\\\"serviceInstance\\\":\\\"\$hostname\\\"/
+qr/\\\"serviceInstance\\\":\\\"\\\"/
+--- wait: 0.5
diff --git a/t/plugin/skywalking-logger.t b/t/plugin/skywalking-logger.t
index 8d9bdeda7..900c5a07a 100644
--- a/t/plugin/skywalking-logger.t
+++ b/t/plugin/skywalking-logger.t
@@ -132,7 +132,8 @@ done
                                 "max_retry_count": 1,
                                 "retry_delay": 2,
                                 "buffer_duration": 2,
-                                "inactive_timeout": 2
+                                "inactive_timeout": 2,
+                                "service_instance_name": "$hostname"
                             }
                         },
                         "upstream": {
@@ -281,3 +282,15 @@ opentracing
 --- error_log eval
 qr/.*\{\\\"json\\\":.*\\\\\\"my_ip\\\\\\":\\\\\\"127\.0\.0\.1\\\\\\".*\}/
 --- wait: 0.5
+
+
+
+=== TEST 12: test serviceInstance $hostname
+--- request
+GET /opentracing
+--- response_body
+opentracing
+--- no_error_log eval
+qr/\\\"serviceInstance\\\":\\\"\$hostname\\\"/
+qr/\\\"serviceInstance\\\":\\\"\\\"/
+--- wait: 0.5