upstate-rails 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9daa36dc978c28d7dc1bba397cc07566184c20dc682ee8c00b28e8a3836858d3
4
- data.tar.gz: 047d1db6c3e0b8b586abed9c69fda37a137e7babcca644df64e8eac42d20c273
3
+ metadata.gz: 6fb7f4706bd20b617139ac0e7990e8bab0634a7d0a7eaf3d86f747fc5400e8b8
4
+ data.tar.gz: 797ee9dfdb3df2b67591c6ff397f7b6fae50f27c452fb8dd634ca12470eed52b
5
5
  SHA512:
6
- metadata.gz: 6b863838c88ded9db1364173f7d778de273c7dc56fe8d536f14ff5de33da35a1788bf61d600cd9c26d9ede9864a920db3ee0d2610cabf1ecc0a09e0c134987cf
7
- data.tar.gz: f475fd351d17f8eaba474f84bc59fc9c2aab10a54b07493bee2011815cba12dc0266ac7b606bdf86ace9fde16787beebd2f0fdc6ae6332bbbcd1d92d8a631b92
6
+ metadata.gz: c083103ef1dce6647c1ccb5f5096a203b8468688fcff8ba1dabd512a47ea2919ca09ed13e609a620bc381f7c295b58900bd175dccc7930aed261af5df0371e00
7
+ data.tar.gz: a88b24544f7d934ff1299b2f875e21f7c8409a25b16e3200ff0cdfff6a4c4c4a7d902ae771a8f1dddf7fa2a79f49c88dbd226791b97d3552b74a2ae0674cef22
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ - Add `rake upstate:verify`, a read-only check that sends nothing to Upstate and writes nothing (it reads the app's database, as the endpoint does, and does not touch the cache, job systems, recurring-job tables or action rollups): disabled or configured, token presence (never the value), reporter and runtime versions, and endpoint authorization and response. Nothing runs on install, and it sends nothing to Upstate.
6
+
3
7
  ## 0.3.0
4
8
 
5
9
  - Solid Queue: report the app's recurring jobs in a new `recurring` section so Upstate can
data/README.md CHANGED
@@ -63,6 +63,16 @@ curl -i -H "Authorization: Bearer $UPSTATE_HEALTH_TOKEN" https://your-app.exampl
63
63
 
64
64
  You should get `200` and JSON starting with `"schema": 1`. A `503` means the database is not answering (the JSON says so). Anything with a slow, missing or erroring section reports that section as `null` and still returns the rest.
65
65
 
66
+ ### Local check
67
+
68
+ ```sh
69
+ bin/rails upstate:verify
70
+ ```
71
+
72
+ Read-only. It sends nothing to Upstate and writes nothing (no cache probe, no job-system access, no action-rollup flush, no credential change), though it reads your database to build the response, as the endpoint does. It does not touch the cache, Sidekiq or other job systems, recurring-job tables or action rollups. It shows whether the reporter is on or disabled, that a token is present (its value is never printed), the reporter, Rails and Ruby versions, and that the endpoint rejects a wrong token, accepts the configured one and answers with schema 1 (the version it shows is your `APP_VERSION` or `KAMAL_VERSION`, not the gem's). It exits non-zero when something needs fixing and says what to do. It never runs on install, only when you invoke it.
73
+
74
+ A passing local check does not prove Upstate is receiving data. After a deploy, run `upstate doctor` to see Configured, Connected and Fresh evidence from Upstate itself.
75
+
66
76
  ## Good to know
67
77
 
68
78
  - The route is added ahead of your routes, so a catch-all route cannot hide it.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :upstate do
4
+ desc "Check the Upstate app-health reporter locally (read-only; prints no token)"
5
+ task verify: :environment do
6
+ report = Upstate::Rails::Verify.call
7
+ puts Upstate::Rails::Verify.render(report)
8
+ exit(1) unless report[:ok]
9
+ end
10
+ end
@@ -6,12 +6,18 @@ module Upstate
6
6
  class Endpoint
7
7
  MAX_TOKEN_BYTES = 256
8
8
 
9
+ # `health` is any callable returning [status, body]; `rake upstate:verify` passes a
10
+ # read-only one.
11
+ def initialize(health: Health.method(:call))
12
+ @health = health
13
+ end
14
+
9
15
  def call(env)
10
16
  expected = ENV["UPSTATE_HEALTH_TOKEN"].to_s
11
17
  return respond(404, "") if expected.empty?
12
18
  return respond(401, "", "www-authenticate" => "Bearer") unless authorized?(env["HTTP_AUTHORIZATION"], expected)
13
19
 
14
- status, body = Health.call
20
+ status, body = @health.call
15
21
  respond(status, JSON.generate(body), "content-type" => "application/json")
16
22
  end
17
23
 
@@ -6,18 +6,22 @@ module Upstate
6
6
  class Health
7
7
  SCHEMA = 1
8
8
 
9
- def self.call(now: Time.now)
10
- new(now).call
9
+ # `read_only` builds the response without any write or side effect: the cache probe,
10
+ # the job-system read (Sidekiq's process set prunes dead entries when loaded) and
11
+ # recurring-job tables (Solid Queue) and action rollups are skipped. Used by the local verify.
12
+ def self.call(now: Time.now, read_only: false)
13
+ new(now, read_only: read_only).call
11
14
  end
12
15
 
13
- def initialize(now)
16
+ def initialize(now, read_only: false)
14
17
  @now = now
18
+ @read_only = read_only
15
19
  end
16
20
 
17
21
  # => [http_status, body_hash]
18
22
  def call
19
23
  database = section { Sections.database }
20
- jobs, solid_queue = section { Jobs.call(now: @now) } || [nil, nil]
24
+ jobs, solid_queue = (@read_only ? nil : section { Jobs.call(now: @now) }) || [nil, nil]
21
25
 
22
26
  body = {
23
27
  schema: SCHEMA,
@@ -29,11 +33,11 @@ module Upstate
29
33
  active_record_pool: section { Sections.active_record_pool },
30
34
  puma: section { Sections.puma },
31
35
  jobs: jobs,
32
- cache: section { Sections.cache },
33
- actions: section { actions }
36
+ cache: @read_only ? nil : section { Sections.cache },
37
+ actions: @read_only ? nil : section { actions }
34
38
  }
35
39
  body[:solid_queue] = solid_queue if solid_queue
36
- recurring = section { Recurring.call(now: @now) }
40
+ recurring = @read_only ? nil : section { Recurring.call(now: @now) }
37
41
  body[:recurring] = recurring if recurring
38
42
 
39
43
  status = database && database[:ok] == false ? 503 : 200
@@ -34,7 +34,12 @@ module Upstate
34
34
  next if ENV["UPSTATE_HEALTH_TOKEN"].to_s.empty?
35
35
 
36
36
  Upstate::Rails.subscribe!
37
- at_exit { Upstate::Rails.rollup.flush! }
37
+ at_exit { Upstate::Rails.rollup.flush! unless Upstate::Rails.flush_disabled? }
38
+ end
39
+
40
+ # Defined only; nothing runs until someone invokes the task.
41
+ rake_tasks do
42
+ load File.expand_path("../../tasks/upstate.rake", __dir__)
38
43
  end
39
44
 
40
45
  generators do
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Upstate
4
+ module Rails
5
+ # Read-only local check for `rake upstate:verify`. It answers the questions that can
6
+ # be answered inside this process: is the reporter turned on, is a token present (never
7
+ # printed), and does the endpoint authorize and answer. It sends nothing to Upstate,
8
+ # writes nothing (no cache probe, no rollup flush) and never changes credentials. It
9
+ # does read from the app's own database to build the response. Whether Upstate has received a report
10
+ # is a separate fact that only Upstate can confirm (`upstate doctor`).
11
+ class Verify
12
+ Check = Struct.new(:name, :status, :detail, :action, keyword_init: true) do
13
+ def ok? = status == "ok"
14
+ end
15
+
16
+ def self.call(endpoint: nil)
17
+ new(endpoint).call
18
+ end
19
+
20
+ def initialize(endpoint)
21
+ @endpoint = endpoint || Endpoint.new(health: -> { Health.call(read_only: true) })
22
+ end
23
+
24
+ # => {reporter:, runtime:, environment:, token:, checks:, ok:}
25
+ def call
26
+ # The railtie registers an exit-time rollup flush when a token is set; a check must
27
+ # write nothing, so switch it off for this process.
28
+ Upstate::Rails.disable_flush!
29
+ token = ENV["UPSTATE_HEALTH_TOKEN"].to_s
30
+ checks = [configured_check(token)]
31
+ checks.concat(endpoint_checks(token)) unless token.empty?
32
+ {
33
+ reporter: {name: "upstate-rails", version: Upstate::Rails::VERSION},
34
+ runtime: {rails: ::Rails.version, ruby: RUBY_VERSION},
35
+ environment: ::Rails.env.to_s,
36
+ token: token_summary(token),
37
+ checks: checks.map(&:to_h),
38
+ ok: checks.all?(&:ok?),
39
+ next_step: "A passing local check is not proof that Upstate receives data. After a deploy, run `upstate doctor` and wait for fresh evidence."
40
+ }
41
+ end
42
+
43
+ # Human-readable lines. Never includes the token or any part of it.
44
+ def self.render(report)
45
+ lines = ["Upstate Rails reporter #{report[:reporter][:version]} (Rails #{report[:runtime][:rails]}, Ruby #{report[:runtime][:ruby]}, #{report[:environment]})"]
46
+ lines << "Token: #{report[:token][:present] ? "present (#{report[:token][:length]} characters, value not shown)" : "not set"}"
47
+ report[:checks].each do |check|
48
+ lines << "#{(check[:status] == "ok") ? "ok " : "FAIL"} #{check[:name]}: #{check[:detail]}"
49
+ lines << " next: #{check[:action]}" if check[:action]
50
+ end
51
+ lines << report[:next_step]
52
+ lines.join("\n")
53
+ end
54
+
55
+ private
56
+
57
+ def token_summary(token)
58
+ {present: !token.empty?, length: token.length}
59
+ end
60
+
61
+ def configured_check(token)
62
+ if token.empty?
63
+ Check.new(name: "configured", status: "disabled",
64
+ detail: "UPSTATE_HEALTH_TOKEN is not set in this environment, so /upstate/health answers 404 and nothing is reported.",
65
+ action: "Generate a health token in Upstate, set UPSTATE_HEALTH_TOKEN for this environment (for example under env.secret in config/deploy.yml), then deploy.")
66
+ else
67
+ Check.new(name: "configured", status: "ok", detail: "UPSTATE_HEALTH_TOKEN is set; the endpoint is on.")
68
+ end
69
+ end
70
+
71
+ def endpoint_checks(token)
72
+ if token.bytesize > Endpoint::MAX_TOKEN_BYTES
73
+ return [Check.new(name: "authorization", status: "failed",
74
+ detail: "UPSTATE_HEALTH_TOKEN is #{token.bytesize} bytes; the endpoint rejects any token over #{Endpoint::MAX_TOKEN_BYTES} bytes.",
75
+ action: "Generate a token in Upstate (they are well under the limit) and set it as UPSTATE_HEALTH_TOKEN, without extra characters.")]
76
+ end
77
+
78
+ # One request with the configured token serves both the authorization and the
79
+ # response check, so the health build (database, jobs, schedules) runs once.
80
+ status, _headers, body = call_endpoint(token)
81
+ [auth_check(token, status), health_check(status, body)]
82
+ end
83
+
84
+ # A wrong token that can never equal the configured one.
85
+ def probe_token(token)
86
+ loop do
87
+ candidate = "probe-#{SecureRandom.hex(16)}"
88
+ return candidate unless candidate == token
89
+ end
90
+ end
91
+
92
+ def call_endpoint(bearer)
93
+ @endpoint.call({"HTTP_AUTHORIZATION" => "Bearer #{bearer}"})
94
+ end
95
+
96
+ def auth_check(token, accepted)
97
+ rejected = call_endpoint(probe_token(token)).first
98
+ if rejected == 401 && accepted != 401
99
+ Check.new(name: "authorization", status: "ok", detail: "The endpoint rejects a wrong token and accepts the configured one.")
100
+ else
101
+ Check.new(name: "authorization", status: "failed",
102
+ detail: "The endpoint did not behave as expected (wrong token: #{rejected}, configured token: #{accepted}).",
103
+ action: "Check that UPSTATE_HEALTH_TOKEN has no stray characters or whitespace, and that this endpoint is the Upstate one, then redeploy.")
104
+ end
105
+ end
106
+
107
+ def health_check(status, body)
108
+ parsed = JSON.parse(body.join)
109
+ if status == 200 && parsed["schema"] == Health::SCHEMA
110
+ Check.new(name: "health response", status: "ok", detail: "Schema #{Health::SCHEMA}, application version #{parsed["version"].inspect} (APP_VERSION or KAMAL_VERSION).")
111
+ elsif status == 503
112
+ Check.new(name: "health response", status: "failed", detail: "The database is not answering (503).",
113
+ action: "Fix database connectivity for this environment; the reporter will then answer 200.")
114
+ else
115
+ Check.new(name: "health response", status: "failed", detail: "Unexpected response #{status}.",
116
+ action: "Run the app and request /upstate/health with the token to see the response.")
117
+ end
118
+ rescue StandardError => e
119
+ Check.new(name: "health response", status: "failed", detail: "Could not read the health response (#{e.class}).",
120
+ action: "Run the app in this environment and check its logs for the error.")
121
+ end
122
+ end
123
+ end
124
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Upstate
4
4
  module Rails
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
data/lib/upstate/rails.rb CHANGED
@@ -26,6 +26,15 @@ module Upstate
26
26
  @rollup ||= ActionRollup.new
27
27
  end
28
28
 
29
+ # `rake upstate:verify` turns the exit-time flush off so the check writes nothing.
30
+ def disable_flush!
31
+ @flush_disabled = true
32
+ end
33
+
34
+ def flush_disabled?
35
+ !!@flush_disabled
36
+ end
37
+
29
38
  # Subscribes once to process_action.action_controller.
30
39
  def subscribe!
31
40
  return if @subscriber
@@ -67,4 +76,5 @@ require_relative "rails/recurring"
67
76
  require_relative "rails/sections"
68
77
  require_relative "rails/health"
69
78
  require_relative "rails/endpoint"
79
+ require_relative "rails/verify"
70
80
  require_relative "rails/railtie"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upstate-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Upstate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-13 00:00:00.000000000 Z
11
+ date: 2026-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -64,6 +64,7 @@ files:
64
64
  - LICENSE.txt
65
65
  - README.md
66
66
  - lib/generators/upstate/install/install_generator.rb
67
+ - lib/tasks/upstate.rake
67
68
  - lib/upstate/rails.rb
68
69
  - lib/upstate/rails/action_rollup.rb
69
70
  - lib/upstate/rails/action_store.rb
@@ -77,6 +78,7 @@ files:
77
78
  - lib/upstate/rails/recurring.rb
78
79
  - lib/upstate/rails/recurring/solid_queue_source.rb
79
80
  - lib/upstate/rails/sections.rb
81
+ - lib/upstate/rails/verify.rb
80
82
  - lib/upstate/rails/version.rb
81
83
  homepage: https://github.com/Upstate-run/upstate-rails
82
84
  licenses: