diff --git a/apisix/plugins/error-log-logger.lua b/apisix/plugins/error-log-logger.lua index a0a364564a27..5d5c5d2a9f8b 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 1c1ba590a0c4..0d90b97893cb 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 edb5003c0988..da339f68e466 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 8d9bdeda7f89..900c5a07aa39 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