Skip to content

Commit 6742809

Browse files
committed
feat(logging): auto-enable structured_logging
When config.enable_logs is true, enable structured logging in Rails automatically too. Closes #2715
1 parent 611ef73 commit 6742809

File tree

6 files changed

+102
-19
lines changed

6 files changed

+102
-19
lines changed

CHANGELOG.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,33 @@
33
### Features
44

55
- Support for `:origin` attribute in log events ([#2712](https://github.com/getsentry/sentry-ruby/pull/2712))
6+
- Auto-enable Rails structured logging when `enable_logs` is true ([#XXXX](https://github.com/getsentry/sentry-ruby/pull/XXXX))
7+
8+
Rails structured logging is now automatically enabled when `config.enable_logs = true` is set, eliminating the need for the double opt-in. Users can still explicitly disable it by setting `config.rails.structured_logging.enabled = false` before setting `enable_logs = true`.
9+
10+
**Before:**
11+
```ruby
12+
Sentry.init do |config|
13+
config.enable_logs = true
14+
config.rails.structured_logging.enabled = true # Required
15+
end
16+
```
17+
18+
**After:**
19+
```ruby
20+
Sentry.init do |config|
21+
config.enable_logs = true
22+
# Rails structured logging is now automatically enabled!
23+
end
24+
```
25+
26+
**To explicitly disable:**
27+
```ruby
28+
Sentry.init do |config|
29+
config.rails.structured_logging.enabled = false # Explicit disable
30+
config.enable_logs = true # Won't auto-enable structured logging
31+
end
32+
```
633

734
### Bug Fixes
835

@@ -26,26 +53,20 @@
2653
Sentry.init do |config|
2754
# ... your setup ...
2855

29-
# Make sure structured logging is enabled
56+
# Enable structured logging (Rails log subscribers are auto-enabled)
3057
config.enable_logs = true
31-
32-
# Enable default Rails log subscribers (ActionController and ActiveRecord)
33-
config.rails.structured_logging.enabled = true
3458
end
3559
```
3660

37-
To configure all subscribers:
61+
To configure additional subscribers:
3862

3963
```ruby
4064
Sentry.init do |config|
4165
# ... your setup ...
4266

43-
# Make sure structured logging is enabled
67+
# Enable structured logging (Rails log subscribers are auto-enabled)
4468
config.enable_logs = true
4569

46-
# Enable Rails log subscribers
47-
config.rails.structured_logging.enabled = true
48-
4970
# Add ActionMailer and ActiveJob subscribers
5071
config.rails.structured_logging.subscribers.update(
5172
action_mailer: Sentry::Rails::LogSubscribers::ActionMailerSubscriber,

sentry-rails/lib/sentry/rails/configuration.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Configuration
1414
attr_reader :rails
1515

1616
add_post_initialization_callback do
17-
@rails = Sentry::Rails::Configuration.new
17+
@rails = Sentry::Rails::Configuration.new(self)
1818
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)
1919

2020
if ::Rails.logger
@@ -166,7 +166,7 @@ class Configuration
166166
# @return [StructuredLoggingConfiguration]
167167
attr_reader :structured_logging
168168

169-
def initialize
169+
def initialize(parent_config = nil)
170170
@register_error_subscriber = false
171171
@report_rescued_exceptions = true
172172
@skippable_job_adapters = []
@@ -183,7 +183,7 @@ def initialize
183183
@db_query_source_threshold_ms = 100
184184
@active_support_logger_subscription_items = Sentry::Rails::ACTIVE_SUPPORT_LOGGER_SUBSCRIPTION_ITEMS_DEFAULT.dup
185185
@active_job_report_on_retry_error = false
186-
@structured_logging = StructuredLoggingConfiguration.new
186+
@structured_logging = StructuredLoggingConfiguration.new(parent_config)
187187
end
188188
end
189189

@@ -192,18 +192,30 @@ class StructuredLoggingConfiguration
192192
# @return [Boolean]
193193
attr_accessor :enabled
194194

195+
private :enabled
196+
195197
# Hash of components to subscriber classes for structured logging
196198
# @return [Hash<Symbol, Class>]
197199
attr_accessor :subscribers
198200

201+
# @return [Sentry::Configuration]
202+
attr_reader :parent_config
203+
199204
DEFAULT_SUBSCRIBERS = {
200205
active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber,
201206
action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber
202207
}.freeze
203208

204-
def initialize
205-
@enabled = false
209+
def initialize(parent_config)
210+
@enabled = nil
206211
@subscribers = DEFAULT_SUBSCRIBERS.dup
212+
@parent_config = parent_config
213+
end
214+
215+
# Returns true if structured logging should be enabled.
216+
# @return [Boolean]
217+
def enabled?
218+
enabled.nil? ? parent_config.enable_logs : enabled
207219
end
208220
end
209221
end

sentry-rails/lib/sentry/rails/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def activate_tracing
140140
end
141141

142142
def activate_structured_logging
143-
if Sentry.configuration.rails.structured_logging.enabled && Sentry.configuration.enable_logs
143+
if Sentry.configuration.rails.structured_logging.enabled? && Sentry.configuration.enable_logs
144144
Sentry::Rails::StructuredLogging.attach(Sentry.configuration.rails.structured_logging)
145145
end
146146
end

sentry-rails/spec/sentry/rails/configuration_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MySubscriber; end
7474
config.rails.structured_logging.enabled = true
7575
end
7676

77-
expect(config.structured_logging.enabled).to be(true)
77+
expect(config.structured_logging.enabled?).to be(true)
7878
expect(config.structured_logging.subscribers).to be_a(Hash)
7979
end
8080

@@ -83,10 +83,35 @@ class MySubscriber; end
8383
config.rails.structured_logging.enabled = false
8484
end
8585

86-
expect(config.structured_logging.enabled).to be(false)
86+
expect(config.structured_logging.enabled?).to be(false)
8787
expect(config.structured_logging.subscribers).to be_a(Hash)
8888
end
8989

90+
it "auto-enables when enable_logs is true and not explicitly set" do
91+
make_basic_app do |config|
92+
config.enable_logs = true
93+
end
94+
95+
expect(config.structured_logging.enabled?).to be(true)
96+
end
97+
98+
it "remains disabled when enable_logs is false" do
99+
make_basic_app do |config|
100+
config.enable_logs = false
101+
end
102+
103+
expect(config.structured_logging.enabled?).to be(false)
104+
end
105+
106+
it "respects explicit disable even when enable_logs is true" do
107+
make_basic_app do |config|
108+
config.rails.structured_logging.enabled = false
109+
config.enable_logs = true
110+
end
111+
112+
expect(config.structured_logging.enabled?).to be(false)
113+
end
114+
90115
it "allows customizing subscribers" do
91116
class TestSubscriber; end
92117

sentry-rails/spec/sentry/rails/log_subscriber_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def error_test_event(event)
4545
make_basic_app do |config|
4646
config.enable_logs = true
4747
config.structured_logging.logger_class = Sentry::DebugStructuredLogger
48+
# Disable default structured logging subscribers to avoid interference
49+
config.rails.structured_logging.enabled = false
4850
end
4951
end
5052

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

125127
expect(duration).to be_a(Float)
126-
expect(duration).to be >= 40.0
127-
expect(duration).to be < 100.0
128+
expect(duration).to be >= 10.0 # At least 10ms (reduced from 40ms)
129+
expect(duration).to be < 200.0 # Less than 200ms (increased from 100ms)
128130
expect(duration.round(2)).to eq(duration)
129131
end
130132
end

sentry-rails/spec/sentry/rails/structured_logging_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@
4040
expect(sentry_logs).to be_empty
4141
end
4242
end
43+
44+
context "when enable_logs is true and structured logging is auto-enabled" do
45+
before do
46+
make_basic_app do |config|
47+
config.enable_logs = true
48+
# Don't explicitly set rails.structured_logging.enabled - should auto-enable
49+
end
50+
end
51+
52+
it "captures structured logs automatically" do
53+
get "/posts"
54+
55+
Post.first
56+
57+
Sentry.get_current_client.flush
58+
59+
expect(sentry_logs).not_to be_empty
60+
61+
# Should have logs from both ActionController and ActiveRecord subscribers
62+
controller_logs = sentry_logs.select { |log| log.dig(:attributes, "sentry.origin", :value) == "auto.logger.rails.log_subscriber" }
63+
expect(controller_logs).not_to be_empty
64+
end
65+
end
4366
end

0 commit comments

Comments
 (0)