Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different context instance for each noop span #89

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading