diff --git a/lib/opentelemetry/trace/recording_span.lua b/lib/opentelemetry/trace/recording_span.lua index a1f402b..35fdd82 100644 --- a/lib/opentelemetry/trace/recording_span.lua +++ b/lib/opentelemetry/trace/recording_span.lua @@ -21,15 +21,16 @@ local mt = { -- @config optional config -- config.kind: span kind -- config.attributes: span attributes +-- @start_time optional start time -- @return span ------------------------------------------------------------------ -function _M.new(tracer, parent_ctx, ctx, name, config) +function _M.new(tracer, parent_ctx, ctx, name, config, start_time) local self = { tracer = tracer, parent_ctx = parent_ctx, ctx = ctx, name = name, - start_time = util.time_nano(), + start_time = start_time or util.time_nano(), end_time = 0, kind = span_kind.validate(config.kind), attributes = config.attributes or {}, diff --git a/lib/opentelemetry/trace/tracer.lua b/lib/opentelemetry/trace/tracer.lua index 0114aa3..c28636d 100644 --- a/lib/opentelemetry/trace/tracer.lua +++ b/lib/opentelemetry/trace/tracer.lua @@ -19,7 +19,7 @@ function _M.new(provider, il) return setmetatable(self, mt) end -local function new_span(self, context, name, config) +local function new_span(self, context, name, config, start_time) local span_context = context:span_context() if not config then config = {} @@ -52,7 +52,7 @@ local function new_span(self, context, name, config) if not sampling_result:is_recording() then span = non_recording_span_new(self, new_span_context) else - span = recording_span_new(self, span_context, new_span_context, name, config) + span = recording_span_new(self, span_context, new_span_context, name, config, start_time) end return context:with_span(span), span @@ -66,12 +66,13 @@ end -- @span_start_config [optional] -- span_start_config.kind: opentelemetry.trace.span_kind.* -- span_start_config.attributes: a list of attribute +-- @start_time [optional] start time -- @return -- context: new context with span -- span ------------------------------------------------------------------ -function _M.start(self, context, span_name, span_start_config) - return new_span(self, context, span_name, span_start_config) +function _M.start(self, context, span_name, span_start_config, start_time) + return new_span(self, context, span_name, span_start_config, start_time) end return _M diff --git a/t/trace/span_finish_timestamp.t b/t/trace/span_timestamps.t similarity index 79% rename from t/trace/span_finish_timestamp.t rename to t/trace/span_timestamps.t index ef5961e..c8efefd 100644 --- a/t/trace/span_finish_timestamp.t +++ b/t/trace/span_timestamps.t @@ -8,7 +8,7 @@ run_tests(); __DATA__ -=== TEST 1: span end timestamp can be set explicitly +=== TEST 1: span start and end timestamps can be set explicitly --- config location = /t { content_by_lua_block { @@ -19,8 +19,11 @@ location = /t { local attr = require("opentelemetry.attribute") local tracer = tracer_provider:tracer("unit_test") local context, recording_span = tracer:start(context, "recording", - {kind = span_kind.producer, attributes = {attr.string("key", "value")}}) + {kind = span_kind.producer, attributes = {attr.string("key", "value")}}, 123456788) context.sp:finish(123456789) + if context.sp.start_time ~= 123456788 then + ngx.log(ngx.ERR, "start time should have been 123456788, was " .. context.sp.start_time) + end if context.sp.end_time ~= 123456789 then ngx.log(ngx.ERR, "end time should have been 123456789, was " .. context.sp.end_time) end