Skip to content

Commit 2c8c2c1

Browse files
authored
feat(logging): auto-enable structured_logging in Rails (#2721)
* feat(logging): auto-enable structured_logging When config.enable_logs is true, enable structured logging in Rails automatically too. Closes #2715 * Update CHANGELOG * Rework to use a callback * Use new syntax for config callbacks * No need for parent_config after all
1 parent 43e5788 commit 2c8c2c1

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
### Features
4+
5+
- Auto-enable Rails structured logging when `enable_logs` is true ([#2721](https://github.com/getsentry/sentry-ruby/pull/2721))
6+
37
### Internal
48

59
- Fix leftover `config.logger` call in `graphql` patch ([#2722](https://github.com/getsentry/sentry-ruby/2722)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Sentry
1313
class Configuration
1414
attr_reader :rails
1515

16-
add_post_initialization_callback do
16+
after(:initialize) do
1717
@rails = Sentry::Rails::Configuration.new
1818
@excluded_exceptions = @excluded_exceptions.concat(Sentry::Rails::IGNORE_DEFAULT)
1919

@@ -33,6 +33,10 @@ class Configuration
3333
end
3434
end
3535
end
36+
37+
after(:configured) do
38+
rails.structured_logging.enabled = enable_logs if rails.structured_logging.enabled.nil?
39+
end
3640
end
3741

3842
module Rails
@@ -202,9 +206,15 @@ class StructuredLoggingConfiguration
202206
}.freeze
203207

204208
def initialize
205-
@enabled = false
209+
@enabled = nil
206210
@subscribers = DEFAULT_SUBSCRIBERS.dup
207211
end
212+
213+
# Returns true if structured logging should be enabled.
214+
# @return [Boolean]
215+
def enabled?
216+
enabled
217+
end
208218
end
209219
end
210220
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
129+
expect(duration).to be < 200.0
128130
expect(duration.round(2)).to eq(duration)
129131
end
130132
end

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,28 @@
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+
end
49+
end
50+
51+
it "captures structured logs automatically" do
52+
get "/posts"
53+
54+
Post.first
55+
56+
Sentry.get_current_client.flush
57+
58+
expect(sentry_logs).not_to be_empty
59+
60+
rails_log_events = sentry_logs.select { |log|
61+
log.dig(:attributes, "sentry.origin", :value) == "auto.logger.rails.log_subscriber"
62+
}
63+
64+
expect(rails_log_events).not_to be_empty
65+
end
66+
end
4367
end

0 commit comments

Comments
 (0)