Skip to content

Commit

Permalink
test: Reinstate active support logger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle committed Jul 12, 2024
1 parent b0da0f1 commit 18a45d1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 126 deletions.
2 changes: 2 additions & 0 deletions instrumentation/logger/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions instrumentation/logger/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions instrumentation/logger/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
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) }
c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)
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
158 changes: 62 additions & 96 deletions instrumentation/logger/test/test_helpers/app_config.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 18a45d1

Please sign in to comment.