Skip to content

Commit

Permalink
Update error handling for shutdown and force_flush
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle committed Oct 4, 2023
1 parent 26ef80d commit 3c0dd57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
18 changes: 8 additions & 10 deletions logs_sdk/lib/opentelemetry/sdk/logs/logger_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ module Logs
class LoggerProvider < OpenTelemetry::Logs::LoggerProvider
attr_reader :resource, :log_record_processors

EMPTY_NAME_ERROR = 'LoggerProvider#logger called without '\
'providing a logger name.'
FORCE_FLUSH_ERROR = 'unexpected error in ' \
'OpenTelemetry::SDK::Logs::LoggerProvider#force_flush'

UNEXPECTED_ERROR_MESSAGE = 'unexpected error in ' \
'OpenTelemetry::SDK::Logs::LoggerProvider#%s'
# Returns a new LoggerProvider instance.
#
# @param [optional Resource] resource The resource to associate with
Expand Down Expand Up @@ -44,11 +41,9 @@ def logger(name = nil, version = nil)
name ||= ''
version ||= ''

OpenTelemetry.logger.warn(EMPTY_NAME_ERROR) if name.empty?
OpenTelemetry.logger.warn('LoggerProvider#logger called without providing a logger name.') if name.empty?

@mutex.synchronize do
OpenTelemetry::SDK::Logs::Logger.new(name, version, self)
end
OpenTelemetry::SDK::Logs::Logger.new(name, version, self)
end

# Adds a new log record processor to this LoggerProvider's
Expand Down Expand Up @@ -92,6 +87,9 @@ def shutdown(timeout: nil)
@stopped = true
results.max || OpenTelemetry::SDK::Logs::Export::SUCCESS
end
rescue StandardError => e
OpenTelemetry.handle_error(exception: e, message: UNEXPECTED_ERROR_MESSAGE % __method__)
Export::FAILURE
end

# Immediately export all {LogRecord}s that have not yet been exported
Expand Down Expand Up @@ -120,7 +118,7 @@ def force_flush(timeout: nil)
results.max || Export::SUCCESS
end
rescue StandardError => e
OpenTelemetry.handle_error(exception: e, message: FORCE_FLUSH_ERROR)
OpenTelemetry.handle_error(exception: e, message: UNEXPECTED_ERROR_MESSAGE % __method__)
Export::FAILURE
end
end
Expand Down
36 changes: 28 additions & 8 deletions logs_sdk/test/opentelemetry/sdk/logs/logger_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@
end

describe '#logger' do
let(:error_text) { /LoggerProvider#logger called without providing a logger name/ }

it 'logs a warning if name is nil' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
logger_provider.logger(nil)
assert_match(
/#{OpenTelemetry::SDK::Logs::LoggerProvider::EMPTY_NAME_ERROR}/,
log_stream.string
)
assert_match(error_text, log_stream.string)
end
end

it 'logs a warning if name is an empty string' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
logger_provider.logger('')
assert_match(
/#{OpenTelemetry::SDK::Logs::LoggerProvider::EMPTY_NAME_ERROR}/,
log_stream.string
)
assert_match(error_text, log_stream.string)
end
end

Expand Down Expand Up @@ -129,6 +125,21 @@
assert_equal(OpenTelemetry::SDK::Logs::Export::TIMEOUT, logger_provider.shutdown)
end
end

it 'logs an error when error is raised' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
OpenTelemetry::Common::Utilities.stub :timeout_timestamp, -> { raise StandardError.new, 'fail' } do
logger_provider.shutdown
assert_match(/LoggerProvider#shutdown/, log_stream.string)
end
end
end

it 'returns a failure code when an error is raised' do
OpenTelemetry::Common::Utilities.stub :timeout_timestamp, -> { raise StandardError.new, 'fail' } do
assert_equal(OpenTelemetry::SDK::Logs::Export::FAILURE, logger_provider.shutdown)
end
end
end

describe '#force_flush' do
Expand Down Expand Up @@ -171,5 +182,14 @@
assert_equal(OpenTelemetry::SDK::Logs::Export::FAILURE, logger_provider.force_flush)
end
end

it 'logs an error when error is raised' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
OpenTelemetry::Common::Utilities.stub :timeout_timestamp, -> { raise StandardError.new, 'fail' } do
logger_provider.shutdown
assert_match(/LoggerProvider#shutdown/, log_stream.string)
end
end
end
end
end

0 comments on commit 3c0dd57

Please sign in to comment.