Skip to content

Commit

Permalink
Merge pull request #89 from plantfansam/noo-context
Browse files Browse the repository at this point in the history
Use different context instance for each noop span
  • Loading branch information
yangxikun authored Mar 13, 2024
2 parents 6e44fe8 + c5f41e0 commit c783239
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/opentelemetry/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local baggage_context_key = "__opentelemetry_baggage__"
-- @return context
--------------------------------------------------------------------------------
function _M.new(entries, span)
return setmetatable({ sp = span or noop_span, entries = entries or {} }, mt)
return setmetatable({ sp = span or noop_span.new(), entries = entries or {} }, mt)
end

--------------------------------------------------------------------------------
Expand Down
12 changes: 10 additions & 2 deletions lib/opentelemetry/trace/noop_span.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
local empty_span_context = require("opentelemetry.trace.span_context").new()
local empty_span_context = require("opentelemetry.trace.span_context")

local _M = {
}

local mt = {
__index = _M
}

function _M.new()
return setmetatable({ ctx = empty_span_context.new() }, mt)
end

function _M.context(self)
return empty_span_context
return self.ctx
end

function _M.is_recording()
Expand Down
12 changes: 12 additions & 0 deletions spec/context_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ describe("current", function()
otel_global.set_context_storage(ctx_storage)
assert.are.equal(ctx_2, context.current())
end)

it("instantiates different noop spans when no span provided", function()
local ctx_1 = context.new()
local ctx_2 = context.new()

-- accessing span context on different contexts gives different object back
assert.are_not.equal(ctx_1.sp:context(), ctx_2.sp:context())
assert.are_not.equal(ctx_1.sp:context().trace_state, ctx_2.sp:context().trace_state)

-- accessing span context on same context gives same object back
assert.are.equal(ctx_1.sp:context(), ctx_1.sp:context())
end)
end)

describe("with_span", function()
Expand Down

0 comments on commit c783239

Please sign in to comment.