Skip to content

Commit

Permalink
feat: Move nanosecond conversion to LogRecord
Browse files Browse the repository at this point in the history
The LogRecordData definition says the time is already
Integer nanoseconds since epoch, so move the logic to convert
the timestamp to LogRecord#to_log_record_data
  • Loading branch information
kaylareopelle committed Jul 15, 2024
1 parent d360759 commit 0fb4606
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 63 deletions.
12 changes: 10 additions & 2 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def initialize(

def to_log_record_data
LogRecordData.new(
@timestamp,
@observed_timestamp,
to_integer_nanoseconds(@timestamp),
to_integer_nanoseconds(@observed_timestamp),
@severity_text,
@severity_number,
@body,
Expand All @@ -92,6 +92,14 @@ def to_log_record_data
@total_recorded_attributes
)
end

private

def to_integer_nanoseconds(timestamp)
return unless timestamp.is_a?(Time)

t = (timestamp.to_r * 10**9).to_i
end
end
end
end
Expand Down
18 changes: 3 additions & 15 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,15 @@ module Logs
:severity_text, # optional String
:severity_number, # optional Integer
:body, # optional String, Numeric, Boolean, Array<String, Numeric,
# Boolean>, Hash{String => String, Numeric, Boolean,
# Array<String, Numeric, Boolean>}
# Boolean>, Hash{String => String, Numeric, Boolean,
# Array<String, Numeric, Boolean>}
:attributes, # optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}
:trace_id, # optional String (16-byte binary)
:span_id, # optional String (8-byte binary)
:trace_flags, # optional Integer (8-bit byte of bit flags)
:resource, # optional OpenTelemetry::SDK::Resources::Resource
:instrumentation_scope, # OpenTelemetry::SDK::InstrumentationScope
:total_recorded_attributes) do # Integer
def unix_nano_timestamp
return unless timestamp.is_a?(Time)

(timestamp.to_r * 10**9).to_i
end

def unix_nano_observed_timestamp
return unless observed_timestamp.is_a?(Time)

(observed_timestamp.to_r * 10**9).to_i
end
end
:total_recorded_attributes) # Integer
end
end
end
2 changes: 1 addition & 1 deletion logs_sdk/lib/opentelemetry/sdk/logs/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def resource
#
# @api public
def on_emit(timestamp: nil,
observed_timestamp: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
observed_timestamp: Time.now,
severity_text: nil,
severity_number: nil,
body: nil,
Expand Down
40 changes: 0 additions & 40 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_data_test.rb

This file was deleted.

10 changes: 5 additions & 5 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

describe 'when timestamp is present' do
let(:timestamp) { Process.clock_gettime(Process::CLOCK_REALTIME) }
let(:timestamp) { Time.now }
let(:args) { { timestamp: timestamp } }

it 'is equal to timestamp' do
Expand Down Expand Up @@ -66,8 +66,8 @@
let(:args) do
span_context = OpenTelemetry::Trace::SpanContext.new
{
timestamp: Process.clock_gettime(Process::CLOCK_REALTIME),
observed_timestamp: Process.clock_gettime(Process::CLOCK_REALTIME),
timestamp: Time.now,
observed_timestamp: Time.now + 1,
severity_text: 'DEBUG',
severity_number: 0,
body: 'body',
Expand All @@ -82,8 +82,8 @@
it 'transforms the LogRecord into a LogRecordData' do
log_record_data = log_record.to_log_record_data

assert_equal(args[:timestamp], log_record_data.timestamp)
assert_equal(args[:observed_timestamp], log_record_data.observed_timestamp)
assert_equal(args[:timestamp].strftime("%s%N").to_i, log_record_data.timestamp)
assert_equal(args[:observed_timestamp].strftime("%s%N").to_i, log_record_data.observed_timestamp)
assert_equal(args[:severity_text], log_record_data.severity_text)
assert_equal(args[:severity_number], log_record_data.severity_number)
assert_equal(args[:body], log_record_data.body)
Expand Down

0 comments on commit 0fb4606

Please sign in to comment.