Support batch segment report (#37)

* support batch segment report

* adapt the new agent test tool

Co-authored-by: Mrproliu <mrproliu@lagou.com>
diff --git a/lib/skywalking/client.lua b/lib/skywalking/client.lua
index 09eeade..b00a59a 100644
--- a/lib/skywalking/client.lua
+++ b/lib/skywalking/client.lua
@@ -15,6 +15,8 @@
 -- limitations under the License.
 --
 
+local SEGMENT_BATCH_COUNT = 100
+
 local Client = {}
 
 -- Tracing timer reports instance properties report, keeps alive and sends traces
@@ -131,6 +133,36 @@
     end
 end
 
+-- Send segemnts data to backend
+local function sendSegments(segmentTransform, backend_http_uri)
+    local log = ngx.log
+    local DEBUG = ngx.DEBUG
+    local ERR = ngx.ERR
+
+    local http = require('resty.http')
+    local httpc = http.new()
+
+    local res, err = httpc:request_uri(backend_http_uri .. '/v3/segments', {
+        method = "POST",
+        body = segmentTransform,
+        headers = {
+            ["Content-Type"] = "application/json",
+        },
+    })
+
+    if err == nil then
+        if res.status ~= 200 then
+            log(ERR, "Segment report fails, response code ", res.status)
+            return false
+        end
+    else
+        log(ERR, "Segment report fails, ", err)
+        return false
+    end
+
+    return true
+end
+
 -- Report trace segments to the backend
 function Client:reportTraces(metadata_buffer, backend_http_uri)
     local log = ngx.log
@@ -139,39 +171,39 @@
 
     local queue = ngx.shared.tracing_buffer
     local segment = queue:rpop('segment')
+    local segmentTransform = ''
 
-    local count = 0;
-
-    local http = require('resty.http')
-    local httpc = http.new()
+    local count = 0
+    local totalCount = 0
 
     while segment ~= nil
     do
-        local res, err = httpc:request_uri(backend_http_uri .. '/v3/segments', {
-            method = "POST",
-            body = segment,
-            headers = {
-                ["Content-Type"] = "application/json",
-            },
-        })
-
-        if err == nil then
-            if res.status ~= 200 then
-                log(ERR, "Segment report fails, response code ", res.status)
-                break
-            else
-                count = count + 1
-            end
-        else
-            log(ERR, "Segment report fails, ", err)
-            break
+        if #segmentTransform > 0 then
+            segmentTransform = segmentTransform .. ','
         end
 
+        segmentTransform = segmentTransform .. segment
         segment = queue:rpop('segment')
+        count = count + 1
+
+        if count >= SEGMENT_BATCH_COUNT then
+            if sendSegments('[' .. segmentTransform .. ']', backend_http_uri) then
+                totalCount = totalCount + count
+            end
+
+            segmentTransform = ''
+            count = 0
+        end
     end
 
-    if count > 0 then
-        log(DEBUG, count,  " segments reported.")
+    if #segmentTransform > 0 then
+        if sendSegments('[' .. segmentTransform .. ']', backend_http_uri) then
+            totalCount = totalCount + count
+        end
+    end
+
+    if totalCount > 0 then
+        log(DEBUG, totalCount,  " segments reported.")
     end
 end
 
diff --git a/test/e2e/agent-test-tools/pom.xml b/test/e2e/agent-test-tools/pom.xml
index c6efbfb..30d972d 100644
--- a/test/e2e/agent-test-tools/pom.xml
+++ b/test/e2e/agent-test-tools/pom.xml
@@ -38,7 +38,7 @@
         <docker.image.name>skywalking-collector</docker.image.name>
         <docker-maven-plugin.version>0.4.13</docker-maven-plugin.version>
         <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
-        <agent-test-tools.version>1f009d692b99896c08cf9f26440f92287f0364e7</agent-test-tools.version>
+        <agent-test-tools.version>ed896e044f146cf607441d375da4830e8d0547f8</agent-test-tools.version>
         <agent-test-tools.workingDirectory>${project.basedir}/target/agent-test-tools</agent-test-tools.workingDirectory>
         <agent-test-tools.repos>https://github.com/apache/skywalking-agent-test-tool.git</agent-test-tools.repos>
     </properties>