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

Allow user to specify start time #94

Merged
merged 2 commits into from
Apr 5, 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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add arg to docstring above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh of course, great catch

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: nanoseconds
-- @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
6 changes: 4 additions & 2 deletions spec/trace/exporter/otlp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe("encode_spans", function()
local ctx = context.new()
local spans = {}
for i=10,1,-1 do
ctx, span = tracer:start(ctx, "test span" .. i)
span:finish()
ctx, span = tracer:start(ctx, "test span" .. i, {}, 123456788)
span:finish(123456789)
table.insert(spans, span)
end
local cb = exporter.new(nil)
Expand All @@ -36,6 +36,8 @@ describe("encode_spans", function()
local resource = encoded.resource_spans[1]
assert(#resource.instrumentation_library_spans == 1)
assert(#resource.instrumentation_library_spans[1].spans == 10)
assert(resource.instrumentation_library_spans[1].spans[1].start_time_unix_nano == "123456788")
assert(resource.instrumentation_library_spans[1].spans[1].end_time_unix_nano == "123456789")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nice addition here to check that we are setting and exporting the times we expect on spans

end)

it("one resource span and two ils for spans from distinct tracers", function()
Expand Down
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
Loading