From 18a45d1c3122a95ec1836821ff765a39afc8ff01 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 12 Jul 2024 09:56:27 -0700 Subject: [PATCH] test: Reinstate active support logger tests --- instrumentation/logger/Appraisals | 2 + instrumentation/logger/Gemfile | 3 + .../logger/patches/active_support_logger.rb | 1 + .../patches/active_support_logger_test.rb | 72 +++++--- instrumentation/logger/test/test_helper.rb | 6 +- .../logger/test/test_helpers/app_config.rb | 158 +++++++----------- 6 files changed, 116 insertions(+), 126 deletions(-) diff --git a/instrumentation/logger/Appraisals b/instrumentation/logger/Appraisals index 41ef24559..fa165e3e1 100644 --- a/instrumentation/logger/Appraisals +++ b/instrumentation/logger/Appraisals @@ -5,6 +5,8 @@ # # SPDX-License-Identifier: Apache-2.0 # # TOOD: Re-enable before release, along with active_support_logger tests +# # Appraisals wont work with gems installed by a branch, so we can't +# # set this up until there's a release of the otel logs gems # appraise 'rails-6.0' do # gem 'rails', '~> 6.0.0' # end diff --git a/instrumentation/logger/Gemfile b/instrumentation/logger/Gemfile index fef6f8723..c292f236c 100644 --- a/instrumentation/logger/Gemfile +++ b/instrumentation/logger/Gemfile @@ -20,3 +20,6 @@ group :test do gem 'opentelemetry-logs-sdk', git: 'https://github.com/kaylareopelle/opentelemetry-ruby', branch: 'log-record-processor3', glob: 'logs_sdk/*.gemspec' gem 'opentelemetry-sdk', git: 'https://github.com/kaylareopelle/opentelemetry-ruby', branch: 'log-record-processor3', glob: 'sdk/*.gemspec' end + +# Temporary for testing, Appraisal does not work with gems installed from git source +gem 'rails', '~> 7.0.0' diff --git a/instrumentation/logger/lib/opentelemetry/instrumentation/logger/patches/active_support_logger.rb b/instrumentation/logger/lib/opentelemetry/instrumentation/logger/patches/active_support_logger.rb index 5efcdc866..7d8bcff1f 100644 --- a/instrumentation/logger/lib/opentelemetry/instrumentation/logger/patches/active_support_logger.rb +++ b/instrumentation/logger/lib/opentelemetry/instrumentation/logger/patches/active_support_logger.rb @@ -13,6 +13,7 @@ module ActiveSupportLogger # The ActiveSupport::Logger.broadcast method emits identical logs to # multiple destinations. This instance variable will prevent the broadcasted # destinations from generating OpenTelemetry log record objects. + # Available in Rails 7.0 and below def broadcast(logger) logger.instance_variable_set(:@skip_instrumenting, true) super diff --git a/instrumentation/logger/test/opentelemetry/instrumentation/logger/patches/active_support_logger_test.rb b/instrumentation/logger/test/opentelemetry/instrumentation/logger/patches/active_support_logger_test.rb index b3e3d561f..e67588569 100644 --- a/instrumentation/logger/test/opentelemetry/instrumentation/logger/patches/active_support_logger_test.rb +++ b/instrumentation/logger/test/opentelemetry/instrumentation/logger/patches/active_support_logger_test.rb @@ -1,37 +1,55 @@ # frozen_string_literal: true -# # Copyright The OpenTelemetry Authors -# # -# # SPDX-License-Identifier: Apache-2.0 +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 -# require 'test_helper' +require 'test_helper' -# require_relative '../../../../../lib/opentelemetry/instrumentation/logger/patches/active_support_logger' +require_relative '../../../../../lib/opentelemetry/instrumentation/logger/patches/active_support_logger' -# describe OpenTelemetry::Instrumentation::Logger::Patches::ActiveSupportLogger do -# let(:instrumentation) { OpenTelemetry::Instrumentation::Logger::Instrumentation.instance } -# let(:exporter) { EXPORTER } -# let(:log_record) { exporter.emitted_log_records.first } -# let(:rails_logger) { Rails.logger } -# let(:log_stream) { LOG_STREAM } -# # let(:ruby_logger) { Logger.new(log_stream) } -# let(:msg) { 'message' } +describe OpenTelemetry::Instrumentation::Logger::Patches::ActiveSupportLogger do + let(:instrumentation) { OpenTelemetry::Instrumentation::Logger::Instrumentation.instance } + let(:main_logger) { ActiveSupport::Logger.new(LOG_STREAM) } + let(:broadcasted_logger) { ActiveSupport::Logger.new(BROADCASTED_STREAM) } -# before do -# exporter.reset -# instrumentation.install -# end + before do + EXPORTER.reset + Rails.logger = main_logger.extend(ActiveSupport::Logger.broadcast(broadcasted_logger)) + instrumentation.install + end -# after { instrumentation.instance_variable_set(:@installed, false) } + after { instrumentation.instance_variable_set(:@installed, false) } -# describe '#broadcast' do -# it 'adds @skip_instrumenting to broadcasted loggers' do -# rails_logger.debug(msg) -# assert_match(/DEBUG -- : #{msg}/, log_stream.string) -# end + describe '#broadcast' do + it 'emits the log to the Rails.logger' do + msg = "spruce #{rand(6)}" + Rails.logger.debug(msg) -# it 'does not add @skip_instrumenting to the initial logger' do + assert_match(/#{msg}/, LOG_STREAM.string) + end -# end -# end -# end + it 'emits the broadcasted log' do + msg = "willow #{rand(6)}" + Rails.logger.debug(msg) + + assert_match(/#{msg}/, BROADCASTED_STREAM.string) + end + + it 'records the log record' do + msg = "hemlock #{rand(6)}" + Rails.logger.debug(msg) + log_record = EXPORTER.emitted_log_records.first + + assert_match(/#{msg}/, log_record.body) + end + + it 'does not add @skip_instrumenting to the initial logger' do + refute Rails.logger.instance_variable_defined?(:@skip_instrumenting) + end + + it 'adds @skip_instrumenting to broadcasted loggers' do + assert broadcasted_logger.instance_variable_defined?(:@skip_instrumenting) + end + end +end diff --git a/instrumentation/logger/test/test_helper.rb b/instrumentation/logger/test/test_helper.rb index b293cf054..547f0209f 100644 --- a/instrumentation/logger/test/test_helper.rb +++ b/instrumentation/logger/test/test_helper.rb @@ -23,6 +23,7 @@ EXPORTER = OpenTelemetry::SDK::Logs::Export::InMemoryLogRecordExporter.new log_record_processor = OpenTelemetry::SDK::Logs::Export::SimpleLogRecordProcessor.new(EXPORTER) LOG_STREAM = StringIO.new +BROADCASTED_STREAM = StringIO.new OpenTelemetry::SDK.configure do |c| c.error_handler = ->(exception:, message:) { raise(exception || message) } @@ -30,8 +31,7 @@ c.add_log_record_processor log_record_processor end -# TODO: Re-enable Rails app and active_support_logger testing # Create a globally available Rails app, this should be used in test unless # specifically testing behaviour with different initialization configs. -# DEFAULT_RAILS_APP = AppConfig.initialize_app -# Rails.application = DEFAULT_RAILS_APP +DEFAULT_RAILS_APP = AppConfig.initialize_app +Rails.application = DEFAULT_RAILS_APP diff --git a/instrumentation/logger/test/test_helpers/app_config.rb b/instrumentation/logger/test/test_helpers/app_config.rb index 7db946da9..4e050324f 100644 --- a/instrumentation/logger/test/test_helpers/app_config.rb +++ b/instrumentation/logger/test/test_helpers/app_config.rb @@ -1,98 +1,64 @@ # frozen_string_literal: true -# # Copyright The OpenTelemetry Authors -# # -# # SPDX-License-Identifier: Apache-2.0 - -# class Application < Rails::Application; end -# require 'action_controller/railtie' -# # require_relative 'middlewares' -# # require_relative 'controllers' -# # require_relative 'routes' - -# module AppConfig -# extend self - -# def initialize_app(use_exceptions_app: false, remove_rack_tracer_middleware: false) -# new_app = Application.new -# new_app.config.secret_key_base = 'secret_key_base' - -# # Ensure we don't see this Rails warning when testing -# new_app.config.eager_load = false - -# # Prevent tests from creating log/*.log -# level = ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym -# new_app.config.logger = Logger.new(LOG_STREAM, level: level) -# new_app.config.log_level = level - -# new_app.config.filter_parameters = [:param_to_be_filtered] - -# case Rails.version -# when /^6\.0/ -# apply_rails_6_0_configs(new_app) -# when /^6\.1/ -# apply_rails_6_1_configs(new_app) -# when /^7\./ -# apply_rails_7_configs(new_app) -# end - -# # remove_rack_middleware(new_app) if remove_rack_tracer_middleware -# # add_exceptions_app(new_app) if use_exceptions_app -# # add_middlewares(new_app) - -# new_app.initialize! - -# # draw_routes(new_app) - -# new_app -# end - -# private - -# # def remove_rack_middleware(application) -# # application.middleware.delete( -# # OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware -# # ) -# # end - -# # def add_exceptions_app(application) -# # application.config.exceptions_app = lambda do |env| -# # ExceptionsController.action(:show).call(env) -# # end -# # end - -# # def add_middlewares(application) -# # application.middleware.insert_after( -# # ActionDispatch::DebugExceptions, -# # ExceptionRaisingMiddleware -# # ) - -# # application.middleware.insert_after( -# # ActionDispatch::DebugExceptions, -# # RedirectMiddleware -# # ) -# # end - -# def apply_rails_6_0_configs(application) -# # Required in Rails 6 -# application.config.hosts << 'example.org' -# # Creates a lot of deprecation warnings on subsequent app initializations if not explicitly set. -# application.config.action_view.finalize_compiled_template_methods = ActionView::Railtie::NULL_OPTION -# end - -# def apply_rails_6_1_configs(application) -# # Required in Rails 6 -# application.config.hosts << 'example.org' -# end - -# def apply_rails_7_configs(application) -# # Required in Rails 7 -# application.config.hosts << 'example.org' - -# # Unfreeze values which may have been frozen on previous initializations. -# ActiveSupport::Dependencies.autoload_paths = -# ActiveSupport::Dependencies.autoload_paths.dup -# ActiveSupport::Dependencies.autoload_once_paths = -# ActiveSupport::Dependencies.autoload_once_paths.dup -# end -# end +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +class Application < Rails::Application; end +require 'action_controller/railtie' + +module AppConfig + extend self + + def initialize_app(use_exceptions_app: false, remove_rack_tracer_middleware: false) + app = Application.new + app.config.secret_key_base = 'secret_key_base' + + # Ensure we don't see this Rails warning when testing + app.config.eager_load = false + + # Prevent tests from creating log/*.log + level = ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym + app.config.logger = ActiveSupport::Logger.new(LOG_STREAM, level: level) + app.config.log_level = level + app.config.filter_parameters = [:param_to_be_filtered] + + case Rails.version + when /^6\.0/ + apply_rails_6_0_configs(app) + when /^6\.1/ + apply_rails_6_1_configs(app) + when /^7\./ + apply_rails_7_configs(app) + end + + app.initialize! + + app + end + + private + + def apply_rails_6_0_configs(application) + # Required in Rails 6 + application.config.hosts << 'example.org' + # Creates a lot of deprecation warnings on subsequent app initializations if not explicitly set. + application.config.action_view.finalize_compiled_template_methods = ActionView::Railtie::NULL_OPTION + end + + def apply_rails_6_1_configs(application) + # Required in Rails 6 + application.config.hosts << 'example.org' + end + + def apply_rails_7_configs(application) + # Required in Rails 7 + application.config.hosts << 'example.org' + + # Unfreeze values which may have been frozen on previous initializations. + ActiveSupport::Dependencies.autoload_paths = + ActiveSupport::Dependencies.autoload_paths.dup + ActiveSupport::Dependencies.autoload_once_paths = + ActiveSupport::Dependencies.autoload_once_paths.dup + end +end