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 2020/02/21 09:43:30 UTC

[skywalking-nginx-lua] branch master updated: Finish the ref, inject, extract logic and unit tests around these.

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 7f061ab  Finish the ref, inject, extract logic and unit tests around these.
7f061ab is described below

commit 7f061abcaf7b3980861958c43da9245efb37c625
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Fri Feb 21 17:43:16 2020 +0800

    Finish the ref, inject, extract logic and unit tests around these.
---
 lib/skywalking/segment_ref.lua          | 46 +++++++++++++++++++++---
 lib/skywalking/segment_ref_test.lua     | 18 ++++++++--
 lib/skywalking/span.lua                 | 56 ++++++++++++++++++++++++++---
 lib/skywalking/span_test.lua            | 18 +++++-----
 lib/skywalking/tracing_context.lua      | 64 ++++++++++++++++++++++++++++-----
 lib/skywalking/tracing_context_test.lua | 22 +++++++++---
 lib/skywalking/util.lua                 |  5 +++
 7 files changed, 197 insertions(+), 32 deletions(-)

diff --git a/lib/skywalking/segment_ref.lua b/lib/skywalking/segment_ref.lua
index 1c83e83..bc35ad6 100644
--- a/lib/skywalking/segment_ref.lua
+++ b/lib/skywalking/segment_ref.lua
@@ -25,13 +25,13 @@ SegmentRef = {
     segment_id,
     span_id,
     network_address,
-    network_address_id,
-    entry_service_instance_id,
-    parent_service_instance_id,
+    network_address_id = 0,
+    entry_service_instance_id = 0,
+    parent_service_instance_id = 0,
     entry_endpoint_name,
-    entry_endpoint_id,
+    entry_endpoint_id = 0,
     parent_endpoint_name,
-    parent_endpoint_id,
+    parent_endpoint_id = 0,
 }
 
 function SegmentRef:new()
@@ -76,4 +76,40 @@ function SegmentRef:fromSW6Value(value)
     return self
 end
 
+-- Return string to represent this ref.
+function SegmentRef:serialize()
+    local encodedRef = '1'
+    encodedRef = encodedRef .. '-' .. Base64.encode(Util:id2String(self.trace_id))
+    encodedRef = encodedRef .. '-' .. Base64.encode(Util:id2String(self.segment_id))
+    encodedRef = encodedRef .. '-' .. self.span_id
+    encodedRef = encodedRef .. '-' .. self.parent_service_instance_id
+    encodedRef = encodedRef .. '-' .. self.entry_service_instance_id
+
+    local networkAddress 
+    if self.network_address_id ~= 0 then
+        networkAddress = self.network_address_id .. ''
+    else
+        networkAddress = '#' .. self.network_address
+    end
+    encodedRef = encodedRef .. '-' .. Base64.encode(networkAddress)
+
+    local entryEndpoint
+    if self.entry_endpoint_id ~= 0 then
+        entryEndpoint = self.entry_endpoint_id .. ''
+    else
+        entryEndpoint = '#' .. self.entry_endpoint_name
+    end
+    encodedRef = encodedRef .. '-' .. Base64.encode(entryEndpoint)
+
+    local parentEndpoint
+    if self.parent_endpoint_id ~= 0 then
+        parentEndpoint = self.parent_endpoint_id .. ''
+    else
+        parentEndpoint = '#' .. self.parent_endpoint_name
+    end
+    encodedRef = encodedRef .. '-' .. Base64.encode(parentEndpoint)
+
+    return encodedRef
+end
+
 return SegmentRef
diff --git a/lib/skywalking/segment_ref_test.lua b/lib/skywalking/segment_ref_test.lua
index 5a62fa5..a076f61 100644
--- a/lib/skywalking/segment_ref_test.lua
+++ b/lib/skywalking/segment_ref_test.lua
@@ -30,15 +30,29 @@ TestSegmentRef = {}
         lu.assertEquals(ref.parent_service_instance_id, 1)
         lu.assertEquals(ref.entry_service_instance_id, 1)
         lu.assertEquals(ref.network_address, '127.0.0.1:8080')
-        lu.assertEquals(ref.network_address_id, nil)
+        lu.assertEquals(ref.network_address_id, 0)
         lu.assertEquals(ref.entry_endpoint_name, '/portal')
-        lu.assertEquals(ref.entry_endpoint_id, nil)
+        lu.assertEquals(ref.entry_endpoint_id, 0)
         lu.assertEquals(ref.parent_endpoint_name, nil)
         lu.assertEquals(ref.parent_endpoint_id, 123)
 
         ref = SegmentRef:new():fromSW6Value('1-My40LjU=-MS')
         lu.assertNil(ref)
     end
+
+    function TestSegmentRef:testSerialize()
+        local ref = SegmentRef:new()
+        ref.trace_id = {3, 4, 5}
+        ref.segment_id = {1, 2, 3}
+        ref.span_id = 4
+        ref.entry_service_instance_id = 1
+        ref.parent_service_instance_id = 1
+        ref.network_address = "127.0.0.1:8080"
+        ref.entry_endpoint_name = "/portal"
+        ref.parent_endpoint_id = 123
+
+        lu.assertEquals(ref:serialize(), '1-My40LjU=-MS4yLjM=-4-1-1-IzEyNy4wLjAuMTo4MDgw-Iy9wb3J0YWw=-MTIz')
+    end
 -- end TestSegmentRef
 
 
diff --git a/lib/skywalking/span.lua b/lib/skywalking/span.lua
index b2d00b3..875676a 100644
--- a/lib/skywalking/span.lua
+++ b/lib/skywalking/span.lua
@@ -16,7 +16,6 @@
 -- 
 
 local spanLayer = require("span_layer")
-local Context = require("tracing_context")
 local Util = require('util')
 local SegmentRef = require("segment_ref")
 
@@ -51,10 +50,7 @@ function Span:createEntrySpan(operationName, context, parent, contextCarrier)
             if ref ~= nil then
                 -- If current trace id is generated by the context, in LUA case, mostly are yes
                 -- use the ref trace id to override it, in order to keep trace id consistently same.
-                if context.self_generated_trace_id == true then
-                    context.self_generated_trace_id = false
-                    context.trace_id = ref.trace_id
-                end
+                context.internal:addRefIfFirst(ref)
                 table.insert(span.refs, ref)
             end
         end
@@ -71,7 +67,51 @@ function Span:createExitSpan(operationName, context, parent, peer, contextCarrie
 
     if contextCarrier ~= nil then
         -- if there is contextCarrier container, the Span will inject the value based on the current tracing context
+        local injectableRef = SegmentRef:new()
+        injectableRef.trace_id = context.trace_id
+        injectableRef.segment_id = context.segment_id
+        injectableRef.span_id = span.span_id
+        -- injectableRef.network_address_id wouldn't be set. Right now, there is no network_address register mechanism
+        injectableRef.network_address = '#' .. peer
+
+        local entryServiceInstanceId
+        local entryEndpointName
+        -- -1 represent the endpoint id doesn't exist, but it is a meaningful value.
+        -- Once -1 is here, the entryEndpointName will be ignored.
+        local entryEndpointId = -1
+
+        local firstSpan = context.internal.first_span
+        if context.internal:hasRef() then
+            local firstRef = context.internal:getFirstRef()
+            injectableRef.entry_service_instance_id = firstRef.entry_service_instance_id
+            entryEndpointName = firstRef.entry_endpoint_name
+            entryEndpointId = firstRef.entry_endpoint_id
+            entryServiceInstanceId = firstRef.entry_service_instance_id
+        else
+            injectableRef.entry_service_instance_id = context.service_inst_id
+            if firstSpan.is_entry then
+                entryEndpointId = 0
+                entryEndpointName = firstSpan.operation_name
+            end
+            entryServiceInstanceId = context.service_inst_id
+        end
+        
+        injectableRef.entry_service_instance_id = entryServiceInstanceId
+        injectableRef.parent_service_instance_id = context.service_inst_id
+        injectableRef.entry_endpoint_name = entryEndpointName
+        injectableRef.entry_endpoint_id = entryEndpointId
+
+        local parentEndpointName
+        local parentEndpointId = -1
+        
+        if firstSpan.is_entry then
+            parentEndpointName = firstSpan.operation_name
+            parentEndpointId = 0
+        end
+        injectableRef.parent_endpoint_name = parentEndpointName
+        injectableRef.parent_endpoint_id = parentEndpointId
 
+        contextCarrier[CONTEXT_CARRIER_KEY] = injectableRef:serialize()
     end
 
     return span
@@ -94,6 +134,7 @@ function Span:new(operationName, context, parent)
 
     o.operation_name = operationName
     o.span_id = context.internal:nextSpanID()
+    
     if parent == nil then
         -- As the root span, the parent span id is -1
         o.parent_span_id = -1
@@ -108,4 +149,9 @@ function Span:new(operationName, context, parent)
     return o
 end
 
+function Span:finish()
+    self.end_time = Util.timestamp()
+    return self
+end
+
 return Span
diff --git a/lib/skywalking/span_test.lua b/lib/skywalking/span_test.lua
index 7abad81..fea35a1 100644
--- a/lib/skywalking/span_test.lua
+++ b/lib/skywalking/span_test.lua
@@ -22,7 +22,7 @@ local SpanLayer = require("span_layer")
 
 TestSpan = {}
     function TestSpan:testNewEntry()
-        local context = TC:new()
+        local context = TC:new(1)
         lu.assertNotNil(context)
 
         local span1 = Span:createEntrySpan("operation_name", context, nil, nil)
@@ -35,7 +35,7 @@ TestSpan = {}
     end
 
     function TestSpan:testNewEntryWithContextCarrier()
-        local context = TC:new()
+        local context = TC:new(1)
         lu.assertNotNil(context)
 
         -- Typical header from the SkyWalking Java Agent test case
@@ -56,9 +56,9 @@ TestSpan = {}
         lu.assertEquals(ref.parent_service_instance_id, 1)
         lu.assertEquals(ref.entry_service_instance_id, 1)
         lu.assertEquals(ref.network_address, '127.0.0.1:8080')
-        lu.assertEquals(ref.network_address_id, nil)
+        lu.assertEquals(ref.network_address_id, 0)
         lu.assertEquals(ref.entry_endpoint_name, '/portal')
-        lu.assertEquals(ref.entry_endpoint_id, nil)
+        lu.assertEquals(ref.entry_endpoint_id, 0)
         lu.assertEquals(ref.parent_endpoint_name, nil)
         lu.assertEquals(ref.parent_endpoint_id, 123)
 
@@ -66,10 +66,11 @@ TestSpan = {}
     end
 
     function TestSpan:testNewExit()
-        local context = TC:new()
+        local context = TC:new(1)
         lu.assertNotNil(context)
 
-        local span1 = Span:createExitSpan("operation_name", context, nil, '127.0.0.1:80')
+        local contextCarrier = {}
+        local span1 = Span:createExitSpan("operation_name", context, nil, '127.0.0.1:80', contextCarrier)
         lu.assertNotNil(span1)
         lu.assertEquals(span1.is_entry, false)
         lu.assertEquals(span1.is_exit, true)
@@ -77,10 +78,11 @@ TestSpan = {}
         lu.assertEquals(span1.peer, '127.0.0.1:80')
 
         lu.assertEquals(#(context.internal.active_spans), 1)
+        lu.assertNotNil(contextCarrier['sw6'])
     end
 
     function TestSpan:testNew()
-        local context = TC:new()
+        local context = TC:new(1)
         lu.assertNotNil(context)
 
         local span1 = Span:new("operation_name", context, nil)
@@ -94,7 +96,7 @@ TestSpan = {}
         lu.assertNotNil(span2.start_time)
 
         -- Use new context to check again
-        context = TC:new()
+        context = TC:new(1)
         lu.assertNotNil(context)
 
         span1 = Span:new("operation_name", context, nil)
diff --git a/lib/skywalking/tracing_context.lua b/lib/skywalking/tracing_context.lua
index 4ce4204..c50b547 100644
--- a/lib/skywalking/tracing_context.lua
+++ b/lib/skywalking/tracing_context.lua
@@ -16,44 +16,62 @@
 -- 
 
 local Util = require('util')
+local Span = require('span')
 
 TracingContext = {
     trace_id,
-    self_generated_trace_id,
     segment_id,
+    service_inst_id,
+
     is_noop = false,
 
     internal,
 }
 
-function TracingContext:new()
+function TracingContext:new(serviceInstID)
     local o = {}
     setmetatable(o, self)
     self.__index = self
 
+    if serviceInstID == nil then
+        return nil
+    end
+
     o.trace_id = Util:newID()
-    o.self_generated_trace_id = true
     o.segment_id = o.trace_id
+    o.service_inst_id = serviceInstID
     o.internal = Internal:new()
     o.internal.owner = o
     return o
 end
 
-function TracingContext:createEntrySpan(operationName)
+-- Delegate to Span:createEntrySpan
+-- @param contextCarrier could be nil if there is no downstream propagated context
+function TracingContext:createEntrySpan(operationName, parent, contextCarrier)
+    return Span:createEntrySpan(operationName, self, parent, contextCarrier)
+end
+
+-- Delegate to Span:createExitSpan
+-- @param contextCarrier could be nil if don't need to inject any context to propagate
+function TracingContext:createExitSpan(operationName, parent, peer, contextCarrier)
+    return Span:createExitSpan(operationName, self, parent, peer, contextCarrier)
 end
 
 -------------- Internal Object-------------
 -- Internal Object hosts the methods for SkyWalking LUA internal APIs only.
 Internal = {
+    self_generated_trace_id,
     -- span id starts from 0
     span_id_seq,
     -- Owner means the Context instance holding this Internal object.
     owner,
     -- The first created span.
     first_span,
-    -- Lists
+    -- The first ref injected in this context
+    first_ref,
     -- Created span and still active
     active_spans,
+    active_count,
     -- Finished spans
     finished_spans,
 }
@@ -64,18 +82,48 @@ function Internal:new()
     setmetatable(o, self)
     self.__index = self
 
+    o.self_generated_trace_id = true
     o.span_id_seq = 0
     o.active_spans = {}
+    o.active_count = 0
     o.finished_spans = {}
 
     return o
 end
 
+-- add the segment ref if this is the first ref of this context
+function Internal:addRefIfFirst(ref)
+    if self.self_generated_trace_id == true then
+        self.self_generated_trace_id = false
+        self.owner.trace_id = ref.trace_id
+        self.first_ref = ref
+    end
+end
+
+function Internal:hasRef()
+    return first_ref ~= nil
+end
+
+function Internal:getFirstRef()
+    return first_ref
+end
+
 function Internal:addActive(span)
-    if first_span == nil then
-        first_span = span
+    if self.first_span == nil then
+        self.first_span = span
     end
-    table.insert(self.active_spans, span)
+
+    -- span id starts at 0, to fit LUA, we need to plus one.    
+    self.active_spans[span.span_id + 1] = span
+    self.active_count = self.active_count + 1
+    return self.owner
+end
+
+function Internal:finishSpan(span)
+    -- span id starts at 0, to fit LUA, we need to plus one.
+    self.active_spans[span.span_id + 1] = nil
+    self.active_count = self.active_count - 1
+    table.insert(self.finished_spans, span)
     return self.owner
 end
 
diff --git a/lib/skywalking/tracing_context_test.lua b/lib/skywalking/tracing_context_test.lua
index 1b07dc1..d464d8b 100644
--- a/lib/skywalking/tracing_context_test.lua
+++ b/lib/skywalking/tracing_context_test.lua
@@ -20,7 +20,7 @@ local TC = require('tracing_context')
 
 TestTracingContext = {}
     function TestTracingContext:testNew()
-        local context = TC:new()
+        local context = TC:new(1)
         lu.assertNotNil(context)
         lu.assertNotNil(context.segment_id[1])
         lu.assertNotNil(context.segment_id[2])
@@ -30,19 +30,33 @@ TestTracingContext = {}
     end
 
     function TestTracingContext:testInternal_NextSpanSeqID()
-        local context = TC:new()
+        local context = TC:new(1)
 
         lu.assertEquals(context.internal:nextSpanID(), 0)
     end
 
     function TestTracingContext:testInternal_addActive()
-        local context = TC:new()
+        local context = TC:new(1)
 
-        local mockSpan = {field = "ws"}
+        local mockSpan = {span_id = 0}
         context.internal:addActive(mockSpan)
 
         lu.assertEquals(#(context.internal.active_spans), 1)
     end
+
+    function TestTracingContext:testSpanStack()
+        local context = TC:new(1)
+        local span1 = context:createEntrySpan('entry_op')
+        local span2 = context:createExitSpan("exit_op", span1, "127.0.0.1")
+
+        local activeSpans = context.internal.active_spans
+        local finishedSpans = context.internal.finished_spans
+        lu.assertEquals(#(activeSpans), 2)
+        lu.assertEquals(#(finishedSpans), 0)
+
+        lu.assertEquals(span1, activeSpans[1])
+        lu.assertEquals(span2, activeSpans[2])
+    end
 -- end TestTracingContext
 
 
diff --git a/lib/skywalking/util.lua b/lib/skywalking/util.lua
index b5d4c96..d2192ed 100644
--- a/lib/skywalking/util.lua
+++ b/lib/skywalking/util.lua
@@ -40,6 +40,11 @@ function Util:formatID(str)
     return parts
 end
 
+-- @param id is an array with length = 3
+function Util:id2String(id)
+    return id[1] .. '.' .. id[2] .. '.' .. id[3]
+end
+
 -- A simulation implementation of Java's System.currentTimeMillis() by following the SkyWalking protocol.
 -- Return the difference as string, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
 -- But in using os.clock(), I am not sure whether it is accurate enough.