Skip to content

Commit 95dc94a

Browse files
authored
Fix issue with OTel timestamps (#257)
1 parent f1339c4 commit 95dc94a

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

temporalio/lib/temporalio/contrib/open_telemetry.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,12 @@ def self.completed_span(
455455
# Create span, which has to be done with illegal call disabling because OTel asks for full exception message
456456
# which uses error highlighting and such which accesses File#path
457457
Temporalio::Workflow::Unsafe.illegal_call_tracing_disabled do
458-
time = Temporalio::Workflow.now
459-
timestamp = (time.to_i * 1_000_000_000) + time.nsec
460-
span = root.tracer.start_span(name, attributes:, links:, start_timestamp: timestamp, kind:) # steep:ignore
458+
time = Temporalio::Workflow.now.dup
459+
span = root.tracer.start_span(name, attributes:, links:, start_timestamp: time, kind:) # steep:ignore
461460
# Record exception if present
462461
span.record_exception(exception) if exception
463462
# Finish the span (returns self)
464-
span.finish(end_timestamp: timestamp)
463+
span.finish(end_timestamp: time)
465464
end
466465
end
467466
end

temporalio/test/contrib/open_telemetry_test.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def trace(
143143
tracer_and_exporter: init_tracer_and_exporter,
144144
always_create_workflow_spans: false,
145145
check_root: true,
146+
append_finished_spans_to: nil,
146147
&
147148
)
148149
tracer, exporter = tracer_and_exporter
@@ -160,6 +161,7 @@ def trace(
160161
end
161162

162163
# Convert spans, confirm there is only the outer, and return children
164+
append_finished_spans_to&.append(*exporter.finished_spans)
163165
spans = ExpectedSpan.from_span_data(exporter.finished_spans)
164166
if check_root
165167
assert_equal 1, spans.size
@@ -174,9 +176,10 @@ def trace_workflow(
174176
start_with_untraced_client: false,
175177
always_create_workflow_spans: false,
176178
check_root: true,
179+
append_finished_spans_to: nil,
177180
&
178181
)
179-
trace(tracer_and_exporter:, always_create_workflow_spans:, check_root:) do |client|
182+
trace(tracer_and_exporter:, always_create_workflow_spans:, check_root:, append_finished_spans_to:) do |client|
180183
# Must capture and attach outer context
181184
outer_context = OpenTelemetry::Context.current
182185
attach_token = nil
@@ -318,7 +321,8 @@ def test_handler_failures
318321

319322
def test_activity
320323
exp_root = ExpectedSpan.new(name: 'root')
321-
act_root = trace_workflow(:wait_on_signal) do |handle|
324+
raw_finished_spans = []
325+
act_root = trace_workflow(:wait_on_signal, append_finished_spans_to: raw_finished_spans) do |handle|
322326
exp_cl_attrs = { 'temporalWorkflowID' => handle.id }
323327
exp_run_attrs = exp_cl_attrs.merge({ 'temporalRunID' => handle.result_run_id })
324328
exp_start_wf = exp_root.add_child(name: 'StartWorkflow:TestWorkflow', attributes: exp_cl_attrs)
@@ -375,6 +379,11 @@ def test_activity
375379
handle.execute_update(TestWorkflow.update, :call_local_activity, id: 'my-update-id2')
376380
end
377381
assert_equal exp_root.to_s_indented, act_root.to_s_indented
382+
383+
# Confirm the custom in-workflow span time is within 5m of the given time
384+
custom_span = raw_finished_spans.find { |span| span.name == 'custom-workflow-span' } || raise
385+
assert_equal custom_span.start_timestamp, custom_span.end_timestamp
386+
assert_in_delta Time.now, Time.at(0, custom_span.start_timestamp, :nanosecond), 5 * 60.0
378387
end
379388

380389
def test_client_fail

temporalio/test/sig/contrib/open_telemetry_test.rbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module Contrib
44
def trace: (
55
?tracer_and_exporter: [untyped, untyped],
66
?always_create_workflow_spans: bool,
7-
?check_root: bool
7+
?check_root: bool,
8+
?append_finished_spans_to: Array[untyped]?
89
) { (Temporalio::Client) -> void } -> untyped
910
def trace_workflow: (
1011
Symbol scenario,
1112
?tracer_and_exporter: [untyped, untyped],
1213
?start_with_untraced_client: bool,
1314
?always_create_workflow_spans: bool,
14-
?check_root: bool
15+
?check_root: bool,
16+
?append_finished_spans_to: Array[untyped]?
1517
) { (Temporalio::Client::WorkflowHandle) -> void } -> untyped
1618

1719
class ExpectedSpan

0 commit comments

Comments
 (0)