Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

### Features

- Auto-enable Rails structured logging when `enable_logs` is true ([#2721](https://github.com/getsentry/sentry-ruby/pull/2721))

### Internal

- Fix leftover `config.logger` call in `graphql` patch ([#2722](https://github.com/getsentry/sentry-ruby/2722)
Expand Down
14 changes: 12 additions & 2 deletions sentry-rails/lib/sentry/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Sentry
class Configuration
attr_reader :rails

add_post_initialization_callback do
after(:initialize) do
@rails = Sentry::Rails::Configuration.new
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)

Expand All @@ -33,6 +33,10 @@ class Configuration
end
end
end

after(:configured) do
rails.structured_logging.enabled = enable_logs if rails.structured_logging.enabled.nil?
end
end

module Rails
Expand Down Expand Up @@ -202,9 +206,15 @@ class StructuredLoggingConfiguration
}.freeze

def initialize
@enabled = false
@enabled = nil
@subscribers = DEFAULT_SUBSCRIBERS.dup
end

# Returns true if structured logging should be enabled.
# @return [Boolean]
def enabled?
enabled
end
end
end
end
2 changes: 1 addition & 1 deletion sentry-rails/lib/sentry/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def activate_tracing
end

def activate_structured_logging
if Sentry.configuration.rails.structured_logging.enabled && Sentry.configuration.enable_logs
if Sentry.configuration.rails.structured_logging.enabled? && Sentry.configuration.enable_logs
Sentry::Rails::StructuredLogging.attach(Sentry.configuration.rails.structured_logging)
end
end
Expand Down
29 changes: 27 additions & 2 deletions sentry-rails/spec/sentry/rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MySubscriber; end
config.rails.structured_logging.enabled = true
end

expect(config.structured_logging.enabled).to be(true)
expect(config.structured_logging.enabled?).to be(true)
expect(config.structured_logging.subscribers).to be_a(Hash)
end

Expand All @@ -83,10 +83,35 @@ class MySubscriber; end
config.rails.structured_logging.enabled = false
end

expect(config.structured_logging.enabled).to be(false)
expect(config.structured_logging.enabled?).to be(false)
expect(config.structured_logging.subscribers).to be_a(Hash)
end

it "auto-enables when enable_logs is true and not explicitly set" do
make_basic_app do |config|
config.enable_logs = true
end

expect(config.structured_logging.enabled?).to be(true)
end

it "remains disabled when enable_logs is false" do
make_basic_app do |config|
config.enable_logs = false
end

expect(config.structured_logging.enabled?).to be(false)
end

it "respects explicit disable even when enable_logs is true" do
make_basic_app do |config|
config.rails.structured_logging.enabled = false
config.enable_logs = true
end

expect(config.structured_logging.enabled?).to be(false)
end

it "allows customizing subscribers" do
class TestSubscriber; end

Expand Down
6 changes: 4 additions & 2 deletions sentry-rails/spec/sentry/rails/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def error_test_event(event)
make_basic_app do |config|
config.enable_logs = true
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
# Disable default structured logging subscribers to avoid interference
config.rails.structured_logging.enabled = false
end
end

Expand Down Expand Up @@ -123,8 +125,8 @@ def error_test_event(event)
duration = log_event["attributes"]["duration_ms"]

expect(duration).to be_a(Float)
expect(duration).to be >= 40.0
expect(duration).to be < 100.0
expect(duration).to be >= 10.0
expect(duration).to be < 200.0
expect(duration.round(2)).to eq(duration)
end
end
Expand Down
24 changes: 24 additions & 0 deletions sentry-rails/spec/sentry/rails/structured_logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,28 @@
expect(sentry_logs).to be_empty
end
end

context "when enable_logs is true and structured logging is auto-enabled" do
before do
make_basic_app do |config|
config.enable_logs = true
end
end

it "captures structured logs automatically" do
get "/posts"

Post.first

Sentry.get_current_client.flush

expect(sentry_logs).not_to be_empty

rails_log_events = sentry_logs.select { |log|
log.dig(:attributes, "sentry.origin", :value) == "auto.logger.rails.log_subscriber"
}

expect(rails_log_events).not_to be_empty
end
end
end
Loading