You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/01/13 09:42:42 UTC

[skywalking-nginx-lua] branch master updated: Add IgnoreSuffix feature (#93)

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

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nginx-lua.git


The following commit(s) were added to refs/heads/master by this push:
     new 49adb5f  Add IgnoreSuffix feature (#93)
49adb5f is described below

commit 49adb5f080c4fabf5456f9ad3a1828c53ad8e337
Author: huawei <al...@gmail.com>
AuthorDate: Thu Jan 13 17:42:36 2022 +0800

    Add IgnoreSuffix feature (#93)
---
 README.md                                          |  2 ++
 examples/nginx.conf                                | 26 ++++++++++++++-
 lib/skywalking/tracer.lua                          | 20 ++++++++----
 lib/skywalking/tracing_context.lua                 |  2 +-
 lib/skywalking/util.lua                            | 38 ++++++++++++++++++++++
 test/e2e/e2e-test/nginx/docker/conf.d/nginx.conf   | 21 ++++++++++++
 test/e2e/e2e-test/nginx/pom.xml                    |  1 +
 .../apache/skywalking/e2e/DataAssertITCase.java    |  8 ++++-
 8 files changed, 109 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 026cf41..ab21317 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,8 @@ http {
         metadata_buffer:set('serviceInstanceName', 'User Service Instance Name')
         -- type 'boolean', mark the entrySpan include host/domain
         metadata_buffer:set('includeHostInEntrySpan', false)
+        -- set ignoreSuffix, If the operation name(HTTP URI) of the entry span includes suffixes in this set, this segment would be ignored. Multiple values should be separated by a comma(',').
+        -- require("skywalking.util").set_ignore_suffix(".jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.svg")
 
         -- set random seed
         require("skywalking.util").set_randomseed()
diff --git a/examples/nginx.conf b/examples/nginx.conf
index c37bf23..845a88c 100644
--- a/examples/nginx.conf
+++ b/examples/nginx.conf
@@ -39,7 +39,8 @@ http {
         metadata_buffer:set('serviceInstanceName', 'User Service Instance Name')
         -- type 'boolean', mark the entrySpan include host/domain
         metadata_buffer:set('includeHostInEntrySpan', false)
-
+        -- set ignoreSuffix, If the operation name(HTTP URI) of the entry span includes suffixes in this set, this segment would be ignored. Multiple values should be separated by a comma(',').
+        -- require("skywalking.util").set_ignore_suffix(".jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.svg")
         -- set randomseed
         require("skywalking.util").set_randomseed()
 
@@ -106,6 +107,29 @@ http {
                 skywalking_tracer:prepareForReport()
             }
         }
+        # ------------------------------------------------------
+        # -- Test suffix of static resources. like: /suffix/suffix.js, /suffix/suffix.css.
+        # -- When suffixes set, this segment would be ignored.
+        # ------------------------------------------------------
+        location /suffix {
+
+            default_type text/html;
+            content_by_lua_block {
+                ngx.say("<p>Suffix for testing only.</p>")
+            }
+            
+            rewrite_by_lua_block {
+                skywalking_tracer:start("backend service")
+            }
+
+            body_filter_by_lua_block {
+                skywalking_tracer:finish()
+            }
+
+            log_by_lua_block {
+                skywalking_tracer:prepareForReport()
+            }
+        }
 
         # ------------------------------------------------------
         # -- Mock backend business service as the upsteeam
diff --git a/lib/skywalking/tracer.lua b/lib/skywalking/tracer.lua
index 5bf8a88..8f62a67 100644
--- a/lib/skywalking/tracer.lua
+++ b/lib/skywalking/tracer.lua
@@ -25,22 +25,29 @@ local json = require('cjson.safe')
 local metadata_shdict = ngx.shared.tracing_buffer
 local ngx = ngx
 local nginxComponentId = 6000
-
-
 local Tracer = {}
 
 
 function Tracer:start(upstream_name, correlation)
+    local log = ngx.log
+    local WARN = ngx.WARN
     local serviceName = metadata_shdict:get("serviceName")
     local serviceInstanceName = metadata_shdict:get('serviceInstanceName')
+    local req_uri = ngx.var.uri
+    if (Util.checkIgnoreSuffix(req_uri)) then
+        local tracingContext =  TC.newNoOP()
+        local ctx = ngx.ctx
+        ctx.tracingContext = tracingContext
+        ctx.entrySpan = Span.newNoOP()
+        ctx.exitSpan = Span.newNoOP()
+        ctx.is_finished = false
+        return
+    end
+
     local includeHostInEntrySpan = metadata_shdict:get('includeHostInEntrySpan')
     local tracingContext = TC.new(serviceName, serviceInstanceName)
-
     -- Constant pre-defined in SkyWalking main repo
     -- 6000 represents Nginx
-
-    local req_uri = ngx.var.uri
-
     local contextCarrier = Util.tablepool_fetch("sw_contextCarrier")
     contextCarrier["sw8"] = ngx.var.http_sw8
     contextCarrier["sw8-correlation"] = ngx.var.http_sw8_correlation
@@ -51,6 +58,7 @@ function Tracer:start(upstream_name, correlation)
     else
         entrySpan = TC.createEntrySpan(tracingContext, req_uri, nil, contextCarrier)
     end
+
     Span.start(entrySpan, time_now)
     Span.setComponentId(entrySpan, nginxComponentId)
     Span.setLayer(entrySpan, Layer.HTTP)
diff --git a/lib/skywalking/tracing_context.lua b/lib/skywalking/tracing_context.lua
index 8e19b63..7c7374d 100644
--- a/lib/skywalking/tracing_context.lua
+++ b/lib/skywalking/tracing_context.lua
@@ -183,4 +183,4 @@ function _M.drainAfterFinished(tracingContext)
     end
 end
 
-return _M
+return _M
\ No newline at end of file
diff --git a/lib/skywalking/util.lua b/lib/skywalking/util.lua
index 942306b..45eb652 100644
--- a/lib/skywalking/util.lua
+++ b/lib/skywalking/util.lua
@@ -15,6 +15,16 @@
 -- limitations under the License.
 --
 
+--  copied from: https://github.com/apache/apisix/blob/b9e36d093bbdafb6d70d332b90679ad13724c2a1/apisix/core/string.lua#L18-L23
+local type = type
+local ffi         = require("ffi")
+local C           = ffi.C
+local ffi_cast    = ffi.cast
+--  copied from: https://github.com/apache/apisix/blob/b9e36d093bbdafb6d70d332b90679ad13724c2a1/apisix/core/string.lua#L26-L28
+ffi.cdef[[
+    int memcmp(const void *s1, const void *s2, size_t n);
+]]
+
 local _M = {}
 
 -- for pure Lua
@@ -28,6 +38,30 @@ local split = function(str, delimiter)
     return t
 end
 
+--  copied from: https://github.com/apache/apisix/blob/b9e36d093bbdafb6d70d332b90679ad13724c2a1/apisix/core/string.lua#L58-L67
+local has_suffix = function(s, suffix)
+    if type(s) ~= "string" or type(suffix) ~= "string" then
+        error("unexpected type: s:" .. type(s) .. ", suffix:" .. type(suffix))
+    end
+    if #s < #suffix then
+        return false
+    end
+    local rc = C.memcmp(ffi_cast("char *", s) + #s - #suffix, suffix, #suffix)
+    return rc == 0
+end
+
+local checkIgnoreSuffix = function(operationName)
+    if _M.ignore_suffix_table ~= nil then
+        for _, suffix in ipairs(_M.ignore_suffix_table) do
+            if has_suffix(operationName, suffix) then
+                return true
+            end
+        end
+    end
+
+    return false
+end
+
 local timestamp = function()
     local _, b = math.modf(os.clock())
     if b == 0 then
@@ -52,6 +86,7 @@ end
 _M.split = split
 _M.timestamp = timestamp
 _M.is_ngx_lua = ok
+_M.checkIgnoreSuffix = checkIgnoreSuffix
 
 
 local MAX_ID_PART2 = 1000000000
@@ -89,6 +124,9 @@ _M.set_randomseed = function ()
     math.randomseed(random_seed())
 end
 
+_M.set_ignore_suffix=function (ignore_suffix)
+    _M.ignore_suffix_table = split(ignore_suffix, ",")
+end
 
 local newID
 -- for Nginx Lua
diff --git a/test/e2e/e2e-test/nginx/docker/conf.d/nginx.conf b/test/e2e/e2e-test/nginx/docker/conf.d/nginx.conf
index ab6bbe5..483f3f8 100644
--- a/test/e2e/e2e-test/nginx/docker/conf.d/nginx.conf
+++ b/test/e2e/e2e-test/nginx/docker/conf.d/nginx.conf
@@ -37,6 +37,8 @@ http {
         metadata_buffer:set('serviceName', 'e2e-test-with-mock-collector')
         -- Instance means the number of Nginx deployment, does not mean the worker instances
         metadata_buffer:set('serviceInstanceName', 'e2e-test-with-mock-collector-instanceA')
+         -- set ignoreSuffix
+        require("skywalking.util").set_ignore_suffix(".jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.svg")
 
         require("skywalking.util").set_randomseed()
         require("skywalking.client"):startBackendTimer("http://${collector}:12800")
@@ -86,6 +88,25 @@ http {
                 skywalking_tracer:prepareForReport()
             }
         }
+        location /suffix {
+
+            default_type text/html;
+            content_by_lua_block {
+                ngx.say("<p>Suffix for testing only.</p>")
+            }
+
+            rewrite_by_lua_block {
+                skywalking_tracer:start("e2e-test-with-mock-collector:upstream_ip:port")
+            }
+
+            body_filter_by_lua_block {
+                skywalking_tracer:finish()
+            }
+
+            log_by_lua_block {
+                skywalking_tracer:prepareForReport()
+            }
+        }
 
         location /backend {
             proxy_pass http://${collector}:12800/receiveData;
diff --git a/test/e2e/e2e-test/nginx/pom.xml b/test/e2e/e2e-test/nginx/pom.xml
index e171fe3..ad349d6 100644
--- a/test/e2e/e2e-test/nginx/pom.xml
+++ b/test/e2e/e2e-test/nginx/pom.xml
@@ -103,6 +103,7 @@
                 <configuration>
                     <systemPropertyVariables>
                         <service.entry>http://${nginx.host}:${nginx.port}/ingress</service.entry>
+                        <suffix.entry>http://${nginx.host}:${nginx.port}/suffix/suffix.js</suffix.entry>
                         <validation.entry>http://${collector.host}:${collector.port}/dataValidate</validation.entry>
                         <healthcheck.entry>http://${collector.host}:${collector.port}/status</healthcheck.entry>
                     </systemPropertyVariables>
diff --git a/test/e2e/e2e-test/nginx/src/test/java/org/apache/skywalking/e2e/DataAssertITCase.java b/test/e2e/e2e-test/nginx/src/test/java/org/apache/skywalking/e2e/DataAssertITCase.java
index cdfe112..ea4fe89 100644
--- a/test/e2e/e2e-test/nginx/src/test/java/org/apache/skywalking/e2e/DataAssertITCase.java
+++ b/test/e2e/e2e-test/nginx/src/test/java/org/apache/skywalking/e2e/DataAssertITCase.java
@@ -38,10 +38,12 @@ public class DataAssertITCase {
     private String validationEntry;
     private String serviceEntry;
     private String healthCheckEntry;
-
+    private String suffixEntry;
+    
     @Before
     public void setup() throws IOException {
         serviceEntry = System.getProperty("service.entry");
+        suffixEntry = System.getProperty("suffix.entry");
         validationEntry = System.getProperty("validation.entry");
         healthCheckEntry = System.getProperty("healthcheck.entry");
     }
@@ -64,6 +66,10 @@ public class DataAssertITCase {
             Assert.assertEquals(200, response.getStatusLine().getStatusCode());
         }
 
+        try (CloseableHttpResponse response = client.execute(new HttpGet(suffixEntry))) {
+            Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        }
+
         times = 0;
         do {
             TimeUnit.SECONDS.sleep(5L); // Wait Agent reported TraceSegment.