From 83c0965e2f1231757375b48e890e76c77ab7ebfb Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Fri, 9 Aug 2024 16:05:22 -0700 Subject: [PATCH] Emit log message for incompatible Lograge setup --- Gemfile | 7 ++++++ .../tracing/contrib/lograge/patcher.rb | 14 ++++++++++++ .../tracing/contrib/lograge/patcher_spec.rb | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/Gemfile b/Gemfile index 28ab18df764..9d1be1a2b01 100644 --- a/Gemfile +++ b/Gemfile @@ -93,3 +93,10 @@ end # # TODO: Remove this once the issue is resolved: https://github.com/ffi/ffi/issues/1107 gem 'ffi', '~> 1.16.3', require: false + +gem 'rails', '~> 6.1.0' +gem 'pg', '>= 1.1', platform: :ruby +gem 'sprockets', '< 4' +gem 'lograge', '~> 0.11' +gem 'net-smtp' +gem 'grpc' \ No newline at end of file diff --git a/lib/datadog/tracing/contrib/lograge/patcher.rb b/lib/datadog/tracing/contrib/lograge/patcher.rb index e27bc8bbfa2..ae04f148a7b 100644 --- a/lib/datadog/tracing/contrib/lograge/patcher.rb +++ b/lib/datadog/tracing/contrib/lograge/patcher.rb @@ -20,6 +20,20 @@ def target_version # patch applies our patch def patch + # ActiveSupport::TaggedLogging is the default Rails logger since Rails 5 + if defined?(::Rails::VERSION::MAJOR) && ::Rails::VERSION::MAJOR >= 5 + Datadog.logger.error( + "Lograge and ActiveSupport::TaggedLogging (the default Rails logger) are not compatible: " \ + "Lograge does not account for Rails log tags, creating polluted logs and breaking log formatting. " \ + "Traces and Logs correlation may not work. " \ + "Either: 1. Disable tagged logging in your Rails configuration " \ + "`config.logger = ActiveSupport::Logger.new(STDOUT); " \ + "config.active_job.logger = ActiveSupport::Logger.new(STDOUT)` " \ + "or 2. Use the `semantic_logger` gem instead of `lograge`." + ) + end + + ::Lograge::LogSubscribers::Base.include(Instrumentation) end end diff --git a/spec/datadog/tracing/contrib/lograge/patcher_spec.rb b/spec/datadog/tracing/contrib/lograge/patcher_spec.rb index 23536af465b..c7b3d72e75c 100644 --- a/spec/datadog/tracing/contrib/lograge/patcher_spec.rb +++ b/spec/datadog/tracing/contrib/lograge/patcher_spec.rb @@ -5,10 +5,32 @@ RSpec.describe Datadog::Tracing::Contrib::Lograge::Patcher do describe '.patch' do + before { described_class.instance_variable_get(:@patch_only_once)&.send(:reset_ran_once_state_for_tests) } + it 'adds Instrumentation to ancestors of LogSubscribers::Base class' do described_class.patch expect(Lograge::LogSubscribers::Base.ancestors).to include(Datadog::Tracing::Contrib::Lograge::Instrumentation) end + + context 'with older Rails' do + it 'does not log incompatibility error' do + stub_const('Rails::VERSION::MAJOR', 4) + + expect(Datadog.logger).to_not receive(:error) + + described_class.patch + end + end + + context 'with Rails with tagged logging' do + it 'logs an incompatibility error' do + stub_const('Rails::VERSION::MAJOR', 5) + + expect(Datadog.logger).to receive(:error).with(/ActiveSupport::TaggedLogging/) + + described_class.patch + end + end end end