Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling of install messages #831

Merged
merged 5 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/lib/opentelemetry/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def configure
end

require 'opentelemetry/sdk/configurator'
require 'opentelemetry/sdk/forwarding_logger'
require 'opentelemetry/sdk/internal'
require 'opentelemetry/sdk/instrumentation_library'
require 'opentelemetry/sdk/resources'
Expand Down
11 changes: 10 additions & 1 deletion sdk/lib/opentelemetry/sdk/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Configurator # rubocop:disable Metrics/ClassLength

private_constant :USE_MODE_UNSPECIFIED, :USE_MODE_ONE, :USE_MODE_ALL

attr_writer :logger, :propagators, :error_handler, :id_generator
attr_writer :propagators, :error_handler, :id_generator

def initialize
@instrumentation_names = []
Expand All @@ -31,6 +31,15 @@ def logger
@logger ||= OpenTelemetry.logger
end

# Accepts a logger and wraps it in the {ForwardingLogger} which allows
# for controlling the severity level emitted by the OpenTelemetry.logger
# independently of the supplied logger.
#
# @param [Logger] new_logger The logger for OpenTelemetry to use
def logger=(new_logger)
@logger = ForwardingLogger.new(new_logger, level: ENV['OTEL_LOG_LEVEL'] || Logger::INFO)
end

def error_handler
@error_handler ||= OpenTelemetry.error_handler
end
Expand Down
69 changes: 69 additions & 0 deletions sdk/lib/opentelemetry/sdk/forwarding_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require 'logger'

module OpenTelemetry
module SDK
# The ForwardingLogger provides a wrapper to control the OpenTelemetry
# log level, while respecting the configured level of the supplied logger.
# If the OTEL_LOG_LEVEL is set to debug, and the supplied logger is configured
# with an ERROR log level, only OpenTelemetry logs at the ERROR level or higher
# will be emitted.
class ForwardingLogger
def initialize(logger, level:) # rubocop:disable Metrics/CyclomaticComplexity
@logger = logger

if level.is_a?(Integer)
@level = level
else
case level.to_s.downcase
when 'debug'
@level = Logger::DEBUG
when 'info'
@level = Logger::INFO
when 'warn'
@level = Logger::WARN
when 'error'
@level = Logger::ERROR
when 'fatal'
@level = Logger::FATAL
when 'unknown'
@level = Logger::UNKNOWN
else
raise ArgumentError, "invalid log level: #{level}"
end
end
end

def add(severity, message = nil, progname = nil)
return true if severity < @level

@logger.add(severity, message, progname)
end

def debug(progname = nil, &block)
add(Logger::DEBUG, nil, progname, &block)
end

def info(progname = nil, &block)
add(Logger::INFO, nil, progname, &block)
end

def warn(progname = nil, &block)
add(Logger::WARN, nil, progname, &block)
end

def error(progname = nil, &block)
add(Logger::ERROR, nil, progname, &block)
end

def fatal(progname = nil, &block)
add(Logger::FATAL, nil, progname, &block)
end

def unknown(progname = nil, &block)
add(Logger::UNKNOWN, nil, progname, &block)
end
end
end
end
31 changes: 29 additions & 2 deletions sdk/test/opentelemetry/sdk/configurator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,41 @@
end

describe '#logger' do
# Reset the logger
after { OpenTelemetry.logger = Logger.new(File::NULL) }

it 'returns a logger instance' do
_(configurator.logger).must_be_instance_of(Logger)
end

it 'assigns the logger to OpenTelemetry.logger' do
custom_logger = Logger.new(File::NULL, level: 'ERROR')
custom_logger = Logger.new(File::NULL, level: 'INFO')
_(OpenTelemetry.logger).wont_equal custom_logger

OpenTelemetry::SDK.configure { |c| c.logger = custom_logger }
_(OpenTelemetry.logger.instance_variable_get(:@logger)).must_equal custom_logger
_(OpenTelemetry.logger).must_be_instance_of(OpenTelemetry::SDK::ForwardingLogger)
end

it 'respects the supplied loggers severity level' do
log_stream = StringIO.new
custom_logger = Logger.new(log_stream, level: 'ERROR')
OpenTelemetry::SDK.configure { |c| c.logger = custom_logger }
_(OpenTelemetry.logger).must_equal custom_logger

OpenTelemetry.logger.debug('The forwarding logger should forward this message')
_(log_stream.string).must_be_empty
end

it 'allows control of the otel log level' do
log_stream = StringIO.new
custom_logger = Logger.new(log_stream, level: 'DEBUG')

with_env('OTEL_LOG_LEVEL' => 'ERROR') do
OpenTelemetry::SDK.configure { |c| c.logger = custom_logger }
end

OpenTelemetry.logger.warn('The forwarding logger should not forward this message')
_(log_stream.string).must_be_empty
end
end

Expand Down