From 4ae2cd96af36dfd7bb8ebe8aa5957a25ca28eaa2 Mon Sep 17 00:00:00 2001 From: Rees Dooley Date: Tue, 26 Mar 2024 08:50:44 -0700 Subject: [PATCH 1/2] Allow user to specify start time * only use for recording spans * yangxikun/opentelemetry-lua#86 --- lib/opentelemetry/trace/recording_span.lua | 5 +++-- lib/opentelemetry/trace/tracer.lua | 9 +++++---- spec/trace/exporter/otlp_spec.lua | 6 ++++-- t/trace/{span_finish_timestamp.t => span_timestamps.t} | 7 +++++-- 4 files changed, 17 insertions(+), 10 deletions(-) rename t/trace/{span_finish_timestamp.t => span_timestamps.t} (79%) 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/spec/trace/exporter/otlp_spec.lua b/spec/trace/exporter/otlp_spec.lua index 20ed8d6..0dd88e6 100644 --- a/spec/trace/exporter/otlp_spec.lua +++ b/spec/trace/exporter/otlp_spec.lua @@ -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) @@ -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") end) it("one resource span and two ils for spans from distinct tracers", function() 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 From 0985ff595c85e27c57c5258d02ebfd1429643ded Mon Sep 17 00:00:00 2001 From: Rees Dooley Date: Tue, 26 Mar 2024 14:09:20 -0700 Subject: [PATCH 2/2] Update lib/opentelemetry/trace/tracer.lua Co-authored-by: Sam <370182+plantfansam@users.noreply.github.com> --- lib/opentelemetry/trace/tracer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/opentelemetry/trace/tracer.lua b/lib/opentelemetry/trace/tracer.lua index c28636d..5b12a4c 100644 --- a/lib/opentelemetry/trace/tracer.lua +++ b/lib/opentelemetry/trace/tracer.lua @@ -66,7 +66,7 @@ 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 +-- @start_time [optional] start time: nanoseconds -- @return -- context: new context with span -- span