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/11/04 10:06:29 UTC
[skywalking-nginx-lua] branch master updated: perf: local cache
some variables and reuse them in Lua module. (#50)
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 7c7915b perf: local cache some variables and reuse them in Lua module. (#50)
7c7915b is described below
commit 7c7915b594cf46009f2d0756d3bd13d4e81ad301
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Wed Nov 4 18:06:22 2020 +0800
perf: local cache some variables and reuse them in Lua module. (#50)
---
.gitignore | 1 +
lib/skywalking/tracer.lua | 95 +++++++++++++++++++++++++++++------------------
2 files changed, 59 insertions(+), 37 deletions(-)
diff --git a/.gitignore b/.gitignore
index c09bccb..ff457e4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,4 @@ packages/
.externalToolBuilders
/test/e2e/agent-test-tools/dist
/t/servroot/
+go
diff --git a/lib/skywalking/tracer.lua b/lib/skywalking/tracer.lua
index c8de70a..edd7d0d 100644
--- a/lib/skywalking/tracer.lua
+++ b/lib/skywalking/tracer.lua
@@ -15,43 +15,51 @@
-- limitations under the License.
--
local Span = require('skywalking.span')
+local TC = require('skywalking.tracing_context')
+local Layer = require('skywalking.span_layer')
+local Segment = require('skywalking.segment')
+local Util = require('skywalking.util')
+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 metadata_buffer = ngx.shared.tracing_buffer
- local TC = require('skywalking.tracing_context')
- local Layer = require('skywalking.span_layer')
- local tracingContext
- local serviceName = metadata_buffer:get("serviceName")
- local serviceInstanceName = metadata_buffer:get('serviceInstanceName')
- tracingContext = TC.new(serviceName, serviceInstanceName)
+function Tracer:start(upstream_name, correlation)
+ local serviceName = metadata_shdict:get("serviceName")
+ local serviceInstanceName = metadata_shdict:get('serviceInstanceName')
+ local tracingContext = TC.new(serviceName, serviceInstanceName)
-- Constant pre-defined in SkyWalking main repo
-- 6000 represents Nginx
- local nginxComponentId = 6000
local contextCarrier = {}
- contextCarrier["sw8"] = ngx.req.get_headers()["sw8"]
- contextCarrier["sw8-correlation"] = ngx.req.get_headers()["sw8-correlation"]
+ contextCarrier["sw8"] = ngx.var.http_sw8
+ contextCarrier["sw8-correlation"] = ngx.var.http_sw8_correlation
+
+ local time_now = ngx.now() * 1000
local entrySpan = TC.createEntrySpan(tracingContext, ngx.var.uri, nil, contextCarrier)
- Span.start(entrySpan, ngx.now() * 1000)
+ Span.start(entrySpan, time_now)
Span.setComponentId(entrySpan, nginxComponentId)
Span.setLayer(entrySpan, Layer.HTTP)
Span.tag(entrySpan, 'http.method', ngx.req.get_method())
- Span.tag(entrySpan, 'http.params', ngx.var.scheme .. '://' .. ngx.var.host .. ngx.var.request_uri )
+ Span.tag(entrySpan, 'http.params',
+ ngx.var.scheme .. '://' .. ngx.var.host .. ngx.var.request_uri )
contextCarrier = {}
-- Use the same URI to represent incoming and forwarding requests
-- Change it if you need.
local upstreamUri = ngx.var.uri
-
local upstreamServerName = upstream_name
------------------------------------------------------
- local exitSpan = TC.createExitSpan(tracingContext, upstreamUri, entrySpan, upstreamServerName, contextCarrier, correlation)
- Span.start(exitSpan, ngx.now() * 1000)
+ local exitSpan = TC.createExitSpan(tracingContext, upstreamUri, entrySpan,
+ upstreamServerName, contextCarrier, correlation)
+ Span.start(exitSpan, time_now)
Span.setComponentId(exitSpan, nginxComponentId)
Span.setLayer(exitSpan, Layer.HTTP)
@@ -60,9 +68,10 @@ function Tracer:start(upstream_name, correlation)
end
-- Push the data in the context
- ngx.ctx.tracingContext = tracingContext
- ngx.ctx.entrySpan = entrySpan
- ngx.ctx.exitSpan = exitSpan
+ local ctx = ngx.ctx
+ ctx.tracingContext = tracingContext
+ ctx.entrySpan = entrySpan
+ ctx.exitSpan = exitSpan
end
function Tracer:finish()
@@ -78,25 +87,37 @@ function Tracer:finish()
end
function Tracer:prepareForReport()
- local TC = require('skywalking.tracing_context')
- local Segment = require('skywalking.segment')
- if ngx.ctx.entrySpan ~= nil then
- local ngxstatus = ngx.var.status
- Span.tag(ngx.ctx.entrySpan, 'http.status', ngxstatus)
- if tonumber(ngxstatus) >= 500 then
- Span.errorOccurred(ngx.ctx.entrySpan)
- end
- Span.finish(ngx.ctx.entrySpan, ngx.now() * 1000)
- local status, segment = TC.drainAfterFinished(ngx.ctx.tracingContext)
- if status then
- local segmentJson = require('cjson').encode(Segment.transform(segment))
- ngx.log(ngx.DEBUG, 'segment = ', segmentJson)
-
- local queue = ngx.shared.tracing_buffer
- local length = queue:lpush('segment', segmentJson)
- ngx.log(ngx.DEBUG, 'segment buffer size = ', queue:llen('segment'))
- end
+ 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 segmentJson, err = json.encode(Segment.transform(segment))
+ if not segmentJson then
+ ngx.log(ngx.ERR, "failed to encode segment: ", err)
+ return
+ end
+ ngx.log(ngx.DEBUG, 'segment = ', segmentJson)
+
+ local length, err = metadata_shdict:lpush('segment', segmentJson)
+ if not length then
+ ngx.log(ngx.ERR, "failed to push segment: ", err)
+ return
end
+ ngx.log(ngx.DEBUG, 'segment buffer size = ', length)
end
return Tracer