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

Fix hash log_tags warning #3022

Merged
merged 4 commits into from
Aug 9, 2023
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
9 changes: 6 additions & 3 deletions lib/datadog/tracing/contrib/rails/log_injection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ module LogInjection
module_function

# Use `app.config.log_tags` to inject propagation tags into the default Rails logger.
def configure_log_tags(app)
app.config.log_tags ||= [] # Can be nil, we initialized it if so
app.config.log_tags << proc { Tracing.log_correlation if Datadog.configuration.tracing.log_injection }
def configure_log_tags(app_config)
# When using SemanticLogger, app_config.log_tags could be a Hash and should not be modified here
return unless app_config.log_tags.nil? || app_config.log_tags.respond_to?(:<<)

app_config.log_tags ||= [] # Can be nil, we initialized it if so
app_config.log_tags << proc { Tracing.log_correlation if Datadog.configuration.tracing.log_injection }
rescue StandardError => e
Datadog.logger.warn(
"Unable to add Datadog Trace context to ActiveSupport::TaggedLogging: #{e.class.name} #{e.message}"
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/contrib/rails/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def before_initialize(app)
# Sometimes we don't want to activate middleware e.g. OpenTracing, etc.
add_middleware(app) if Datadog.configuration.tracing[:rails][:middleware]

Rails::LogInjection.configure_log_tags(app)
Rails::LogInjection.configure_log_tags(app.config)
end
end

Expand Down
53 changes: 53 additions & 0 deletions spec/datadog/tracing/contrib/rails/log_injection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'datadog/tracing/contrib/rails/log_injection'

RSpec.describe Datadog::Tracing::Contrib::Rails::LogInjection do
describe '.configure_log_tags' do
context 'when given `nil` as `log_tags` configuration ' do
it 'sets an array and adds a proc to the config' do
expect(Datadog.logger).not_to receive(:warn)

config = OpenStruct.new(log_tags: nil)

expect do
described_class.configure_log_tags(config)
end.to change { config.log_tags }.from(nil).to(be_an(Array).and(have(1).item))
end
end

context 'when given an array as `log_tags` configuration ' do
it 'adds a proc to the config' do
expect(Datadog.logger).not_to receive(:warn)

config = OpenStruct.new(log_tags: [])

expect do
described_class.configure_log_tags(config)
end.to change { config.log_tags }.from([]).to(be_an(Array).and(have(1).item))
end
end

context 'when given a hash as `log_tags` configuration ' do
it 'deos not change the configuration' do
expect(Datadog.logger).not_to receive(:warn)

config = OpenStruct.new(log_tags: {})

expect do
described_class.configure_log_tags(config)
end.not_to(change { config.log_tags })
end
end

context 'when given a string as `log_tags` configuration ' do
it 'deos not change the configuration and warn about the error' do
expect(Datadog.logger).to receive(:warn)

config = OpenStruct.new(log_tags: 'misconfigured with a string')

expect do
described_class.configure_log_tags(config)
end.not_to(change { config.log_tags })
end
end
end
end