This is an automated email from the ASF dual-hosted git repository. daming pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/skywalking-kong.git
commit 48dd74a91f62f79fcd41bece49d8b613e586acbd Author: daming <[email protected]> AuthorDate: Sat Apr 17 00:21:20 2021 +0800 Initial commit --- README.md | 51 ++++++++++++++ plugins/skywalking/handler.lua | 82 +++++++++++++++++++++++ plugins/skywalking/schema.lua | 69 +++++++++++++++++++ rockspec/kong-plugin-skywalking-0.5.0-0.rockspec | 24 +++++++ rockspec/kong-plugin-skywalking-master-0.rockspec | 24 +++++++ 5 files changed, 250 insertions(+) diff --git a/README.md b/README.md new file mode 100644 index 0000000..1cb7c30 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +Apache SkyWalking Nginx Agent For Kong +========== + +This plugin base on Apache SkyWalking Nginx Agent for the Kong API gateway to integrate with the Apache SkyWalking distributed tracing system. + +## Usage + +1. Install the plugin on Kong: + +To install kong-plugin-skywalking: +```bash +$ luarocks install kong-plugin-skywalking --local +``` + +Edit kong.conf: +``` +plugins = bundled,skywalking + +lua_package_path = ${user.home}/.luarocks/share/lua/5.1/?.lua;; +``` + +Set environment: +```bash +$ export KONG_NGINX_HTTP_LUA_SHARED_DICT="tracing_buffer 128m" +``` + +Restart Kong + +2. Enabling & configuring plugin: + +Add the plugin to a service: + +```bash +$ curl -i -X POST \ + --url http://localhost:8001/services/{service_name}/plugins/ \ + --data 'name=skywalking' \ + --data 'config.backend_http_uri=http://localhost:12800' \ + --data 'config.sample_ratio=100' \ + --data 'config.service_name=kong' + --data 'config.service_instance_name=kong-with-skywalking' +``` + +Add the plugin to global: +```bash +$ curl -X POST --url http://localhost:8001/plugins/ \ + --data 'name=skywalking' \ + --data 'config.backend_http_uri=http://localhost:12800' \ + --data 'config.sample_ratio=100' \ + --data 'config.service_name=kong' + --data 'config.service_instance_name=kong-with-skywalking' +``` \ No newline at end of file diff --git a/plugins/skywalking/handler.lua b/plugins/skywalking/handler.lua new file mode 100644 index 0000000..11516cc --- /dev/null +++ b/plugins/skywalking/handler.lua @@ -0,0 +1,82 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local tracer = require("skywalking.tracer") +local client = require("skywalking.client") +local Span = require('skywalking.span') + +local subsystem = ngx.config.subsystem + +local SkyWalkingHandler = { + PRIORITY = 100001, + VERSION = "0.0.1", +} + +function SkyWalkingHandler:init_worker() + require("skywalking.util").set_randomseed() +end + +function SkyWalkingHandler:access(config) + if subsystem == "stream" then + kong.log.warn("Not supportted to trace \"stream\" request yet.") + return + end + + if config.sample_ratio == 100 or math.random() * 100 < config.sample_ratio then + kong.ctx.plugin.skywalking_sample = true + + if not client:isInitialized() then + local metadata_buffer = ngx.shared.tracing_buffer + metadata_buffer:set('serviceName', config.service_name) + metadata_buffer:set('serviceInstanceName', config.service_instance_name) + metadata_buffer:set('includeHostInEntrySpan', config.include_host_in_entry_span) + + client:startBackendTimer(config.backend_http_uri) + end + + tracer:start(kong.request.get_forwarded_host()) + end +end + +function SkyWalkingHandler:body_filter(config) + if ngx.arg[2] and kong.ctx.plugin.skywalking_sample then + local entrySpan = ngx.ctx.entrySpan + Span.setComponentId(entrySpan, 6001) + Span.tag(entrySpan, 'kong.node', kong.node.get_hostname()) + + local service = kong.router.get_service() + if service and service.id then + Span.tag(entrySpan, 'kong.service', service.id) + local route = kong.router.get_route() + if route and route.id then + Span.tag(entrySpan, "kong.route", route.id) + end + if type(service.name) == "string" then + Span.tag(entrySpan, "kong.service_name", service.name) + end + end + Span.setComponentId(ngx.ctx.exitSpan, 6001) + + tracer:finish() + end +end + +function SkyWalkingHandler:log(config) + tracer:prepareForReport() +end + +return SkyWalkingHandler diff --git a/plugins/skywalking/schema.lua b/plugins/skywalking/schema.lua new file mode 100644 index 0000000..5f71246 --- /dev/null +++ b/plugins/skywalking/schema.lua @@ -0,0 +1,69 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local typedefs = require "kong.db.schema.typedefs" + +return { + name = "skywalking", + fields = { + { + consumer = typedefs.no_consumer + }, + { + protocols = typedefs.protocols_http + }, + { + config = { + type = "record", + fields = { + { + backend_http_uri = { + type = "string", + required = true + } + }, + { + service_name = { + type = "string", + required = true + } + }, + { + service_instance_name = { + type = "string", + required = false + } + }, + { + include_host_in_entry_span = { + type = "boolean", + default = false + } + }, + { + sample_ratio = { + type = "number", + between = { 1 , 100 }, + default = 100 + } + } + }, + }, + }, + }, + entity_checks = {}, +} \ No newline at end of file diff --git a/rockspec/kong-plugin-skywalking-0.5.0-0.rockspec b/rockspec/kong-plugin-skywalking-0.5.0-0.rockspec new file mode 100644 index 0000000..ea7d2ce --- /dev/null +++ b/rockspec/kong-plugin-skywalking-0.5.0-0.rockspec @@ -0,0 +1,24 @@ +package = "kong-plugin-skywalking" +version = "0.5.0-0" +source = { + url = "git://github.com/apache/skywalking-nginx-lua", + branch = "v0.5.0", +} + +description = { + summary = "The Nginx Lua agent for Apache SkyWalking", + homepage = "https://github.com/apache/skywalking-nginx-lua", + license = "Apache License 2.0" +} + +dependencies = { + "skywalking-nginx-lua >= 0.5.0" +} + +build = { + type = "builtin", + modules = { + ["kong.plugins.skywalking.handler"] = "kong/plugins/skywalking/handler.lua", + ["kong.plugins.skywalking.schema"] = "kong/plugins/skywalking/schema.lua" + } +} diff --git a/rockspec/kong-plugin-skywalking-master-0.rockspec b/rockspec/kong-plugin-skywalking-master-0.rockspec new file mode 100644 index 0000000..d5f3661 --- /dev/null +++ b/rockspec/kong-plugin-skywalking-master-0.rockspec @@ -0,0 +1,24 @@ +package = "kong-plugin-skywalking" +version = "master-0" +source = { + url = "git://github.com/apache/skywalking-nginx-lua", + branch = "master", +} + +description = { + summary = "The Nginx Lua agent for Apache SkyWalking", + homepage = "https://github.com/apache/skywalking-nginx-lua", + license = "Apache License 2.0" +} + +dependencies = { + "skywalking-nginx-lua >= master" +} + +build = { + type = "builtin", + modules = { + ["kong.plugins.skywalking.handler"] = "kong/plugins/skywalking/handler.lua", + ["kong.plugins.skywalking.schema"] = "kong/plugins/skywalking/schema.lua" + } +}
