Skip to content

Commit

Permalink
fix(skywalking-logger, error-log-logger): support $hostname in skywal…
Browse files Browse the repository at this point in the history
…king service_instance_name (#9401)
  • Loading branch information
shreemaan-abhishek authored May 25, 2023
1 parent d720047 commit 212f59b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
7 changes: 6 additions & 1 deletion apisix/plugins/error-log-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 6 additions & 1 deletion apisix/plugins/skywalking-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
}

Expand Down
31 changes: 31 additions & 0 deletions t/plugin/error-log-logger-skywalking.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion t/plugin/skywalking-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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

0 comments on commit 212f59b

Please sign in to comment.