Skip to content
Open
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
46 changes: 22 additions & 24 deletions apisix/plugins/skywalking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,33 @@ function _M.delayed_body_filter(conf, ctx)
end


local started_skywalking_reporter = false
function _M.log(conf, ctx)
if ctx.skywalking_sample then
sw_tracer:prepareForReport()
core.log.info("tracer prepare for report")
if not ctx.skywalking_sample then
return
end

if not started_skywalking_reporter then
started_skywalking_reporter = true

local sk_cli = require("skywalking.client")

local plugin_info = _M.plugin_info
if plugin_info.report_interval then
sk_cli.backendTimerDelay = plugin_info.report_interval
end
sk_cli:startBackendTimer(plugin_info.endpoint_addr)
end

sw_tracer:prepareForReport()
core.log.info("tracer prepare for report")
end


function _M.init()
if process.type() ~= "worker" then
return
end

local local_plugin_info = plugin.plugin_attr(plugin_name)
local_plugin_info = local_plugin_info and core.table.clone(local_plugin_info) or {}
local ok, err = core.schema.check(attr_schema, local_plugin_info)
Expand All @@ -127,32 +141,16 @@ function _M.init()
core.json.delay_encode(local_plugin_info))

-- TODO: maybe need to fetch them from plugin-metadata
local metadata_shdict = ngx.shared.tracing_buffer

if local_plugin_info.service_instance_name == "$hostname" then
local_plugin_info.service_instance_name = core.utils.gethostname()
end

metadata_shdict:set('serviceName', local_plugin_info.service_name)
metadata_shdict:set('serviceInstanceName', local_plugin_info.service_instance_name)
_M.plugin_info = local_plugin_info

local sk_cli = require("skywalking.client")
if local_plugin_info.report_interval then
sk_cli.backendTimerDelay = local_plugin_info.report_interval
end

sk_cli:startBackendTimer(local_plugin_info.endpoint_addr)
end


function _M.destroy()
if process.type() ~= "worker" then
return
end
local metadata_shdict = ngx.shared.tracing_buffer

local sk_cli = require("skywalking.client")
sk_cli:destroyBackendTimer()
metadata_shdict:set('serviceName', local_plugin_info.service_name)
metadata_shdict:set('serviceInstanceName', local_plugin_info.service_instance_name)
end


return _M
62 changes: 4 additions & 58 deletions t/admin/plugins-reload.t
Original file line number Diff line number Diff line change
Expand Up @@ -285,61 +285,7 @@ done



=== TEST 5: reload plugins to disable skywalking
--- yaml_config
apisix:
node_listen: 1984
plugins:
- skywalking
plugin_attr:
skywalking:
service_name: APISIX
service_instance_name: "APISIX Instance Name"
endpoint_addr: http://127.0.0.1:12801
report_interval: 1
--- config
location /t {
content_by_lua_block {
local core = require "apisix.core"
ngx.sleep(1.2)
local t = require("lib.test_admin").test

local data = [[
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key: null
apisix:
node_listen: 1984
plugins:
- prometheus
]]
require("lib.test_admin").set_config_yaml(data)

local code, _, org_body = t('/apisix/admin/plugins/reload',
ngx.HTTP_PUT)

ngx.say(org_body)

ngx.sleep(2)
}
}
--- request
GET /t
--- response_body
done
--- no_error_log
[alert]
--- grep_error_log eval
qr/Instance report fails/
--- grep_error_log_out
Instance report fails



=== TEST 6: check disabling plugin via etcd
=== TEST 5: check disabling plugin via etcd
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -376,7 +322,7 @@ passed



=== TEST 7: hit
=== TEST 6: hit
--- yaml_config
apisix:
node_listen: 1984
Expand All @@ -388,7 +334,7 @@ hello upstream



=== TEST 8: hit after disabling echo
=== TEST 7: hit after disabling echo
--- yaml_config
apisix:
node_listen: 1984
Expand Down Expand Up @@ -421,7 +367,7 @@ hello world



=== TEST 9: wrong method to reload plugins
=== TEST 8: wrong method to reload plugins
--- request
GET /apisix/admin/plugins/reload
--- error_code: 405
Expand Down
64 changes: 3 additions & 61 deletions t/control/plugins-reload.t
Original file line number Diff line number Diff line change
Expand Up @@ -229,79 +229,21 @@ done



=== TEST 4: reload plugins to disable skywalking
--- yaml_config
apisix:
node_listen: 1984
enable_control: true
control:
ip: "127.0.0.1"
port: 9090
plugins:
- skywalking
plugin_attr:
skywalking:
service_name: APISIX
service_instance_name: "APISIX Instance Name"
endpoint_addr: http://127.0.0.1:12801
report_interval: 1
--- config
location /t {
content_by_lua_block {
local core = require "apisix.core"
ngx.sleep(1.2)
local t = require("lib.test_admin").test

local data = [[
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key: null
apisix:
node_listen: 1984
plugins:
- prometheus
]]
require("lib.test_admin").set_config_yaml(data)

local code, _, org_body = t('/v1/plugins/reload',
ngx.HTTP_PUT)

ngx.say(org_body)

ngx.sleep(2)
}
}
--- request
GET /t
--- response_body
done
--- no_error_log
[alert]
--- grep_error_log eval
qr/Instance report fails/
--- grep_error_log_out
Instance report fails



=== TEST 5: wrong method to reload plugins
=== TEST 4: wrong method to reload plugins
--- request
GET /v1/plugins/reload
--- error_code: 404



=== TEST 6: wrong method to reload plugins
=== TEST 5: wrong method to reload plugins
--- request
POST /v1/plugins/reload
--- error_code: 404



=== TEST 7: reload plugin with data_plane deployment
=== TEST 6: reload plugin with data_plane deployment
--- yaml_config
apisix:
node_listen: 1984
Expand Down
Loading
Loading