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

Emit log message for incompatible Lograge setup #3839

Merged
merged 1 commit into from
Aug 13, 2024
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
16 changes: 16 additions & 0 deletions lib/datadog/tracing/contrib/lograge/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def target_version

# patch applies our patch
def patch
# ActiveSupport::TaggedLogging is the default Rails logger since Rails 5
if defined?(::ActiveSupport::TaggedLogging::Formatter) &&
::Lograge::LogSubscribers::ActionController
.logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)

Datadog.logger.error(
'Lograge and ActiveSupport::TaggedLogging (the default Rails log formatter) 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
Expand Down
21 changes: 21 additions & 0 deletions spec/datadog/tracing/contrib/lograge/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@

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 'without Rails tagged logging' do
it 'does not log incompatibility error' do
expect(Datadog.logger).to_not receive(:error)

described_class.patch
end
end

context 'with Rails tagged logging' do
it 'logs an incompatibility error' do
logger = ActiveSupport::TaggedLogging.new(Logger.new(File::NULL))
stub_const('Lograge::LogSubscribers::ActionController', double('controller', logger: logger))

expect(Datadog.logger).to receive(:error).with(/ActiveSupport::TaggedLogging/)

described_class.patch
end
end
end
end
Loading