forked from open-telemetry/opentelemetry-ruby-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Reinstate active support logger tests
- Loading branch information
1 parent
b0da0f1
commit 18a45d1
Showing
6 changed files
with
116 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 45 additions & 27 deletions
72
...on/logger/test/opentelemetry/instrumentation/logger/patches/active_support_logger_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |