Skip to content

Commit bcd262b

Browse files
committed
Extract more into engine, and simplify downstream app
1 parent 7937ece commit bcd262b

11 files changed

Lines changed: 123 additions & 40 deletions

File tree

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ The `upright:install` generator creates:
4242
- `config/initializers/0_url_options.rb` - Subdomain routing configuration
4343
- `config/initializers/upright.rb` - Engine configuration
4444
- `config/sites.yml` - Site/location definitions
45-
- `config/probes/http_probes.yml` - HTTP probe definitions
46-
- `config/probes/smtp_probes.yml` - SMTP probe definitions
4745
- `config/prometheus/prometheus.yml` - Prometheus configuration
4846
- `config/alertmanager/alertmanager.yml` - AlertManager configuration
4947
- `config/otel_collector.yml` - OpenTelemetry Collector configuration
48+
- `probes/` - Directory for all probe files
49+
- `probes/authenticators/` - Directory for authenticator classes
50+
- `probes/http_probes.yml` - HTTP probe definitions
51+
- `probes/smtp_probes.yml` - SMTP probe definitions
5052

5153
It also mounts the engine at `/` in your routes.
5254

@@ -181,7 +183,7 @@ end
181183

182184
### HTTP Probes
183185

184-
Add probes to `config/probes/http_probes.yml`:
186+
Add probes to `probes/http_probes.yml`:
185187

186188
```yaml
187189
- name: Main Website
@@ -199,7 +201,7 @@ Add probes to `config/probes/http_probes.yml`:
199201

200202
### SMTP Probes
201203

202-
Add probes to `config/probes/smtp_probes.yml`:
204+
Add probes to `probes/smtp_probes.yml`:
203205

204206
```yaml
205207
- name: Primary Mail Server
@@ -220,7 +222,7 @@ bin/rails generate upright:playwright_probe MyServiceAuth
220222
This creates a probe class:
221223

222224
```ruby
223-
# app/models/probes/playwright/my_service_auth_probe.rb
225+
# probes/my_service_auth_probe.rb
224226
class Probes::Playwright::MyServiceAuthProbe < Upright::Probes::Playwright::Base
225227
# Optionally authenticate before running
226228
# authenticate_with_form :my_service
@@ -239,20 +241,17 @@ end
239241
For probes that require authentication, create an authenticator:
240242

241243
```ruby
242-
# app/models/playwright/authenticator/my_service.rb
244+
# probes/authenticators/my_service.rb
243245
class Playwright::Authenticator::MyService < Upright::Playwright::Authenticator::Base
246+
def signin_redirect_url = "https://app.example.com/dashboard"
247+
def signin_path = "/login"
248+
def service_name = :my_service
249+
244250
def authenticate
245251
page.goto("https://app.example.com/login")
246-
page.fill('[name="email"]', credentials[:email])
247-
page.fill('[name="password"]', credentials[:password])
248-
page.click('button[type="submit"]')
249-
page.wait_for_url("**/dashboard**")
250-
end
251-
252-
private
253-
254-
def credentials
255-
Rails.application.credentials.my_service
252+
page.get_by_label("Email").fill(credentials.my_service.email)
253+
page.get_by_label("Password").fill(credentials.my_service.password)
254+
page.get_by_role("button", name: "Sign in").click
256255
end
257256
end
258257
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Upright::ProbeYamlSource
2+
extend ActiveSupport::Concern
3+
4+
class_methods do
5+
def file_path
6+
filename = name.demodulize.underscore.pluralize
7+
Upright.configuration.probes_path.join("#{filename}.yml").to_s
8+
end
9+
end
10+
end

app/models/upright/probes/http_probe.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Upright::Probes::HTTPProbe < FrozenRecord::Base
22
include Upright::Probeable
3+
include Upright::ProbeYamlSource
34

45
stagger_by_site 3.seconds
56

67
DEFAULT_EXPECTED_STATUS = 200..399
78

8-
self.backend = FrozenRecord::Backends::Yaml
9-
109
attr_accessor :last_response
1110

1211
def check

app/models/upright/probes/smtp_probe.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
class Upright::Probes::SMTPProbe < FrozenRecord::Base
44
include Upright::Probeable
5+
include Upright::ProbeYamlSource
56

67
stagger_by_site 3.seconds
78

8-
self.backend = FrozenRecord::Backends::Yaml
9-
109
attr_accessor :smtp_log
1110

1211
def check

app/models/upright/probes/traceroute_probe.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Upright::Probes::TracerouteProbe < FrozenRecord::Base
22
include Upright::Probeable
3+
include Upright::ProbeYamlSource
34
include Upright::Traceroute::OtelTracing
45

56
stagger_by_site 3.seconds
67

7-
# Probes are loaded from config/upright/probes/traceroute_probes.yml
8-
98
TIMEOUT = 60.seconds
109
MAX_HOPS = Upright::Traceroute::Result::MAX_HOPS
1110
PROBE_COUNT = Upright::Traceroute::Result::PROBE_COUNT

lib/generators/upright/install/install_generator.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ def copy_sites_config
1818
end
1919

2020
def create_probe_directories
21-
empty_directory "config/probes"
22-
template "http_probes.yml", "config/probes/http_probes.yml"
23-
template "smtp_probes.yml", "config/probes/smtp_probes.yml"
21+
empty_directory "probes"
22+
empty_directory "probes/authenticators"
23+
template "http_probes.yml", "probes/http_probes.yml"
24+
template "smtp_probes.yml", "probes/smtp_probes.yml"
2425
end
2526

2627
def copy_observability_configs

lib/generators/upright/install/templates/upright.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
# Site configuration file path
1717
config.sites_config_path = Rails.root.join("config/sites.yml")
1818

19-
# Probe definitions path (for FrozenRecord)
20-
config.frozen_record_path = Rails.root.join("config/probes")
21-
2219
# Playwright browser server URL (production only)
2320
# config.playwright_server_url = ENV["PLAYWRIGHT_SERVER_URL"]
2421

lib/generators/upright/playwright_probe/playwright_probe_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class PlaywrightProbeGenerator < Rails::Generators::NamedBase
77
desc: "Generate an authenticator class for this probe"
88

99
def create_probe_file
10-
template "probe.rb.tt", File.join("app/models/probes/playwright", "#{file_name}_probe.rb")
10+
template "probe.rb.tt", File.join("probes", "#{file_name}_probe.rb")
1111
end
1212

1313
def create_authenticator_file
1414
if options[:with_authenticator]
15-
template "authenticator.rb.tt", File.join("app/models/playwright/authenticator", "#{file_name}.rb")
15+
template "authenticator.rb.tt", File.join("probes/authenticators", "#{file_name}.rb")
1616
end
1717
end
1818

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
class Playwright::Authenticator::<%= class_name %> < Upright::Playwright::Authenticator::Base
2+
def signin_redirect_url = "https://example.com/dashboard"
3+
def signin_path = "/login"
4+
def service_name = :<%= file_name %>
5+
26
def authenticate
37
# Your authentication logic here
48
# page.goto("https://example.com/login")
5-
# page.fill('[name="email"]', credentials[:email])
6-
# page.fill('[name="password"]', credentials[:password])
7-
# page.click('button[type="submit"]')
8-
# page.wait_for_url("**/dashboard**")
9+
# page.get_by_label("Email").fill(credentials.<%= file_name %>.email)
10+
# page.get_by_label("Password").fill(credentials.<%= file_name %>.password)
11+
# page.get_by_role("button", name: "Sign in").click
912
raise NotImplementedError, "Implement the authenticate method for <%= class_name %>"
1013
end
11-
12-
private
13-
14-
def credentials
15-
Rails.application.credentials.<%= file_name %>
16-
end
1714
end

lib/upright/configuration.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Upright::Configuration
1616
attr_accessor :storage_state_dir
1717
attr_accessor :frozen_record_path
1818

19+
# Probe and authenticator paths (for auto-loading app-specific code)
20+
attr_writer :probes_path
21+
attr_writer :authenticators_path
22+
1923
# Playwright
2024
attr_accessor :playwright_server_url
2125

@@ -42,6 +46,8 @@ def initialize
4246
@storage_state_dir = nil
4347
@frozen_record_path = nil
4448
@sites_config_path = nil
49+
@probes_path = nil
50+
@authenticators_path = nil
4551

4652
@playwright_server_url = ENV["PLAYWRIGHT_SERVER_URL"]
4753
@otel_endpoint = ENV["OTEL_EXPORTER_OTLP_ENDPOINT"]
@@ -113,6 +119,14 @@ def sites_config_path
113119
@sites_config_path || Rails.root.join("config/sites.yml")
114120
end
115121

122+
def probes_path
123+
@probes_path || Rails.root.join("probes")
124+
end
125+
126+
def authenticators_path
127+
@authenticators_path || Rails.root.join("probes/authenticators")
128+
end
129+
116130
private
117131
def default_storage_path(subdir)
118132
if defined?(Rails) && Rails.root

0 commit comments

Comments
 (0)