Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(skywalking-logger, error-log-logger): support $hostname in skywalking service_instance_name #9401

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
leslie-tsang marked this conversation as resolved.
Show resolved Hide resolved

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