Skip to content

Commit

Permalink
Merge pull request yangxikun#27 from plantfansam/context-fallback
Browse files Browse the repository at this point in the history
wrap context_storage in getter; fallback to empty context
  • Loading branch information
yangxikun committed Aug 25, 2022
2 parents 5bb51c9 + b1ca373 commit 2dddb60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
21 changes: 12 additions & 9 deletions lib/opentelemetry/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ end
-- @return token to be used for detaching
--------------------------------------------------------------------------------
function _M.attach(self)
if otel_global.context_storage[context_key] then
table.insert(otel_global.context_storage[context_key], self)
if otel_global.get_context_storage()[context_key] then
table.insert(otel_global.get_context_storage()[context_key], self)
else
otel_global.context_storage[context_key] = { self }
otel_global.get_context_storage()[context_key] = { self }
end

-- the length of the stack is token used to detach context
return #otel_global.context_storage[context_key]
return #otel_global.get_context_storage()[context_key]
end

--------------------------------------------------------------------------------
Expand All @@ -47,12 +47,12 @@ end
-- @return boolean, string
--------------------------------------------------------------------------------
function _M.detach(self, token)
if #otel_global.context_storage[context_key] == token then
table.remove(otel_global.context_storage[context_key])
if #otel_global.get_context_storage()[context_key] == token then
table.remove(otel_global.get_context_storage()[context_key])
return true, nil
else
local error_message = "Token does not match (" ..
#otel_global.context_storage[context_key] ..
#otel_global.get_context_storage()[context_key] ..
" context entries in stack, token provided was " .. token .. ")."
ngx.log(ngx.WARN, error_message)
return false, error_message
Expand All @@ -66,8 +66,11 @@ end
-- @return boolean, string
--------------------------------------------------------------------------------
function _M.current()
return otel_global.context_storage[context_key] and
otel_global.context_storage[context_key][#otel_global.context_storage[context_key]]
if otel_global.get_context_storage()[context_key] then
return otel_global.get_context_storage()[context_key][#otel_global.get_context_storage()[context_key]]
else
return _M.new()
end
end

--------------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion lib/opentelemetry/global.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_M = { context_storage = ngx.ctx }
_M = { context_storage = nil }

function _M.set_tracer_provider(tp)
_M.tracer_provider = tp
Expand All @@ -12,6 +12,10 @@ function _M.tracer(name, opts)
return _M.tracer_provider:tracer(name, opts)
end

function _M.get_context_storage()
return _M.context_storage or ngx.ctx
end

function _M.set_context_storage(context_storage)
_M.context_storage = context_storage
end
Expand Down
4 changes: 2 additions & 2 deletions t/context.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ location = /t {
local tracer_provider = require("opentelemetry.trace.tracer_provider").new()
local tracer = tracer_provider:tracer("unit_test")
local context, recording_span = tracer:start(context, "recording")
if context.current() ~= nil then
ngx.say("unexpected context.current()")
if context.current().sp:is_recording() ~= false then
ngx.say("expected context.current() span to be non-recording")
end
context:attach()
if context.current():span().name ~= "recording" then
Expand Down

0 comments on commit 2dddb60

Please sign in to comment.