membphis commented on a change in pull request #10: used the nginx lua code 
style.
URL: 
https://github.com/apache/skywalking-nginx-lua/pull/10#discussion_r387031063
 
 

 ##########
 File path: lib/skywalking/segment_ref.lua
 ##########
 @@ -16,142 +16,147 @@
 --
 local Util = require('util')
 local Base64 = require('dependencies/base64')
+local encode_base64 = Base64.encode
+local decode_base64 = Base64.decode
 
-local SegmentRef = {
-    -- There is no multiple-threads scenario in the LUA, no only hard coded as 
CROSS_PROCESS
-    type = 'CROSS_PROCESS',
-    trace_id,
-    segment_id,
-    span_id,
-    network_address,
-    network_address_id = 0,
-    entry_service_instance_id = 0,
-    parent_service_instance_id = 0,
-    entry_endpoint_name,
-    entry_endpoint_id = 0,
-    parent_endpoint_name,
-    parent_endpoint_id = 0,
-}
-
--- Due to nesting relationship inside Segment/Span/TracingContext at the 
runtime,
--- RefProtocol is created to prepare JSON format serialization.
--- Following SkyWalking official trace protocol v2
--- 
https://github.com/apache/skywalking-data-collect-protocol/blob/master/language-agent-v2/trace.proto
-local RefProtocol = {
-    -- Constant in LUA, no cross-thread
-    refType = 'CrossProcess',
-    parentTraceSegmentId,
-    parentSpanId,
-    parentServiceInstanceId,
-    networkAddress,
-    networkAddressId,
-    entryServiceInstanceId,
-    entryEndpoint,
-    entryEndpointId,
-    parentEndpoint,
-    parentEndpointId,
-}
-
-function SegmentRef:new()
-    local o = {}
-    setmetatable(o, self)
-    self.__index = self
-
-    return o
+if Util.is_ngx_lua then
+    encode_base64 = ngx.encode_base64
+    decode_base64 = ngx.decode_base64
 end
 
-function RefProtocol:new()
-    local o = {}
-    setmetatable(o, self)
-    self.__index = self
-
-    return o
+local _M = {}
+-- local SegmentRef = {
+--     -- There is no multiple-threads scenario in the LUA, no only hard coded 
as CROSS_PROCESS
+--     type = 'CROSS_PROCESS',
+--     trace_id,
+--     segment_id,
+--     span_id,
+--     network_address,
+--     network_address_id = 0,
+--     entry_service_instance_id = 0,
+--     parent_service_instance_id = 0,
+--     entry_endpoint_name,
+--     entry_endpoint_id = 0,
+--     parent_endpoint_name,
+--     parent_endpoint_id = 0,
+-- }
+
+function _M.new()
+    return {
+        type = 'CROSS_PROCESS',
+        network_address_id = 0,
+        entry_service_instance_id = 0,
+        parent_service_instance_id = 0,
+        entry_endpoint_id = 0,
+        parent_endpoint_id = 0,
+    }
 end
 
 -- Deserialize value from the propagated context and initialize the SegmentRef
-function SegmentRef:fromSW6Value(value)
+function _M.fromSW6Value(value)
+    local ref = _M.new()
+
     local parts = Util.split(value, '-')
     if #parts ~= 9 then
         return nil
     end
 
-    self.trace_id = Util.formatID(Base64.decode(parts[2]))
-    self.segment_id = Util.formatID(Base64.decode(parts[3]))
-    self.span_id = tonumber(parts[4])
-    self.parent_service_instance_id = tonumber(parts[5])
-    self.entry_service_instance_id = tonumber(parts[6])
-    local peerStr = Base64.decode(parts[7])
+    ref.trace_id = Util.formatID(decode_base64(parts[2]))
+    ref.segment_id = Util.formatID(decode_base64(parts[3]))
+    ref.span_id = tonumber(parts[4])
+    ref.parent_service_instance_id = tonumber(parts[5])
+    ref.entry_service_instance_id = tonumber(parts[6])
+    local peerStr = decode_base64(parts[7])
     if string.sub(peerStr, 1, 1) == '#' then
-        self.network_address = string.sub(peerStr, 2)
+        ref.network_address = string.sub(peerStr, 2)
     else
-        self.network_address_id = tonumber(peerStr)
+        ref.network_address_id = tonumber(peerStr)
     end
-    local entryEndpointStr = Base64.decode(parts[8])
+    local entryEndpointStr = decode_base64(parts[8])
     if string.sub(entryEndpointStr, 1, 1) == '#' then
-        self.entry_endpoint_name = string.sub(entryEndpointStr, 2)
+        ref.entry_endpoint_name = string.sub(entryEndpointStr, 2)
     else
-        self.entry_endpoint_id = tonumber(entryEndpointStr)
+        ref.entry_endpoint_id = tonumber(entryEndpointStr)
     end
-    local parentEndpointStr = Base64.decode(parts[9])
+    local parentEndpointStr = decode_base64(parts[9])
     if string.sub(parentEndpointStr, 1, 1) == '#' then
-        self.parent_endpoint_name = string.sub(parentEndpointStr, 2)
+        ref.parent_endpoint_name = string.sub(parentEndpointStr, 2)
     else
-        self.parent_endpoint_id = tonumber(parentEndpointStr)
+        ref.parent_endpoint_id = tonumber(parentEndpointStr)
     end
 
-    return self
+    return ref
 end
 
 -- Return string to represent this ref.
-function SegmentRef:serialize()
+function _M.serialize(ref)
     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
+    encodedRef = encodedRef .. '-' .. 
encode_base64(Util.id2String(ref.trace_id))
 
 Review comment:
   better performance.
   
   ```lua
       encodedRef = encodedRef .. '-' .. 
encode_base64(Util.id2String(ref.trace_id))
                    .. encodedRef .. '-' .. 
encode_base64(Util.id2String(ref.segment_id))
                    .. -- ...
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to