diff --git a/lib/opentelemetry/context.lua b/lib/opentelemetry/context.lua index 831f305..2dd44e6 100644 --- a/lib/opentelemetry/context.lua +++ b/lib/opentelemetry/context.lua @@ -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 -------------------------------------------------------------------------------- diff --git a/lib/opentelemetry/trace/noop_span.lua b/lib/opentelemetry/trace/noop_span.lua index 54dca73..13ff56b 100644 --- a/lib/opentelemetry/trace/noop_span.lua +++ b/lib/opentelemetry/trace/noop_span.lua @@ -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() diff --git a/spec/context_spec.lua b/spec/context_spec.lua index 3963741..57821d2 100644 --- a/spec/context_spec.lua +++ b/spec/context_spec.lua @@ -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()