You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2021/04/27 15:49:12 UTC

[GitHub] [skywalking-nginx-lua] membphis commented on a change in pull request #81: support kafka reporter

membphis commented on a change in pull request #81:
URL: https://github.com/apache/skywalking-nginx-lua/pull/81#discussion_r621337495



##########
File path: lib/skywalking/kafka/client.lua
##########
@@ -0,0 +1,165 @@
+--
+-- 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 Const = require('skywalking.constants')
+local TC = require('skywalking.tracing_context')
+local Span = require("skywalking.span")
+local Segment = require("skywalking.segment")
+
+local ngx = ngx
+local prodcuer
+local SEGMENT_BATCH_COUNT = 100
+
+local Client = {
+    -- expose the delay for test
+    backend_timer_delay = 3 -- in seconds
+}
+
+local producer_config = {
+    producer_type = "async",
+    batch_num = SEGMENT_BATCH_COUNT,
+    flush_time = Client.backend_timer_delay * 1000
+}
+
+
+function Client:start_backend_timer(broker_list)
+    local metadata_buffer = ngx.shared.tracing_buffer
+
+    producer = require('resty.kafka.producer'):new(broker_list, producer_config)

Review comment:
       we need to `local cache` it at top level

##########
File path: lib/skywalking/kafka/client.lua
##########
@@ -0,0 +1,165 @@
+--
+-- 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 Const = require('skywalking.constants')
+local TC = require('skywalking.tracing_context')
+local Span = require("skywalking.span")
+local Segment = require("skywalking.segment")
+
+local ngx = ngx
+local prodcuer
+local SEGMENT_BATCH_COUNT = 100
+
+local Client = {
+    -- expose the delay for test
+    backend_timer_delay = 3 -- in seconds
+}
+
+local producer_config = {
+    producer_type = "async",
+    batch_num = SEGMENT_BATCH_COUNT,
+    flush_time = Client.backend_timer_delay * 1000
+}
+
+
+function Client:start_backend_timer(broker_list)
+    local metadata_buffer = ngx.shared.tracing_buffer
+
+    producer = require('resty.kafka.producer'):new(broker_list, producer_config)
+
+    local log = ngx.log
+    local ERR = ngx.ERR
+
+    -- The codes of timer setup is following the OpenResty timer doc
+    local new_timer = ngx.timer.at
+    local check
+    check = function(premature)
+        if not premature and not self.stopped then
+            local instance_properties_submitted = metadata_buffer:get("instancePropertiesSubmitted")
+            if (instance_properties_submitted) then
+                self:ping(metadata_buffer, producer)
+            else
+                self:report_service_instance(metadata_buffer, producer)
+            end
+
+            -- do the health check
+            local ok, err = new_timer(self.backend_timer_delay, check)
+            if not ok then
+                log(ERR, "failed to create timer: ", err)
+                return
+            end
+        end
+    end
+
+    if 0 == ngx.worker.id() then
+        local ok, err = new_timer(self.backend_timer_delay, check)
+        if not ok then
+            log(ERR, "failed to create timer: ", err)
+            return
+        end
+    end
+end
+
+
+-- Stop the tracing report timer and clean unreported data
+function Client:destroy_backend_timer()
+    self.stopped = true
+
+    local metadata_buffer = ngx.shared.tracing_buffer
+    local ok, err = metadata_buffer:delete(Const.segment_queue)
+    if not ok then
+        return nil, err
+    end
+
+    return true
+end
+
+
+function Client:report_service_instance(metadata_buffer, producer)
+    local log = ngx.log
+    local ERR = ngx.ERR
+
+    local service_name = metadata_buffer:get("serviceName")
+    local service_instance_name = metadata_buffer:get("serviceInstanceName")
+
+    local cjson = require('cjson')

Review comment:
       this may wrong.
   it may fail if this library was used under standard Lua.

##########
File path: lib/skywalking/kafka/client.lua
##########
@@ -0,0 +1,165 @@
+--
+-- 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 Const = require('skywalking.constants')
+local TC = require('skywalking.tracing_context')
+local Span = require("skywalking.span")
+local Segment = require("skywalking.segment")
+
+local ngx = ngx
+local prodcuer
+local SEGMENT_BATCH_COUNT = 100
+
+local Client = {
+    -- expose the delay for test
+    backend_timer_delay = 3 -- in seconds
+}
+
+local producer_config = {
+    producer_type = "async",
+    batch_num = SEGMENT_BATCH_COUNT,
+    flush_time = Client.backend_timer_delay * 1000
+}
+
+
+function Client:start_backend_timer(broker_list)
+    local metadata_buffer = ngx.shared.tracing_buffer
+
+    producer = require('resty.kafka.producer'):new(broker_list, producer_config)
+
+    local log = ngx.log
+    local ERR = ngx.ERR
+
+    -- The codes of timer setup is following the OpenResty timer doc
+    local new_timer = ngx.timer.at
+    local check
+    check = function(premature)
+        if not premature and not self.stopped then
+            local instance_properties_submitted = metadata_buffer:get("instancePropertiesSubmitted")
+            if (instance_properties_submitted) then
+                self:ping(metadata_buffer, producer)
+            else
+                self:report_service_instance(metadata_buffer, producer)
+            end
+
+            -- do the health check
+            local ok, err = new_timer(self.backend_timer_delay, check)
+            if not ok then
+                log(ERR, "failed to create timer: ", err)
+                return
+            end
+        end
+    end
+
+    if 0 == ngx.worker.id() then
+        local ok, err = new_timer(self.backend_timer_delay, check)
+        if not ok then
+            log(ERR, "failed to create timer: ", err)
+            return
+        end
+    end
+end
+
+
+-- Stop the tracing report timer and clean unreported data
+function Client:destroy_backend_timer()
+    self.stopped = true
+
+    local metadata_buffer = ngx.shared.tracing_buffer
+    local ok, err = metadata_buffer:delete(Const.segment_queue)
+    if not ok then
+        return nil, err
+    end
+
+    return true
+end
+
+
+function Client:report_service_instance(metadata_buffer, producer)
+    local log = ngx.log
+    local ERR = ngx.ERR
+
+    local service_name = metadata_buffer:get("serviceName")
+    local service_instance_name = metadata_buffer:get("serviceInstanceName")
+
+    local cjson = require('cjson')
+    local report_instance = require('skywalking.management')
+        .newReportInstanceProperties(service_name, service_instance_name)
+    local instance = require("skywalking.kafka.proto_util")
+        .instance_properties_transform(report_instance)
+
+    local _, err = producer:send("skywalking-managements", "register-" .. service_instance_name, instance)
+    if err then
+        log(ERR, "Agent register fails, ", err)
+        return false
+    else
+        return true
+    end
+end
+
+
+-- Ping the backend to update instance heartheat
+function Client:ping(metadata_buffer, producer)
+    local log = ngx.log
+    local ERR = ngx.ERR
+
+    local service_name = metadata_buffer:get("serviceName")
+    local service_instance_name = metadata_buffer:get("serviceInstanceName")
+
+    local ping_pkg = require('skywalking.management').newServiceInstancePingPkg(service_name, service_instance_name)
+    local instance_ping = require("skywalking.kafka.proto_util").instance_ping_transform(ping_pkg)
+
+    local _, err = producer:send("skywalking-managements", service_instance_name, instance_ping)
+    if err then
+        log(ERR, "Agent ping fails, ", err)
+        return false
+    else
+        return true
+    end
+end
+
+
+function Client:report()
+    local entrySpan = ngx.ctx.entrySpan
+    if not entrySpan then
+        return
+    end
+
+    local ngxstatus = ngx.var.status
+    Span.tag(entrySpan, 'http.status', ngxstatus)
+    if tonumber(ngxstatus) >= 500 then
+        Span.errorOccurred(entrySpan)
+    end
+
+    Span.finish(entrySpan, ngx.now() * 1000)
+
+    local ok, segment = TC.drainAfterFinished(ngx.ctx.tracingContext)
+    if not ok then
+        return
+    end
+
+    local proto_util = require("skywalking.kafka.proto_util")

Review comment:
       local cache

##########
File path: lib/skywalking/kafka/proto_util.lua
##########
@@ -0,0 +1,143 @@
+--
+-- 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 pb = require("pb")
+local protoc  = require("protoc")
+
+
+assert(protoc:load [[
+    syntax = "proto3";
+    message KeyStringValuePair {
+        string key = 1;
+        string value = 2;
+    }
+
+    message CPU {
+        double usagePercent = 2;
+    }
+
+    enum DetectPoint {
+        client = 0;
+        server = 1;
+        proxy = 2;
+    }
+
+    message Commands {
+        repeated Command commands = 1;
+    }
+
+    message Command {
+        string command = 1;
+        repeated KeyStringValuePair args = 2;
+    }
+
+    message InstanceProperties {
+        string service = 1;
+        string serviceInstance = 2;
+        repeated KeyStringValuePair properties = 3;
+    }
+
+    message InstancePingPkg {
+        string service = 1;
+        string serviceInstance = 2;
+    }
+
+    message SegmentObject {
+        string traceId = 1;
+        string traceSegmentId = 2;
+        repeated SpanObject spans = 3;
+        string service = 4;
+        string serviceInstance = 5;
+        bool isSizeLimited = 6;
+    }
+
+    message SegmentReference {
+        RefType refType = 1;
+        string traceId = 2;
+        string parentTraceSegmentId = 3;
+        int32 parentSpanId = 4;
+        string parentService = 5;
+        string parentServiceInstance = 6;
+        string parentEndpoint = 7;
+        string networkAddressUsedAtPeer = 8;
+    }
+
+    message SpanObject {
+        int32 spanId = 1;
+        int32 parentSpanId = 2;
+        int64 startTime = 3;
+        int64 endTime = 4;
+        repeated SegmentReference refs = 5;
+        string operationName = 6;
+        string peer = 7;
+        SpanType spanType = 8;
+        SpanLayer spanLayer = 9;
+        int32 componentId = 10;
+        bool isError = 11;
+        repeated KeyStringValuePair tags = 12;
+        repeated Log logs = 13;
+        bool skipAnalysis = 14;
+    }
+
+    message Log {
+        int64 time = 1;
+        repeated KeyStringValuePair data = 2;
+    }
+
+    enum SpanType {
+        Entry = 0;
+        Exit = 1;
+        Local = 2;
+    }
+
+    message ID {
+        repeated string id = 1;
+    }
+
+    enum RefType {
+        CrossProcess = 0;
+        CrossThread = 1;
+    }
+
+    enum SpanLayer {
+        Unknown = 0;
+        Database = 1;
+        RPCFramework = 2;
+        Http = 3;
+        MQ = 4;
+        Cache = 5;
+    }
+
+    message SegmentCollection {
+        repeated SegmentObject segments = 1;
+    } ]])
+
+local _M = {}
+
+function _M.instance_properties_transform(instance_properties)
+    return pb.encode("InstanceProperties", instance_properties)
+end
+
+function _M.instance_ping_transform(instance_properties)
+    return pb.encode("InstancePingPkg", instance_properties)
+end
+
+function _M.segment_transform(segment)
+    return pb.encode("SegmentObject", segment)
+end
+
+return _M

Review comment:
       style: need a blank line at the end of file




-- 
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:
users@infra.apache.org