Skip to content

Commit fabf6c1

Browse files
authored
fix(skywalking-logger, error-log-logger): skywalking service_instance_name $hostname is supported (#7252)
1 parent 24eea26 commit fabf6c1

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

apisix/plugins/error-log-logger.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,15 @@ local function send_to_skywalking(log_message)
180180
httpc:set_timeout(config.timeout * 1000)
181181

182182
local entries = {}
183+
local service_instance_name = config.skywalking.service_instance_name
184+
if service_instance_name == "$hostname" then
185+
service_instance_name = core.utils.gethostname()
186+
end
187+
183188
for i = 1, #log_message, 2 do
184189
local content = {
185190
service = config.skywalking.service_name,
186-
serviceInstance = config.skywalking.service_instance_name,
191+
serviceInstance = service_instance_name,
187192
endpoint = "",
188193
body = {
189194
text = {

apisix/plugins/skywalking-logger.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ function _M.log(conf, ctx)
143143
end
144144
end
145145

146+
local service_instance_name = conf.service_instance_name
147+
if service_instance_name == "$hostname" then
148+
service_instance_name = core.utils.gethostname()
149+
end
150+
146151
local entry = {
147152
traceContext = trace_context,
148153
body = {
@@ -151,7 +156,7 @@ function _M.log(conf, ctx)
151156
}
152157
},
153158
service = conf.service_name,
154-
serviceInstance = conf.service_instance_name,
159+
serviceInstance = service_instance_name,
155160
endpoint = ctx.var.uri,
156161
}
157162

t/plugin/skywalking-logger.t

+14-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ done
136136
"max_retry_count": 1,
137137
"retry_delay": 2,
138138
"buffer_duration": 2,
139-
"inactive_timeout": 2
139+
"inactive_timeout": 2,
140+
"service_instance_name": "$hostname"
140141
}
141142
},
142143
"upstream": {
@@ -232,3 +233,15 @@ opentracing
232233
--- error_log eval
233234
qr/.*\{\\\"json\\\":\\\"\{(\\\\\\\"\@timestamp\\\\\\\":\\\\\\\".*\\\\\\\"|\\\\\\\"client_ip\\\\\\\":\\\\\\\"127\.0\.0\.1\\\\\\\"|\\\\\\\"host\\\\\\\":\\\\\\\"localhost\\\\\\\"|\\\\\\\"route_id\\\\\\\":\\\\\\\"1\\\\\\\"|,){7}\}/
234235
--- wait: 0.5
236+
237+
238+
239+
=== TEST 10: test serviceInstance $hostname
240+
--- request
241+
GET /opentracing
242+
--- response_body
243+
opentracing
244+
--- no_error_log eval
245+
qr/\\\"serviceInstance\\\":\\\"\$hostname\\\"/
246+
qr/\\\"serviceInstance\\\":\\\"\\\"/
247+
--- wait: 0.5

0 commit comments

Comments
 (0)