Skip to content

Commit

Permalink
feat: Use Time for timestamps
Browse files Browse the repository at this point in the history
Convert Time to the appropriate nanosecond format in LogRecordData.
Remove Float option from argument declaration.
  • Loading branch information
kaylareopelle committed Jul 13, 2024
1 parent 0f0e3eb commit d360759
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 45 deletions.
13 changes: 6 additions & 7 deletions logs_api/lib/opentelemetry/logs/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class Logger

# Emit a {LogRecord} to the processing pipeline.
#
# @param [optional Float, Time] timestamp Time in nanoseconds since Unix
# epoch when the event occurred measured by the origin clock, i.e. the
# time at the source.
# @param [optional Float, Time] observed_timestamp Time in nanoseconds
# since Unix epoch when the event was observed by the collection system.
# Intended default: Process.clock_gettime(Process::CLOCK_REALTIME)
# @param [optional Integer] severity_number Numerical value of the
# @param timestamp [optional Time] Time when the event occurred.
# @param observed_timestamp [optional Time] Time when the event was
# observed by the collection system.
# @param context [optional Context] The Context to associate with the
# LogRecord. Intended default: OpenTelemetry::Context.current
# @param severity_number [optional Integer] Numerical value of the
# severity. Smaller numerical values correspond to less severe events
# (such as debug events), larger numerical values correspond to more
# severe events (such as errors and critical events).
Expand Down
11 changes: 6 additions & 5 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class LogRecord < OpenTelemetry::Logs::LogRecord

# Creates a new {LogRecord}.
#
# @param [optional Float, Time] timestamp Time when the event occurred.
# @param [optional Float, Time] observed_timestamp Time when the event
# @param [optional Time] timestamp Time when the event occurred.
# @param [optional Time] observed_timestamp Time when the event
# was observed by the collection system. If nil, will first attempt
# to set to `timestamp`. If `timestamp` is nil, will set to
# `Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)`.
# to set to `timestamp`. If `timestamp` is nil, will set to Time.now.
# @param [optional OpenTelemetry::Trace::SpanContext] span_context The
# OpenTelemetry::Trace::SpanContext to associate with the LogRecord.
# @param [optional String] severity_text The log severity, also known as
# log level.
# @param [optional Integer] severity_number The numerical value of the
Expand Down Expand Up @@ -62,7 +63,7 @@ def initialize(
logger: nil
)
@timestamp = timestamp
@observed_timestamp = observed_timestamp || timestamp || Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
@observed_timestamp = observed_timestamp || timestamp || Time.now
@severity_text = severity_text
@severity_number = severity_number
@body = body
Expand Down
16 changes: 6 additions & 10 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ module Logs
:instrumentation_scope, # OpenTelemetry::SDK::InstrumentationScope
:total_recorded_attributes) do # Integer
def unix_nano_timestamp
if timestamp.is_a?(Time)
(timestamp.to_r * 1_000_000_000).to_i
else
timestamp
end
return unless timestamp.is_a?(Time)

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

def unix_nano_observed_timestamp
if timestamp.is_a?(Time)
(timestamp.to_r * 1_000_000_000).to_i
else
timestamp
end
return unless observed_timestamp.is_a?(Time)

(observed_timestamp.to_r * 10**9).to_i
end
end
end
Expand Down
36 changes: 17 additions & 19 deletions logs_sdk/lib/opentelemetry/sdk/logs/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ def resource

# Emit a {LogRecord} to the processing pipeline.
#
# @param [optional Float, Time] timestamp Time in nanoseconds since Unix
# epoch when the event occurred measured by the origin clock, i.e. the
# time at the source.
# @param [optional Float, Time] observed_timestamp Time in nanoseconds
# since Unix epoch when the event was observed by the collection system.
# Intended default: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
# @param [optional String] severity_text Original string representation of
# the severity as it is known at the source. Also known as log level.
# @param [optional Integer] severity_number Numerical value of the
# @param [optional Time] timestamp Time when the event occurred.
# @param [optional Time] observed_timestamp Time when the event was
# observed by the collection system.
# @param [optional OpenTelemetry::Trace::SpanContext] span_context The
# OpenTelemetry::Trace::SpanContext to associate with the
# {LogRecord}.
# @param severity_number [optional Integer] Numerical value of the
# severity. Smaller numerical values correspond to less severe events
# (such as debug events), larger numerical values correspond to more
# severe events (such as errors and critical events).
Expand All @@ -68,18 +66,18 @@ def resource
#
# @api public
def on_emit(timestamp: nil,
observed_timestamp: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
severity_text: nil,
severity_number: nil,
body: nil,
attributes: nil,
trace_id: nil,
span_id: nil,
trace_flags: nil,
context: OpenTelemetry::Context.current)
observed_timestamp: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
severity_text: nil,
severity_number: nil,
body: nil,
attributes: nil,
trace_id: nil,
span_id: nil,
trace_flags: nil,
context: OpenTelemetry::Context.current)

current_span = OpenTelemetry::Trace.current_span(context)
span_context = current_span.context unless OpenTelemetry::Trace::Span::INVALID == current_span
span_context = current_span.context unless current_span == OpenTelemetry::Trace::Span::INVALID
log_record = LogRecord.new(timestamp: timestamp,
observed_timestamp: observed_timestamp,
severity_text: severity_text,
Expand Down
40 changes: 40 additions & 0 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_data_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::SDK::Logs::LogRecordData do
LOGS = OpenTelemetry::SDK::Logs

describe 'unix_nano_timestamp' do
it 'does not error if given a non-time object' do
data = OpenTelemetry::SDK::Logs::LogRecordData.new(timestamp: 'fake')

assert_nil data.unix_nano_timestamp
end

it 'converts the object to integer' do
time = Time.now
data = OpenTelemetry::SDK::Logs::LogRecordData.new(timestamp: time)

assert_instance_of Integer, data.unix_nano_timestamp
end
end

describe 'unix_nano_observed_timestamp' do
it 'does not error if given a non-time object' do
data = OpenTelemetry::SDK::Logs::LogRecordData.new(observed_timestamp: 'fake')

assert_nil data.unix_nano_observed_timestamp
end

it 'converts the object to integer' do
data = OpenTelemetry::SDK::Logs::LogRecordData.new(observed_timestamp: Time.now)

assert_instance_of Integer, data.unix_nano_observed_timestamp
end
end
end
4 changes: 2 additions & 2 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'test_helper'

describe OpenTelemetry::SDK::Logs::LogRecord do
Logs = OpenTelemetry::SDK::Logs # rubocop:disable Lint/ConstantDefinitionInBlock
Logs = OpenTelemetry::SDK::Logs
let(:log_record) { Logs::LogRecord.new(**args) }
let(:args) { {} }
let(:logger) { Logs::Logger.new('', '', Logs::LoggerProvider.new) }
Expand Down Expand Up @@ -57,7 +57,7 @@
# I'm going to assert it's an Integer, which is the
# Process.clock_gettime return value class when passed the
# :nanosecond option
assert_instance_of(Integer, log_record.observed_timestamp)
assert_instance_of(Time, log_record.observed_timestamp)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
it 'sends the log record to the processors' do
mock_log_record = Minitest::Mock.new
mock_context = Minitest::Mock.new
def mock_context.value(key); OpenTelemetry::Trace::Span::INVALID; end
def mock_context.value(key); OpenTelemetry::Trace::Span::INVALID; end # rubocop:disable Style/SingleLineMethods

logger_provider.add_log_record_processor(mock_log_record_processor)
mock_log_record_processor.expect(:on_emit, nil, [mock_log_record, mock_context])
Expand Down
2 changes: 1 addition & 1 deletion logs_sdk/test/opentelemetry/sdk/logs/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
it 'sends the newly-created log record to the processors' do
mock_log_record = Minitest::Mock.new
mock_context = Minitest::Mock.new
def mock_context.value(key); OpenTelemetry::Trace::Span::INVALID; end
def mock_context.value(key); OpenTelemetry::Trace::Span::INVALID; end # rubocop:disable Style/SingleLineMethods

OpenTelemetry::SDK::Logs::LogRecord.stub(:new, ->(_) { mock_log_record }) do
mock_log_record_processor = Minitest::Mock.new
Expand Down

0 comments on commit d360759

Please sign in to comment.