Skip to content

Commit

Permalink
test: Misc logs fixes (#1697)
Browse files Browse the repository at this point in the history
* test: Add LogRecordProcessor tests

* test: Update observed_timestamp LogRecord test

Tests for timestamp were updated to use Time objects, but
observed_timestamp was not. Now, they both evaluate Time.
They are turned into floats when
converted to LogRecordData.

* test: Exclude test directory from SimpleCov

* style: Misc docs/spacing updates

* test: Skip flaky JRuby test with link to fix issue

---------

Co-authored-by: Matthew Wear <matthew.wear@gmail.com>
  • Loading branch information
kaylareopelle and mwear authored Sep 4, 2024
1 parent 0f5cbfc commit 3ff81bd
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion logs_api/test/opentelemetry/logs/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
describe OpenTelemetry::Logs::Logger do
let(:logger) { OpenTelemetry::Logs::Logger.new }

describe '#emit' do
describe '#on_emit' do
it 'returns nil, as it is a no-op method' do
assert_nil(logger.on_emit)
end
Expand Down
7 changes: 6 additions & 1 deletion logs_api/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
# SPDX-License-Identifier: Apache-2.0

require 'simplecov'
SimpleCov.start { enable_coverage :branch }

SimpleCov.start do
enable_coverage :branch
add_filter '/test/'
end

SimpleCov.minimum_coverage 85

require 'opentelemetry-logs-api'
Expand Down
2 changes: 1 addition & 1 deletion logs_sdk/lib/opentelemetry/sdk/logs/log_record_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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>}
:attributes, # optional 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)
Expand Down
4 changes: 2 additions & 2 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def on_emit(log_record, context); end
# the process after an invocation, but before the `Processor` exports
# the completed spans.
#
# @param [Numeric] timeout An optional timeout in seconds.
# @param [optional Numeric] timeout An optional timeout in seconds.
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
def force_flush(timeout: nil)
Expand All @@ -35,7 +35,7 @@ def force_flush(timeout: nil)

# Called when {LoggerProvider#shutdown} is called.
#
# @param [Numeric] timeout An optional timeout in seconds.
# @param [optional Numeric] timeout An optional timeout in seconds.
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
def shutdown(timeout: nil)
Expand Down
2 changes: 2 additions & 0 deletions logs_sdk/lib/opentelemetry/sdk/logs/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def initialize(name, version, logger_provider)
# @param [optional OpenTelemetry::Trace::SpanContext] span_context The
# OpenTelemetry::Trace::SpanContext to associate with the
# {LogRecord}.
# @param [optional String] severity_text Original string representation of
# the severity as it is known at the source. Also known as log level.
# @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def to_log_record_data
end

it 'logs a warning if a log record was emitted after the buffer is full' do
# This will be fixed as part of Issue #1701
# https://github.com/open-telemetry/opentelemetry-ruby/issues/1701
skip if RUBY_ENGINE == 'jruby'

mock_otel_logger = Minitest::Mock.new
mock_otel_logger.expect(:warn, nil, ['1 log record(s) dropped. Reason: buffer-full'])

Expand Down
33 changes: 33 additions & 0 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_processor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

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

require 'test_helper'

describe OpenTelemetry::SDK::Logs::LogRecordProcessor do
let(:processor) { OpenTelemetry::SDK::Logs::LogRecordProcessor.new }
let(:log_record) { nil }
let(:context) { nil }

it 'implements #on_emit' do
processor.on_emit(log_record, context)
end

it 'implements #force_flush' do
processor.force_flush
end

it 'returns a success code when #force_flush is called' do
assert(OpenTelemetry::SDK::Logs::Export::SUCCESS, processor.force_flush)
end

it 'implements #shutdown' do
processor.shutdown
end

it 'returns a success code when #shutdown is called' do
assert(OpenTelemetry::SDK::Logs::Export::SUCCESS, processor.shutdown)
end
end
10 changes: 3 additions & 7 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
describe '#initialize' do
describe 'observed_timestamp' do
describe 'when observed_timestamp is present' do
let(:observed_timestamp) { '1692661486.2841358' }
let(:current_time) { Time.now }
let(:observed_timestamp) { current_time + 1 }
let(:args) { { observed_timestamp: observed_timestamp } }

it 'is equal to observed_timestamp' do
Expand All @@ -26,13 +27,8 @@
refute_equal(log_record.timestamp, log_record.observed_timestamp)
end

# Process.clock_gettime is used to set the current time
# That method returns a Float. Since the stubbed value of
# observed_timestamp is a String, we can know the the
# observed_timestamp was not set to the value of Process.clock_gettime
# by making sure its value is not a Float.
it 'is not equal to the current time' do
refute_instance_of(Float, log_record.observed_timestamp)
refute_equal(current_time, log_record.observed_timestamp)
end
end

Expand Down
7 changes: 6 additions & 1 deletion logs_sdk/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
# SPDX-License-Identifier: Apache-2.0

require 'simplecov'
SimpleCov.start { enable_coverage :branch }

SimpleCov.start do
enable_coverage :branch
add_filter '/test/'
end

SimpleCov.minimum_coverage 85

require 'opentelemetry-logs-api'
Expand Down

0 comments on commit 3ff81bd

Please sign in to comment.