Skip to content

Commit e28b19d

Browse files
committed
Fix issue with OTel timestamps
1 parent 1d8b2c6 commit e28b19d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
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
@@ -144,6 +144,7 @@ def trace(
144144
tracer_and_exporter: init_tracer_and_exporter,
145145
always_create_workflow_spans: false,
146146
check_root: true,
147+
append_finished_spans_to: nil,
147148
&
148149
)
149150
tracer, exporter = tracer_and_exporter
@@ -161,6 +162,7 @@ def trace(
161162
end
162163

163164
# Convert spans, confirm there is only the outer, and return children
165+
append_finished_spans_to&.append(*exporter.finished_spans)
164166
spans = ExpectedSpan.from_span_data(exporter.finished_spans)
165167
if check_root
166168
assert_equal 1, spans.size
@@ -175,9 +177,10 @@ def trace_workflow(
175177
start_with_untraced_client: false,
176178
always_create_workflow_spans: false,
177179
check_root: true,
180+
append_finished_spans_to: nil,
178181
&
179182
)
180-
trace(tracer_and_exporter:, always_create_workflow_spans:, check_root:) do |client|
183+
trace(tracer_and_exporter:, always_create_workflow_spans:, check_root:, append_finished_spans_to:) do |client|
181184
# Must capture and attach outer context
182185
outer_context = OpenTelemetry::Context.current
183186
attach_token = nil
@@ -319,7 +322,8 @@ def test_handler_failures
319322

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

381390
def test_client_fail

0 commit comments

Comments
 (0)