Skip to content

Commit 1934b7d

Browse files
committed
Make Sentry logs opt-in and update deployment docs
1 parent be9efdb commit 1934b7d

7 files changed

Lines changed: 60 additions & 9 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ This trial run is intentionally minimal. Use Docker Compose for Browserless, aut
4848
## Deploy (Docker Compose)
4949

5050
1. Generate a key: `openssl rand -hex 32`.
51-
2. Export `HTML2RSS_SECRET_KEY`, `HEALTH_CHECK_TOKEN`, `BROWSERLESS_IO_API_TOKEN`, `BUILD_TAG`, and `GIT_SHA` in your shell or `.env`.
52-
3. Optionally export `SENTRY_DSN` to send application errors and structured logs to Sentry.
53-
4. Start: `docker-compose up`.
51+
2. Export `HTML2RSS_SECRET_KEY`, `HEALTH_CHECK_TOKEN`, and `BROWSERLESS_IO_API_TOKEN` in your shell or `.env`.
52+
3. Optionally export `SENTRY_DSN` to send application errors to Sentry.
53+
4. Optionally export `SENTRY_ENABLE_LOGS=true` to forward structured application logs to Sentry.
54+
5. Start: `docker-compose up`.
5455

5556
UI + API run on `http://localhost:4000`. The app exits if the secret key is missing.
5657

app/web/boot/sentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def initialize_sentry!
3535
def apply_settings(config)
3636
config.dsn = RuntimeEnv.sentry_dsn
3737
config.environment = RuntimeEnv.rack_env
38-
config.enable_logs = true
38+
config.enable_logs = RuntimeEnv.sentry_logs_enabled?
3939
config.send_default_pii = false
4040
config.release = release_name
4141
end

app/web/config/runtime_env.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Web
77
# from the process environment after validation.
88
module RuntimeEnv
99
SENSITIVE_KEYS = %w[HTML2RSS_SECRET_KEY HEALTH_CHECK_TOKEN SENTRY_DSN].freeze
10-
BOOT_METADATA_KEYS = %w[BUILD_TAG GIT_SHA RACK_ENV].freeze
10+
BOOT_METADATA_KEYS = %w[BUILD_TAG GIT_SHA RACK_ENV SENTRY_ENABLE_LOGS].freeze
1111

1212
class << self
1313
# @return [void]
@@ -42,6 +42,11 @@ def sentry_enabled?
4242
!sentry_dsn.to_s.strip.empty?
4343
end
4444

45+
# @return [Boolean]
46+
def sentry_logs_enabled?
47+
parse_boolean(fetch('SENTRY_ENABLE_LOGS', 'false'), default: false)
48+
end
49+
4550
# @return [String]
4651
def build_tag
4752
fetch('BUILD_TAG', 'unknown')
@@ -86,6 +91,18 @@ def scrub_sensitive_env!
8691
SENSITIVE_KEYS.each { |key| ENV.delete(key) }
8792
nil
8893
end
94+
95+
# @param value [Object]
96+
# @param default [Boolean]
97+
# @return [Boolean]
98+
def parse_boolean(value, default:)
99+
normalized = value.to_s.strip.downcase
100+
return true if normalized == 'true'
101+
return false if normalized == 'false'
102+
return default if normalized.empty?
103+
104+
raise ArgumentError, "Malformed env 'SENTRY_ENABLE_LOGS': expected true/false, got '#{value}'"
105+
end
89106
end
90107
end
91108
end

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
HTML2RSS_SECRET_KEY: ${HTML2RSS_SECRET_KEY:?set HTML2RSS_SECRET_KEY}
1919
HEALTH_CHECK_TOKEN: ${HEALTH_CHECK_TOKEN:?set HEALTH_CHECK_TOKEN}
2020
SENTRY_DSN: ${SENTRY_DSN:-}
21+
SENTRY_ENABLE_LOGS: ${SENTRY_ENABLE_LOGS:-false}
2122
BROWSERLESS_IO_WEBSOCKET_URL: ws://browserless:4002
2223
BROWSERLESS_IO_API_TOKEN: ${BROWSERLESS_IO_API_TOKEN:?set BROWSERLESS_IO_API_TOKEN}
2324
# Trial runs use the image's bundled config/feeds.yml.

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ Managed flags and environment keys:
171171
| `build_tag` | `BUILD_TAG` | string | `unknown` outside production |
172172
| `git_sha` | `GIT_SHA` | string | `unknown` outside production |
173173
| `sentry_dsn` | `SENTRY_DSN` | string | `nil` |
174+
| `sentry_enable_logs` | `SENTRY_ENABLE_LOGS` | boolean | `false` |
174175

175176
Rules:
176177

spec/html2rss/web/boot/setup_spec.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
'RACK_ENV' => 'development',
1414
'HTML2RSS_SECRET_KEY' => boot_secret_key,
1515
'BUILD_TAG' => '2026-03-27',
16-
'GIT_SHA' => 'abc1234'
16+
'GIT_SHA' => 'abc1234',
17+
'SENTRY_ENABLE_LOGS' => nil
1718
}
1819
end
1920

@@ -56,7 +57,7 @@
5657
end
5758
end
5859

59-
it 'configures Sentry for errors and logs when a DSN is present', :aggregate_failures do
60+
it 'configures Sentry for error reporting when a DSN is present', :aggregate_failures do
6061
stub_environment_validation
6162
allow(Html2rss::Web::Boot::Sentry).to receive(:configure!).and_call_original
6263
allow(Html2rss::Web::Boot::Sentry).to receive(:require).with('sentry-ruby').and_return(true)
@@ -71,6 +72,36 @@
7172
expect_sentry_to_be_configured
7273
end
7374

75+
it 'enables Sentry logs when SENTRY_ENABLE_LOGS is true', :aggregate_failures do
76+
stub_environment_validation
77+
allow(Html2rss::Web::Boot::Sentry).to receive(:configure!).and_call_original
78+
allow(Html2rss::Web::Boot::Sentry).to receive(:require).with('sentry-ruby').and_return(true)
79+
allow(Bundler).to receive(:require)
80+
fake_sentry = build_fake_sentry
81+
stub_const('Sentry', fake_sentry)
82+
83+
ClimateControl.modify(boot_env.merge('SENTRY_DSN' => sentry_dsn, 'SENTRY_ENABLE_LOGS' => 'true')) do
84+
described_class.call!
85+
end
86+
87+
expect_sentry_config(:enable_logs, true)
88+
end
89+
90+
it 'fails fast when SENTRY_ENABLE_LOGS is malformed' do
91+
stub_environment_validation
92+
allow(Html2rss::Web::Boot::Sentry).to receive(:configure!).and_call_original
93+
allow(Html2rss::Web::Boot::Sentry).to receive(:require).with('sentry-ruby').and_return(true)
94+
allow(Bundler).to receive(:require)
95+
fake_sentry = build_fake_sentry
96+
stub_const('Sentry', fake_sentry)
97+
98+
expect do
99+
ClimateControl.modify(boot_env.merge('SENTRY_DSN' => sentry_dsn, 'SENTRY_ENABLE_LOGS' => '1')) do
100+
described_class.call!
101+
end
102+
end.to raise_error(ArgumentError, /SENTRY_ENABLE_LOGS/)
103+
end
104+
74105
it 'logs build metadata on startup' do
75106
stub_environment_validation
76107
logger = instance_double(Logger, info: nil)
@@ -113,7 +144,7 @@ def expect_sensitive_env_to_be_scrubbed
113144
def expect_sentry_to_be_configured
114145
expect(Bundler).to have_received(:require).with(:sentry)
115146
expect_sentry_config(:dsn, sentry_dsn)
116-
expect_sentry_config(:enable_logs, true)
147+
expect_sentry_config(:enable_logs, false)
117148
expect_sentry_config(:release, '2026-03-27+abc1234')
118149
end
119150

spec/support/runtime_env_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def capture_scrubbed_runtime_env(env_overrides)
1414
private
1515

1616
def preserved_runtime_env
17-
%w[HTML2RSS_SECRET_KEY HEALTH_CHECK_TOKEN SENTRY_DSN].each_with_object({}) do |key, env|
17+
%w[HTML2RSS_SECRET_KEY HEALTH_CHECK_TOKEN SENTRY_DSN SENTRY_ENABLE_LOGS].each_with_object({}) do |key, env|
1818
value = ENV.fetch(key, nil)
1919
env[key] = value unless value.nil?
2020
end

0 commit comments

Comments
 (0)