You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by we...@apache.org on 2020/10/29 07:30:00 UTC

[apisix] branch master updated: feat: upgrade skywalking plugin to support skywalking 8.0 . (#2389)

This is an automated email from the ASF dual-hosted git repository.

wenming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new b23475f  feat: upgrade skywalking plugin to support skywalking 8.0 . (#2389)
b23475f is described below

commit b23475fcba315868f9bb56c492d2715cc8b8bfd5
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Thu Oct 29 15:29:52 2020 +0800

    feat: upgrade skywalking plugin to support skywalking 8.0 . (#2389)
---
 .travis/linux_openresty_runner.sh    |   3 +
 Makefile                             |   3 -
 apisix/plugins/skywalking.lua        | 105 +++++++++++++---
 apisix/plugins/skywalking/client.lua | 232 -----------------------------------
 apisix/plugins/skywalking/tracer.lua | 101 ---------------
 bin/apisix                           |   3 +-
 conf/config-default.yaml             |   4 +
 doc/plugins/skywalking.md            | 107 ++++++++++------
 doc/zh-cn/plugins/skywalking.md      | 109 ++++++++++------
 rockspec/apisix-master-0.rockspec    |   2 +-
 t/APISIX.pm                          |   3 +-
 t/plugin/request-validation.t        |  80 ------------
 t/plugin/skywalking.t                | 109 ++++++++--------
 13 files changed, 292 insertions(+), 569 deletions(-)

diff --git a/.travis/linux_openresty_runner.sh b/.travis/linux_openresty_runner.sh
index 1c5d091..75c9e21 100755
--- a/.travis/linux_openresty_runner.sh
+++ b/.travis/linux_openresty_runner.sh
@@ -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() {
diff --git a/Makefile b/Makefile
index b8bd5f7..322db9d 100644
--- a/Makefile
+++ b/Makefile
@@ -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/
 
diff --git a/apisix/plugins/skywalking.lua b/apisix/plugins/skywalking.lua
index f95286b..26aeb60 100644
--- a/apisix/plugins/skywalking.lua
+++ b/apisix/plugins/skywalking.lua
@@ -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,
 }
 
 
@@ -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,
 }
 
 
@@ -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, ...)
+    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)
+    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
diff --git a/apisix/plugins/skywalking/client.lua b/apisix/plugins/skywalking/client.lua
deleted file mode 100644
index f83a6e3..0000000
--- a/apisix/plugins/skywalking/client.lua
+++ /dev/null
@@ -1,232 +0,0 @@
---
--- 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 core = require("apisix.core")
-local http = require("resty.http")
-local cjson = require('cjson')
-local ngx = ngx
-local ipairs = ipairs
-
-local register = require("skywalking.register")
-
-local _M = {}
-
-local function register_service(conf)
-    local endpoint = conf.endpoint
-
-    local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-    local service_id = tracing_buffer:get(endpoint .. '_service_id')
-    if service_id then
-        return service_id
-    end
-
-    local service_name = conf.service_name
-    local service = register.newServiceRegister(service_name)
-
-    local httpc = http.new()
-    local res, err = httpc:request_uri(endpoint .. '/v2/service/register',
-                        {
-                            method = "POST",
-                            body = core.json.encode(service),
-                            headers = {
-                                ["Content-Type"] = "application/json",
-                            },
-                        })
-    if not res then
-        core.log.error("skywalking service register failed, request uri: ",
-                       endpoint .. '/v2/service/register', ", err: ", err)
-
-    elseif res.status == 200 then
-        core.log.debug("skywalking service register response: ", res.body)
-        local register_results = cjson.decode(res.body)
-
-        for _, result in ipairs(register_results) do
-            if result.key == service_name then
-                service_id = result.value
-                core.log.debug("skywalking service registered, service id:"
-                                .. service_id)
-            end
-        end
-
-    else
-        core.log.error("skywalking service register failed, request uri:",
-                        endpoint .. "/v2/service/register",
-                        ", response code:", res.status)
-    end
-
-    if service_id then
-        tracing_buffer:set(endpoint .. '_service_id', service_id)
-    end
-
-    return service_id
-end
-
-local function register_service_instance(conf, service_id)
-    local endpoint = conf.endpoint
-
-    local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-    local instance_id = tracing_buffer:get(endpoint .. '_instance_id')
-    if instance_id then
-        return instance_id
-    end
-
-    local service_instance_name = core.id.get()
-    local service_instance = register.newServiceInstanceRegister(
-                                        service_id,
-                                        service_instance_name,
-                                        ngx.now() * 1000)
-
-    local httpc = http.new()
-    local res, err = httpc:request_uri(endpoint .. '/v2/instance/register',
-                        {
-                            method = "POST",
-                            body = core.json.encode(service_instance),
-                            headers = {
-                                ["Content-Type"] = "application/json",
-                            },
-                        })
-
-    if not res then
-        core.log.error("skywalking service Instance register failed",
-                        ", request uri: ", conf.endpoint .. '/v2/instance/register',
-                        ", err: ", err)
-
-    elseif res.status == 200 then
-        core.log.debug("skywalking service instance register response: ", res.body)
-        local register_results = cjson.decode(res.body)
-
-        for _, result in ipairs(register_results) do
-            if result.key == service_instance_name then
-                instance_id = result.value
-            end
-        end
-
-    else
-        core.log.error("skywalking service instance register failed, ",
-                        "response code:", res.status)
-    end
-
-    if instance_id then
-        tracing_buffer:set(endpoint .. '_instance_id', instance_id)
-    end
-
-    return instance_id
-end
-
-local function ping(endpoint)
-    local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-    local ping_pkg = register.newServiceInstancePingPkg(
-        tracing_buffer:get(endpoint .. '_instance_id'),
-        core.id.get(),
-        ngx.now() * 1000)
-
-    local httpc = http.new()
-    local res, err = httpc:request_uri(endpoint .. '/v2/instance/heartbeat', {
-        method = "POST",
-        body = core.json.encode(ping_pkg),
-        headers = {
-            ["Content-Type"] = "application/json",
-        },
-    })
-
-    if err then
-        core.log.error("skywalking agent ping failed, err: ", err)
-    else
-        core.log.debug(res.body)
-    end
-end
-
--- report trace segments to the backend
-local function report_traces(endpoint)
-    local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-    local segment = tracing_buffer:rpop(endpoint .. '_segment')
-
-    local count = 0
-
-    local httpc = http.new()
-
-    while segment ~= nil do
-        local res, err = httpc:request_uri(endpoint .. '/v2/segments', {
-            method = "POST",
-            body = segment,
-            headers = {
-                ["Content-Type"] = "application/json",
-            },
-        })
-
-        if err == nil then
-            if res.status ~= 200 then
-                core.log.error("skywalking segment report failed, response code ", res.status)
-                break
-            else
-                count = count + 1
-                core.log.debug(res.body)
-            end
-        else
-            core.log.error("skywalking segment report failed, err: ", err)
-            break
-        end
-
-        segment = tracing_buffer:rpop('segment')
-    end
-
-    if count > 0 then
-        core.log.debug(count, " skywalking segments reported")
-    end
-end
-
-do
-    local heartbeat_timer
-
-function _M.heartbeat(conf)
-    local sw_heartbeat = function()
-        local service_id = register_service(conf)
-        if not service_id then
-            return
-        end
-
-        core.log.debug("skywalking service registered, ",
-                                "service id: ", service_id)
-
-        local service_instance_id = register_service_instance(conf, service_id)
-        if not service_instance_id then
-            return
-        end
-
-        core.log.debug("skywalking service Instance registered, ",
-                                "service instance id: ", service_instance_id)
-        report_traces(conf.endpoint)
-        ping(conf.endpoint)
-    end
-
-    local err
-    if ngx.worker.id() == 0 and not heartbeat_timer then
-        heartbeat_timer, err = core.timer.new("skywalking_heartbeat",
-                                            sw_heartbeat,
-                                            {check_interval = 3}
-                                            )
-        if not heartbeat_timer then
-            core.log.error("failed to create skywalking_heartbeat timer: ", err)
-        else
-            core.log.info("succeed to create timer: skywalking heartbeat")
-        end
-    end
-end
-
-end -- do
-
-
-return _M
diff --git a/apisix/plugins/skywalking/tracer.lua b/apisix/plugins/skywalking/tracer.lua
deleted file mode 100644
index 187b941..0000000
--- a/apisix/plugins/skywalking/tracer.lua
+++ /dev/null
@@ -1,101 +0,0 @@
---
--- 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 core = require("apisix.core")
-local span = require("skywalking.span")
-local tracing_context = require("skywalking.tracing_context")
-local span_layer = require("skywalking.span_layer")
-local sw_segment = require('skywalking.segment')
-
-local pairs = pairs
-local ngx = ngx
-
--- Constant pre-defined in SkyWalking main repo
--- 84 represents Nginx
-local NGINX_COMPONENT_ID = 6000
-
-local _M = {}
-
-function _M.start(ctx, endpoint, upstream_name)
-    local context
-    -- TODO: use lrucache for better performance
-    local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-    local instance_id = tracing_buffer:get(endpoint .. '_instance_id')
-    local service_id = tracing_buffer:get(endpoint .. '_service_id')
-
-    if service_id and instance_id then
-        context = tracing_context.new(service_id, instance_id)
-    else
-        context = tracing_context.newNoOP()
-    end
-
-    local context_carrier = {}
-    context_carrier["sw6"] = ngx.req.get_headers()["sw6"]
-    local entry_span = tracing_context.createEntrySpan(context, ctx.var.uri, nil, context_carrier)
-    span.start(entry_span, ngx.now() * 1000)
-    span.setComponentId(entry_span, NGINX_COMPONENT_ID)
-    span.setLayer(entry_span, span_layer.HTTP)
-
-    span.tag(entry_span, 'http.method', ngx.req.get_method())
-    span.tag(entry_span, 'http.params', ctx.var.scheme .. '://'
-                                        .. ctx.var.host .. ctx.var.request_uri)
-
-    context_carrier = {}
-    local exit_span = tracing_context.createExitSpan(context,
-                                                    ctx.var.upstream_uri,
-                                                    entry_span,
-                                                    upstream_name,
-                                                    context_carrier)
-    span.start(exit_span, ngx.now() * 1000)
-    span.setComponentId(exit_span, NGINX_COMPONENT_ID)
-    span.setLayer(exit_span, span_layer.HTTP)
-
-    for name, value in pairs(context_carrier) do
-        ngx.req.set_header(name, value)
-    end
-
-    -- Push the data in the context
-    ctx.sw_tracing_context = context
-    ctx.sw_entry_span = entry_span
-    ctx.sw_exit_span = exit_span
-
-    core.log.debug("push data into skywalking context")
-end
-
-function _M.finish(ctx)
-    -- Finish the exit span when received the first response package from upstream
-    if ctx.sw_exit_span then
-        span.finish(ctx.sw_exit_span, ngx.now() * 1000)
-        ctx.sw_exit_span = nil
-    end
-end
-
-function _M.prepareForReport(ctx, endpoint)
-    if ctx.sw_entry_span then
-        span.finish(ctx.sw_entry_span, ngx.now() * 1000)
-        local status, segment = tracing_context.drainAfterFinished(ctx.sw_tracing_context)
-        if status then
-            local segment_json = core.json.encode(sw_segment.transform(segment))
-            core.log.debug('segment = ', segment_json)
-
-            local tracing_buffer = ngx.shared['skywalking-tracing-buffer']
-            local length = tracing_buffer:lpush(endpoint .. '_segment', segment_json)
-            core.log.debug('segment buffer size = ', length)
-        end
-    end
-end
-
-return _M
diff --git a/bin/apisix b/bin/apisix
index f98c636..10798e2 100755
--- a/bin/apisix
+++ b/bin/apisix
@@ -191,7 +191,8 @@ http {
     lua_shared_dict balancer_ewma_locks  10m;
     lua_shared_dict balancer_ewma_last_touched_at 10m;
     lua_shared_dict plugin-limit-count-redis-cluster-slot-lock 1m;
-    lua_shared_dict plugin-api-breaker 10m;
+    lua_shared_dict tracing_buffer       10m; # plugin: skywalking
+    lua_shared_dict plugin-api-breaker   10m;
 
     # for openid-connect plugin
     lua_shared_dict discovery             1m; # cache for discovery metadata documents
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index 5c15aaf..bb51039 100644
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -208,3 +208,7 @@ plugin_attr:
   log-rotate:
     interval: 3600    # rotate interval (unit: second)
     max_kept: 168     # max number of log files will be kept
+  skywalking:
+    service_name: APISIX
+    service_instance_name: "APISIX Instance Name"
+    endpoint_addr: http://127.0.0.1:12800
diff --git a/doc/plugins/skywalking.md b/doc/plugins/skywalking.md
index ee46ee8..dff04e1 100644
--- a/doc/plugins/skywalking.md
+++ b/doc/plugins/skywalking.md
@@ -20,26 +20,25 @@
 - [中文](../zh-cn/plugins/skywalking.md)
 
 # Summary
-- [**Summary**](#Summary)
-  - [**Name**](#Name)
-  - [**Attributes**](#Attributes)
-  - [**How To Enable**](#How-To-Enable)
-  - [**Test Plugin**](#Test-Plugin)
-    - [**Run Skywalking Example**](#Run-Skywalking-Example)
-  - [**Disable Plugin**](#Disable-Plugin)
-  - [**Upstream services(Code With SpringBoot)**](#Upstream-services(Code-With-SpringBoot))
+
+- [**Name**](#Name)
+- [**Attributes**](#Attributes)
+- [**How To Enable**](#How-To-Enable)
+- [**Test Plugin**](#Test-Plugin)
+  - [**Run Skywalking Example**](#Run-Skywalking-Example)
+- [**Disable Plugin**](#Disable-Plugin)
+- [**Upstream services(Code With SpringBoot)**](#Upstream-services(Code-With-SpringBoot))
 
 ## Name
 
-**Skywalking**(https://github.com/apache/skywalking) is an OpenTracing plugin.\
-The skywalking server can supports both http and grpc protocols . The APISIX client only support http protocols.
+[**Skywalking**](https://github.com/apache/skywalking) is an OpenTracing plugin.\
+The skywalking server can supports both http and grpc protocols. The APISIX client only support http protocols.
+
 ## Attributes
 
 | Name         | Type   | Requirement | Default  | Valid        | Description                                                          |
 | ------------ | ------ | ----------- | -------- | ------------ | -------------------------------------------------------------------- |
-| endpoint     | string | required    |          |              | the http endpoint of Skywalking, for example: http://127.0.0.1:12800 |
 | sample_ratio | number | required    | 1        | [0.00001, 1] | the ratio of sample                                                  |
-| service_name | string | optional    | "APISIX" |              | service name for skywalking reporter                                 |
 
 ## How To Enable
 
@@ -54,9 +53,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
     ],
     "plugins": {
         "skywalking": {
-            "endpoint": "http://10.110.149.175:12800",
-            "sample_ratio": 1,
-            "service_name": "APISIX_SERVER"
+            "sample_ratio": 1
         }
     },
     "upstream": {
@@ -67,59 +64,97 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
     }
 }'
 ```
+
 You can open dashboard with a browser:`http://127.0.0.1:9080/apisix/dashboard/`,to complete the above operation through the web interface, first add a route:\
-![](../images/plugin/skywalking-1.png)\
+![ ](../images/plugin/skywalking-1.png)\
 Then add skywalking plugin:\
-![](../images/plugin/skywalking-2.png)
+![ ](../images/plugin/skywalking-2.png)
+
+## How to set endpoint
+
+We can set the endpoint by specified the configuration in `conf/config.yaml`.
+
+| Name         | Type   | Default  | Description                                                          |
+| ------------ | ------ | -------- | -------------------------------------------------------------------- |
+| service_name | string | "APISIX" | service name for skywalking reporter                                 |
+|service_instance_name|string|"APISIX Instance Name" | service instance name for skywalking reporter |
+| endpoint     | string | "http://127.0.0.1:12800" | the http endpoint of Skywalking, for example: http://127.0.0.1:12800 |
+
+Here is an example:
+
+```yaml
+plugin_attr:
+  skywalking:
+    service_name: APISIX
+    service_instance_name: "APISIX Instance Name"
+    endpoint_addr: http://127.0.0.1:12800
+```
+
 ## Test Plugin
 
 ### Run-Skywalking-Example
 
 #### e.g.
+
 1. Run Skywalking Server:
-    - By default,use H2 storage , start skywalking directly
-        ```
+    - By default, use H2 storage, start skywalking directly
+
+        ```shell
         sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always apache/skywalking-oap-server
         ```
 
-    -  Of Course,you can use elasticsearch storage
-        1. Firstly, you should install elasticsearch:
-            ```
+    - Of Course, you can use Elasticsearch storage
+
+        1. Firstly, you should install Elasticsearch:
+
+            ```shell
             sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 --restart always -e "discovery.type=single-node" elasticsearch:6.7.2
             ```
+
         2. You can install ElasticSearch management page: elasticsearch-hq(Optional)
-            ```
+
+            ```shell
             sudo docker run -d --name elastic-hq -p 5000:5000 --restart always elastichq/elasticsearch-hq
             ```
+
         3. Run skywalking server:
-            ```
+
+            ```shell
             sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server
             ```
+
 2. Skywalking WebUI:
     1. Run SkyWalking webUI Server:
-        ```
+
+        ```shell
         sudo docker run --name skywalking-ui -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 --restart always apache/skywalking-ui
         ```
+
     2. Open the webUI of  skywalking:
-        You can open dashboard with a browser: http://10.110.149.175:8080 .it will be a successful install as follow:
-        ![](../images/plugin/skywalking-3.png)
+        You can open dashboard with a browser: http://10.110.149.175:8080. It will be a successful install as follow:
+        ![ ](../images/plugin/skywalking-3.png)
 
 3. Test:
-    -  Access to upstream services through access apisix:
+
+    - Access to upstream services through access apisix:
+
         ```bash
         $ curl -v http://10.110.149.192:9080/uid/12
         HTTP/1.1 200 OK
         OK
         ...
         ```
+
     - Open the webUI of skyWalking:
-        ```
+
+        ```shell
         http://10.110.149.175:8080/
         ```
+
         You can see the topology of all service\
-        ![](../../doc/images/plugin/skywalking-4.png)\
+        ![ ](../../doc/images/plugin/skywalking-4.png)\
         You can also see the tracer of all service\
-        ![](../../doc/images/plugin/skywalking-5.png)
+        ![ ](../../doc/images/plugin/skywalking-5.png)
 
 ## Disable Plugin
 
@@ -147,7 +182,6 @@ $ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1  -H 'X-API-KEY: edd1c9f0343
 
 The skywalking plugin has been disabled now. It works for other plugins.
 
-
 ## Upstream services(Code With SpringBoot)
 
 ```java
@@ -172,17 +206,20 @@ public class TestController {
     }
 }
 ```
+
 Configuring the skywalking agent, when starting the service.
 update the file of agent/config/agent.config
-```
+
+```shell
 agent.service_name=yourservername
 collector.backend_service=10.110.149.175:11800
 ```
+
 Run the script:
-```
+
+```shell
 nohup java -javaagent:/root/skywalking/app/agent/skywalking-agent.jar \
 -jar /root/skywalking/app/app.jar \
 --server.port=8089 \
 2>&1 > /root/skywalking/app/logs/nohup.log &
 ```
-
diff --git a/doc/zh-cn/plugins/skywalking.md b/doc/zh-cn/plugins/skywalking.md
index e0ed26e..5245226 100644
--- a/doc/zh-cn/plugins/skywalking.md
+++ b/doc/zh-cn/plugins/skywalking.md
@@ -20,14 +20,14 @@
 - [English](../../plugins/skywalking.md)
 
 # 目录
-- [目录](#目录)
-  - [名字](#名字)
-  - [属性](#属性)
-  - [如何启用](#如何启用)
-  - [测试插件](#测试插件)
-    - [运行 Skywalking 实例](#运行-Skywalking-实例)
-  - [禁用插件](#禁用插件)
-  - [上游服务是java的SpringBoot示例代码](#上游服务是java的SpringBoot示例代码)
+
+- [名字](#名字)
+- [属性](#属性)
+- [如何启用](#如何启用)
+- [测试插件](#测试插件)
+  - [运行 Skywalking 实例](#运行-Skywalking-实例)
+- [禁用插件](#禁用插件)
+- [上游服务是java的SpringBoot示例代码](#上游服务是java的SpringBoot示例代码)
 
 ## 名字
 
@@ -39,9 +39,7 @@
 
 | 名称         | 类型   | 必选项 | 默认值   | 有效值       | 描述                                                  |
 | ------------ | ------ | ------ | -------- | ------------ | ----------------------------------------------------- |
-| endpoint     | string | 必须   |          |              | Skywalking 的 http 节点,例如`http://127.0.0.1:12800` |
 | sample_ratio | number | 必须   | 1        | [0.00001, 1] | 监听的比例                                            |
-| service_name | string | 可选   | "APISIX" |              | 标记当前服务的名称                                    |
 
 ## 如何启用
 
@@ -72,45 +70,75 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
 
 你可以使用浏览器打开 dashboard:`http://127.0.0.1:9080/apisix/dashboard/`,通过 web 界面来完成上面的操作,先增加一个 route:
 
-![](../../images/plugin/skywalking-1.png)
+![plugin_skywalking](../../images/plugin/skywalking-1.png)
 
 然后在 route 页面中添加 skywalking 插件:
 
-![](../../images/plugin/skywalking-2.png)
+![plugin_skywalking](../../images/plugin/skywalking-2.png)
+
+## 如何设置 endpoint
+
+我们可以通过指定 `conf/config.yaml` 中的配置来指定 endpoint:
+
+| 名称         | 类型   | 默认值   | 描述                                                  |
+| ------------ | ------ | -------- | ----------------------------------------------------- |
+| service_name | string |  "APISIX" | skywalking 上报的 service 名称                                 |
+|service_instance_name|string| "APISIX Instance Name" | skywalking 上报的 service 实例名 |
+| endpoint     | string | "http://127.0.0.1:12800" | Skywalking 的 HTTP endpoint 地址,例如:http://127.0.0.1:12800 |
+
+配置示例:
+
+```yaml
+plugin_attr:
+  skywalking:
+    service_name: APISIX
+    service_instance_name: "APISIX Instance Name"
+    endpoint_addr: http://127.0.0.1:12800
+```
 
 ## 测试插件
 
 ### 运行 Skywalking 实例
 
-#### 例子:
-1. 启动Skywalking Server:
-    - 默认使用H2存储,直接启动skywalking即可
-        ```
+#### 例子
+
+1. 启动 Skywalking Server:
+    - 默认使用 H2 存储,直接启动 skywalking 即可
+
+        ```shell
         sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always apache/skywalking-oap-server
         ```
 
-    - 如果使用elasticsearch存储
-        1. 则需要先安装elasticsearch:
-            ```
-            sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 --restart always -e "discovery.type=single-node" elasticsearch:6.7.2
+    - 如果使用 Elasticsearch 存储
+        1. 则需要先安装 Elasticsearch:
 
+            ```shell
+            sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 --restart always -e "discovery.type=single-node" elasticsearch:6.7.2
             ```
-        2. 安装 ElasticSearch管理界面elasticsearch-hq
-            ```
+
+        2. 安装 ElasticSearch 管理界面 elasticsearch-hq
+
+            ```shell
             sudo docker run -d --name elastic-hq -p 5000:5000 --restart always elastichq/elasticsearch-hq
             ```
-        3. 启动skywalking:
-            ```
+
+        3. 启动 skywalking:
+
+            ```shell
             sudo docker run --name skywalking -d -p 1234:1234 -p 11800:11800 -p 12800:12800 --restart always --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server
             ```
-2. Skywalking管理系统:
+
+2. Skywalking 管理系统:
     1. 启动管理系统:
-        ```
+
+        ```shell
         sudo docker run --name skywalking-ui -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 --restart always apache/skywalking-ui
         ```
+
     2. 打开管理页面
-        在浏览器里面输入http://10.110.149.175:8080,出现了如下界面,则表示安装成功
-        ![](../../images/plugin/skywalking-3.png)
+        在浏览器里面输入 http://10.110.149.175:8080,出现了如下界面,则表示安装成功
+
+        ![plugin_skywalking](../../images/plugin/skywalking-3.png)
 
 3. 测试示例:
     - 通过访问apisix,访问上游服务
@@ -120,15 +148,19 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
         HTTP/1.1 200 OK
         OK
         ...
-      ```
-    - 打开浏览器,访问 Skywalking 的 web 页面:
         ```
+
+    - 打开浏览器,访问 Skywalking 的 web 页面:
+
+        ```bash
         http://10.110.149.175:8080/
         ```
+
         可以看到访问拓扑图\
-        ![](../../images/plugin/skywalking-4.png)\
+        ![plugin_skywalking](../../images/plugin/skywalking-4.png)\
         可以看到服务追踪图\
-        ![](../../images/plugin/skywalking-5.png)
+        ![plugin_skywalking](../../images/plugin/skywalking-5.png)
+
 ## 禁用插件
 
 当你想去掉插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
@@ -153,8 +185,7 @@ $ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1  -H 'X-API-KEY: edd1c9f0343
 
 现在就已经移除了 Skywalking 插件了。其他插件的开启和移除也是同样的方法。
 
-
-## 上游服务是java的SpringBoot示例代码
+## 上游服务是 java 的 SpringBoot 示例代码
 
 ```java
 package com.lenovo.ai.controller;
@@ -177,18 +208,22 @@ public class TestController {
        return "OK";
     }
 }
+
 ```
+
 启动服务的时候,需要配置skywalking agent,
 修改agent/config/agent.config中的配置
-```
+
+```shell
 agent.service_name=yourservername
 collector.backend_service=10.110.149.175:11800
 ```
+
 启动服务脚本:
-```
+
+```shell
 nohup java -javaagent:/root/skywalking/app/agent/skywalking-agent.jar \
 -jar /root/skywalking/app/app.jar \
 --server.port=8089 \
 2>&1 > /root/skywalking/app/logs/nohup.log &
 ```
-
diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec
index 7b7c16b..aa85f5a 100644
--- a/rockspec/apisix-master-0.rockspec
+++ b/rockspec/apisix-master-0.rockspec
@@ -51,7 +51,7 @@ dependencies = {
     "lua-resty-ipmatcher = 0.6",
     "lua-resty-kafka = 0.07",
     "lua-resty-logger-socket = 2.0-0",
-    "skywalking-nginx-lua-plugin = 1.0-0",
+    "skywalking-nginx-lua = 0.3-0",
     "base64 = 1.5-2",
     "dkjson = 2.5-2",
     "resty-redis-cluster = 1.02-4",
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 66fd0cc..76653eb 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -241,7 +241,8 @@ _EOC_
     lua_shared_dict balancer_ewma_locks   1m;
     lua_shared_dict balancer_ewma_last_touched_at  1m;
     lua_shared_dict plugin-limit-count-redis-cluster-slot-lock 1m;
-    lua_shared_dict plugin-api-breaker 10m;
+    lua_shared_dict tracing_buffer       10m;    # plugin skywalking
+    lua_shared_dict plugin-api-breaker   10m;
 
     resolver $dns_addrs_str;
     resolver_timeout 5;
diff --git a/t/plugin/request-validation.t b/t/plugin/request-validation.t
index 3742fb4..249d06b 100644
--- a/t/plugin/request-validation.t
+++ b/t/plugin/request-validation.t
@@ -109,46 +109,6 @@ done
                             "type": "roundrobin"
                         },
                         "uri": "/opentracing"
-                }]],
-                [[{
-                    "node": {
-                        "value": {
-                            "plugins": {
-                            "request-validation": {
-                                "body_schema": {
-                                    "type": "object",
-                                    "required": ["required_payload"],
-                                    "properties": {
-                                        "required_payload": {"type": "string"},
-                                        "boolean_payload": {"type": "boolean"},
-                                        "timeouts": {
-                                            "type": "integer",
-                                            "minimum": 1,
-                                            "maximum": 254,
-                                            "default": 3
-                                        },
-                                        "req_headers": {
-                                            "type": "array",
-                                            "minItems": 1,
-                                            "items": {
-                                                "type": "string"
-                                            }
-                                        }
-                                    }
-                                }
-                            }
-                        },
-                            "upstream": {
-                                "nodes": {
-                                    "127.0.0.1:1982": 1
-                                },
-                                "type": "roundrobin"
-                            },
-                            "uri": "/opentracing"
-                        },
-                        "key": "/apisix/routes/1"
-                    },
-                    "action": "set"
                 }]]
                 )
 
@@ -271,46 +231,6 @@ hello1 world
                             "type": "roundrobin"
                         },
                         "uri": "/opentracing"
-                }]],
-                [[{
-                    "node": {
-                        "value": {
-                            "plugins": {
-                            "request-validation": {
-                                "header_schema": {
-                                    "type": "object",
-                                    "required": ["required_payload"],
-                                    "properties": {
-                                        "required_payload": {"type": "string"},
-                                        "boolean_payload": {"type": "boolean"},
-                                        "timeouts": {
-                                            "type": "integer",
-                                            "minimum": 1,
-                                            "maximum": 254,
-                                            "default": 3
-                                        },
-                                        "req_headers": {
-                                            "type": "array",
-                                            "minItems": 1,
-                                            "items": {
-                                                "type": "string"
-                                            }
-                                        }
-                                    }
-                                }
-                            }
-                        },
-                            "upstream": {
-                                "nodes": {
-                                    "127.0.0.1:1982": 1
-                                },
-                                "type": "roundrobin"
-                            },
-                            "uri": "/opentracing"
-                        },
-                        "key": "/apisix/routes/1"
-                    },
-                    "action": "set"
                 }]]
                 )
 
diff --git a/t/plugin/skywalking.t b/t/plugin/skywalking.t
index fef7b46..924af7d 100644
--- a/t/plugin/skywalking.t
+++ b/t/plugin/skywalking.t
@@ -31,6 +31,7 @@ repeat_each(1);
 no_long_string();
 no_root_location();
 log_level("debug");
+
 run_tests;
 
 __DATA__
@@ -41,31 +42,25 @@ __DATA__
         content_by_lua_block {
             local t = require("lib.test_admin").test
             local code, body = t('/apisix/admin/routes/1',
-                 ngx.HTTP_PUT,
-                 [[{
-                        "plugins": {
-                            "skywalking": {
-                                "endpoint": "http://127.0.0.1:1982/mock_skywalking",
-                                "sample_ratio": 1,
-                                "service_name": "APISIX"
-                            }
-                        },
-                        "upstream": {
-                            "nodes": {
-                                "127.0.0.1:1980": 1
-                            },
-                            "type": "roundrobin"
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "skywalking": {
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
                         },
-                        "uri": "/opentracing"
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
                 }]],
                 [[{
                     "node": {
                         "value": {
                             "plugins": {
                                 "skywalking": {
-                                    "endpoint": "http://127.0.0.1:1982/mock_skywalking",
-                                    "sample_ratio": 1,
-                                    "service_name":"APISIX"
                                 }
                             },
                             "upstream": {
@@ -104,22 +99,21 @@ GET /opentracing
 opentracing
 --- no_error_log
 [error]
---- grep_error_log eval
-qr/skywalking service Instance registered, service instance id: \d+/
---- grep_error_log_out eval
-qr/skywalking service Instance registered, service instance id: 1/
+--- error_log
+segments reported
+--- wait: 4
 
 
 
-=== TEST 3: test heartbeat
+=== TEST 3: test heartbeat(TODO: need to update skywalking library)
 --- request
 GET /opentracing
 --- response_body
 opentracing
 --- no_error_log
 [error]
---- error_log
-skywalking heartbeat ok
+--- wait: 4
+--- SKIP
 
 
 
@@ -129,28 +123,26 @@ skywalking heartbeat ok
         content_by_lua_block {
             local t = require("lib.test_admin").test
             local code, body = t('/apisix/admin/routes/1',
-                 ngx.HTTP_PUT,
-                 [[{
-                        "plugins": {
-                            "skywalking": {
-                                "endpoint": "http://127.0.0.1:1982/mock_skywalking",
-                                "sample_ratio": 0.00001
-                            }
-                        },
-                        "upstream": {
-                            "nodes": {
-                                "127.0.0.1:1980": 1
-                            },
-                            "type": "roundrobin"
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "skywalking": {
+                            "sample_ratio": 0.00001
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
                         },
-                        "uri": "/opentracing"
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
                 }]],
                 [[{
                     "node": {
                         "value": {
                             "plugins": {
                                 "skywalking": {
-                                    "endpoint": "http://127.0.0.1:1982/mock_skywalking",
                                     "sample_ratio": 0.00001
                                 }
                             },
@@ -188,8 +180,10 @@ passed
 GET /opentracing
 --- response_body
 opentracing
+--- error_log
+miss sampling, ignore
 --- no_error_log
-push data into skywalking context
+[error]
 
 
 
@@ -199,17 +193,17 @@ push data into skywalking context
         content_by_lua_block {
             local t = require("lib.test_admin").test
             local code, body = t('/apisix/admin/routes/1',
-                 ngx.HTTP_PUT,
-                 [[{
-                        "plugins": {
-                        },
-                        "upstream": {
-                            "nodes": {
-                                "127.0.0.1:1980": 1
-                            },
-                            "type": "roundrobin"
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
                         },
-                        "uri": "/opentracing"
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
                 }]],
                 [[{
                     "node": {
@@ -255,7 +249,7 @@ rewrite phase of skywalking plugin
 
 
 
-=== TEST 8: enable skywalking
+=== TEST 8: enable skywalking(sample_ratio=1)
 --- config
     location /t {
         content_by_lua_block {
@@ -265,9 +259,7 @@ rewrite phase of skywalking plugin
                  [[{
                         "plugins": {
                             "skywalking": {
-                                "endpoint": "http://127.0.0.1:1982/mock_skywalking",
-                                "sample_ratio": 1,
-                                "service_name": "APISIX"
+                                "sample_ratio": 1
                             }
                         },
                         "upstream": {
@@ -283,9 +275,7 @@ rewrite phase of skywalking plugin
                         "value": {
                             "plugins": {
                                 "skywalking": {
-                                    "endpoint": "http://127.0.0.1:1982/mock_skywalking",
-                                    "sample_ratio": 1,
-                                    "service_name":"APISIX"
+                                    "sample_ratio": 1
                                 }
                             },
                             "upstream": {
@@ -324,5 +314,4 @@ GET /opentracing
 opentracing
 --- no_error_log
 [error]
---- error_log
-skywalking segments reported
+--- wait: 4