diff --git a/logs_sdk/lib/opentelemetry/sdk/logs/logger_provider.rb b/logs_sdk/lib/opentelemetry/sdk/logs/logger_provider.rb index a16b67884..f71700e5c 100644 --- a/logs_sdk/lib/opentelemetry/sdk/logs/logger_provider.rb +++ b/logs_sdk/lib/opentelemetry/sdk/logs/logger_provider.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/logs_sdk/test/opentelemetry/sdk/logs/logger_provider_test.rb b/logs_sdk/test/opentelemetry/sdk/logs/logger_provider_test.rb index 57a558181..72aadf8b9 100644 --- a/logs_sdk/test/opentelemetry/sdk/logs/logger_provider_test.rb +++ b/logs_sdk/test/opentelemetry/sdk/logs/logger_provider_test.rb @@ -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 @@ -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 @@ -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