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

feat: upgrade skywalking plugin to support skywalking 8.0 . #2389

Merged
merged 20 commits into from
Oct 29, 2020
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
3 changes: 3 additions & 0 deletions .travis/linux_openresty_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ before_install() {
docker run --name eureka -d -p 8761:8761 --env ENVIRONMENT=apisix --env spring.application.name=apisix-eureka --env server.port=8761 --env eureka.instance.ip-address=127.0.0.1 --env eureka.client.registerWithEureka=true --env eureka.client.fetchRegistry=false --env eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/ bitinit/eureka
sleep 5
docker exec -i kafka-server1 /opt/bitnami/kafka/bin/kafka-topics.sh --create --zookeeper zookeeper-server:2181 --replication-factor 1 --partitions 1 --topic test2

# start skywalking
docker run --rm --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 apache/skywalking-oap-server
}

do_install() {
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ install: default
$(INSTALL) -d $(INST_LUADIR)/apisix/plugins/zipkin
$(INSTALL) apisix/plugins/zipkin/*.lua $(INST_LUADIR)/apisix/plugins/zipkin/

$(INSTALL) -d $(INST_LUADIR)/apisix/plugins/skywalking
$(INSTALL) apisix/plugins/skywalking/*.lua $(INST_LUADIR)/apisix/plugins/skywalking/

$(INSTALL) -d $(INST_LUADIR)/apisix/ssl/router
$(INSTALL) apisix/ssl/router/*.lua $(INST_LUADIR)/apisix/ssl/router/

Expand Down
105 changes: 87 additions & 18 deletions apisix/plugins/skywalking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,48 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local sw_tracer = require("skywalking.tracer")
local core = require("apisix.core")
local process = require("ngx.process")
local ngx = ngx
local math = math

local sw_client = require("apisix.plugins.skywalking.client")
local sw_tracer = require("apisix.plugins.skywalking.tracer")
local select = select
local type = type
local require = require

local plugin_name = "skywalking"

local metadata_schema = {
type = "object",
properties = {
service_name = {
type = "string",
description = "service name for skywalking",
default = "APISIX",
},
service_instance_name = {
type = "string",
description = "User Service Instance Name",
default = "APISIX Instance Name",
},
endpoint_addr = {
type = "string",
default = "http://127.0.0.1:12800",
},
},
additionalProperties = false,
}

local schema = {
type = "object",
properties = {
endpoint = {type = "string"},
sample_ratio = {type = "number", minimum = 0.00001, maximum = 1, default = 1}
sample_ratio = {
type = "number",
minimum = 0.00001,
maximum = 1,
default = 1
}
},
service_name = {
type = "string",
description = "service name for skywalking",
default = "APISIX",
},
required = {"endpoint"}
additionalProperties = false,
}


Expand All @@ -44,6 +64,7 @@ local _M = {
priority = -1100, -- last running plugin, but before serverless post func
name = plugin_name,
schema = schema,
metadata_schema = metadata_schema,
}


Expand All @@ -55,26 +76,74 @@ end
function _M.rewrite(conf, ctx)
core.log.debug("rewrite phase of skywalking plugin")
ctx.skywalking_sample = false
if conf.sample_ratio == 1 or math.random() < conf.sample_ratio then
if conf.sample_ratio == 1 or math.random() <= conf.sample_ratio then
ctx.skywalking_sample = true
sw_client.heartbeat(conf)
-- Currently, we can not have the upstream real network address
sw_tracer.start(ctx, conf.endpoint, "upstream service")
sw_tracer:start("upstream service")
core.log.info("tracer start")
return
end

core.log.info("miss sampling, ignore")
end


function _M.body_filter(conf, ctx)
if ctx.skywalking_sample and ngx.arg[2] then
sw_tracer.finish(ctx)
sw_tracer:finish()
core.log.info("tracer finish")
end
end


function _M.log(conf, ctx)
if ctx.skywalking_sample then
sw_tracer.prepareForReport(ctx, conf.endpoint)
sw_tracer:prepareForReport()
core.log.info("tracer prepare for report")
end
end


local function try_read_attr(t, ...)
membphis marked this conversation as resolved.
Show resolved Hide resolved
local count = select('#', ...)
for i = 1, count do
local attr = select(i, ...)
if type(t) ~= "table" then
return nil
end
t = t[attr]
end

return t
end


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

local local_conf = core.config.local_conf()
local local_plugin_info = try_read_attr(local_conf, "plugin_attr",
plugin_name) or {}
local_plugin_info = core.table.clone(local_plugin_info)
membphis marked this conversation as resolved.
Show resolved Hide resolved
local ok, err = core.schema.check(metadata_schema, local_plugin_info)
if not ok then
core.log.error("failed to check the plugin_attr[", plugin_name, "]",
": ", err)
return
end

core.log.info("plugin attribute: ",
core.json.delay_encode(local_plugin_info))

-- TODO: maybe need to fetch them from plugin-metadata
local metadata_shdict = ngx.shared.tracing_buffer
metadata_shdict:set('serviceName', local_plugin_info.service_name)
metadata_shdict:set('serviceInstanceName', local_plugin_info.service_instance_name)

local sk_cli = require("skywalking.client")
sk_cli:startBackendTimer(local_plugin_info.endpoint_addr)
end


return _M
Loading