Skip to content

Commit

Permalink
Allow user to specify start time
Browse files Browse the repository at this point in the history
* only use for recording spans
* #86
  • Loading branch information
rdooley committed Mar 26, 2024
1 parent 5443c9d commit 538fafd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/opentelemetry/trace/recording_span.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {},
Expand Down
9 changes: 5 additions & 4 deletions lib/opentelemetry/trace/tracer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand All @@ -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
7 changes: 5 additions & 2 deletions t/trace/span_finish_timestamp.t → t/trace/span_timestamps.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 538fafd

Please sign in to comment.