upstate-rails 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +3 -2
- data/lib/upstate/rails/sections.rb +118 -1
- data/lib/upstate/rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cb6487ba0801250441daade159c3a5c34c97c51816ec224b8db412602a42852e
|
|
4
|
+
data.tar.gz: c6fe4a6ae7856ba764b77c63a2c14172c52f82af5dfc040f9924cc9a51468f39
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5a6c2f42b4e28bc76c82d08e1fac8e88ecf49f6b18bfe41bfcb6d41acf712663ab125b5266d25bf76c6399a8b3f0759f0dde5d0a58064149b90af69fc89aca19
|
|
7
|
+
data.tar.gz: ba06b6aa72b1658fe913a8847a15cc88a3a20be4053c4cbb4d35771564ad86204377932daf9cefdcbf7a9809e8947d36b7901359444a82405edd21e0bde38231
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
- Report database health in the `database` section, for the primary connection on
|
|
6
|
+
PostgreSQL:
|
|
7
|
+
- `size_bytes`: `pg_database_size(current_database())` (this database only, not WAL or
|
|
8
|
+
other databases on the server);
|
|
9
|
+
- `connections`: `{in_use, max}` (client connections and `max_connections`);
|
|
10
|
+
- `oldest_transaction_age_seconds` (0 when none is open) and `idle_in_transaction`, for
|
|
11
|
+
sessions the app's role can see;
|
|
12
|
+
- `xid_age`: `age(datfrozenxid)` (transaction ID wraparound headroom used);
|
|
13
|
+
- `replication`: replica replay lag, or the replica count and largest `replay_lag` on a
|
|
14
|
+
primary where visible; left out otherwise;
|
|
15
|
+
- `largest_tables`: the 5 largest tables with total size and estimated live/dead rows.
|
|
16
|
+
Table names and numbers only, never row data.
|
|
17
|
+
- One transaction with a 250ms `SET LOCAL statement_timeout` and a savepoint per query.
|
|
18
|
+
Anything that fails, or another adapter, is left out; it never changes `database.ok` or
|
|
19
|
+
the status. The schema stays 1 (all fields are additive); Upstate servers that predate
|
|
20
|
+
them ignore them.
|
|
21
|
+
|
|
3
22
|
## 0.1.0
|
|
4
23
|
|
|
5
24
|
- First release: `GET /upstate/health` (schema 1) with boot facts, migrations, DB pool, Puma,
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# upstate-rails
|
|
2
2
|
|
|
3
|
-
Rails app health for [Upstate](https://upstate.cloud): boot facts, migrations, Puma, jobs, and the slowest actions. Counts only.
|
|
3
|
+
Rails app health for [Upstate](https://upstate.cloud): boot facts, migrations, database health, Puma, jobs, and the slowest actions. Counts only.
|
|
4
4
|
|
|
5
5
|
The gem adds one endpoint, `GET /upstate/health`. Upstate's host observer reads it from inside your server, over your private Docker network, every minute. Nothing else is installed, and nothing is sent anywhere by the gem itself.
|
|
6
6
|
|
|
@@ -8,6 +8,7 @@ The gem adds one endpoint, `GET /upstate/health`. Upstate's host observer reads
|
|
|
8
8
|
|
|
9
9
|
- App version (`APP_VERSION`, else `KAMAL_VERSION`, else `unknown`), Rails and Ruby versions, whether YJIT is on, when the process booted.
|
|
10
10
|
- Whether the database answers, and whether migrations are pending.
|
|
11
|
+
- Database health for the primary connection (PostgreSQL only): its size in bytes (`pg_database_size`, this database only, not WAL or other databases), connections in use and `max_connections`, the oldest open transaction's age and the number of sessions idle in transaction (as visible to the app's role), `age(datfrozenxid)` (wraparound headroom), replication lag where this connection can see it, and the 5 largest tables with their total size and estimated live and dead rows. Table names are sent; row data never is. One transaction with a 250ms statement timeout and a savepoint per query; anything that fails, or MySQL/SQLite, is left out.
|
|
11
12
|
- Active Record pool size, busy and waiting connections.
|
|
12
13
|
- Puma workers, threads, backlog and spare capacity.
|
|
13
14
|
- Job numbers for Solid Queue, Sidekiq or GoodJob: queue depth, oldest queued age, worker count, jobs processed and failed in the last 5 minutes, and when something last ran.
|
|
@@ -16,7 +17,7 @@ The gem adds one endpoint, `GET /upstate/health`. Upstate's host observer reads
|
|
|
16
17
|
|
|
17
18
|
## What is never sent
|
|
18
19
|
|
|
19
|
-
No params, URLs or paths, SQL, IP addresses, user agents, headers, cookies, job arguments, exception classes or messages, logs, or traces. An error is only a count (a 5xx response or an unhandled exception). Job queue names and job class names are not read either.
|
|
20
|
+
No params, URLs or paths, SQL, row data, IP addresses, user agents, headers, cookies, job arguments, exception classes or messages, logs, or traces. An error is only a count (a 5xx response or an unhandled exception). Job queue names and job class names are not read either.
|
|
20
21
|
|
|
21
22
|
## Install
|
|
22
23
|
|
|
@@ -16,7 +16,124 @@ module Upstate
|
|
|
16
16
|
end
|
|
17
17
|
return {ok: false, pending_migrations: nil} unless ok
|
|
18
18
|
|
|
19
|
-
{ok: true, pending_migrations: pending_migrations}
|
|
19
|
+
{ok: true, pending_migrations: pending_migrations}.merge(database_stats)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
STATEMENT_TIMEOUT_MS = 250
|
|
23
|
+
|
|
24
|
+
SUMMARY_SQL = <<~SQL
|
|
25
|
+
SELECT
|
|
26
|
+
pg_database_size(current_database()),
|
|
27
|
+
(SELECT count(*) FROM pg_stat_activity WHERE backend_type = 'client backend'),
|
|
28
|
+
current_setting('max_connections')::int,
|
|
29
|
+
COALESCE((SELECT floor(extract(epoch FROM now() - min(xact_start)))::bigint
|
|
30
|
+
FROM pg_stat_activity
|
|
31
|
+
WHERE datname = current_database() AND xact_start IS NOT NULL
|
|
32
|
+
AND pid <> pg_backend_pid() AND backend_type = 'client backend'), 0),
|
|
33
|
+
(SELECT count(*) FROM pg_stat_activity
|
|
34
|
+
WHERE datname = current_database()
|
|
35
|
+
AND state IN ('idle in transaction', 'idle in transaction (aborted)')),
|
|
36
|
+
(SELECT age(datfrozenxid) FROM pg_database WHERE datname = current_database()),
|
|
37
|
+
pg_is_in_recovery(),
|
|
38
|
+
CASE WHEN pg_is_in_recovery()
|
|
39
|
+
THEN floor(extract(epoch FROM now() - pg_last_xact_replay_timestamp()))::bigint END
|
|
40
|
+
SQL
|
|
41
|
+
|
|
42
|
+
TABLES_SQL = <<~SQL
|
|
43
|
+
SELECT quote_ident(schemaname) || '.' || quote_ident(relname), pg_total_relation_size(relid), n_live_tup, n_dead_tup
|
|
44
|
+
FROM pg_stat_user_tables
|
|
45
|
+
ORDER BY pg_total_relation_size(relid) DESC
|
|
46
|
+
LIMIT 5
|
|
47
|
+
SQL
|
|
48
|
+
|
|
49
|
+
REPLICAS_SQL = <<~SQL
|
|
50
|
+
SELECT count(*), floor(extract(epoch FROM max(replay_lag)))::bigint FROM pg_stat_replication
|
|
51
|
+
SQL
|
|
52
|
+
|
|
53
|
+
# Cheap, aggregate, read-only PostgreSQL health numbers for the primary connection:
|
|
54
|
+
# size (this database only, not WAL or other databases), connections vs
|
|
55
|
+
# max_connections, the oldest open transaction and sessions idle in transaction (as
|
|
56
|
+
# visible to the app's role), age(datfrozenxid), replication lag where visible, and
|
|
57
|
+
# the 5 largest tables with estimated live/dead rows (names and numbers, no row data;
|
|
58
|
+
# each name part is quote_ident-ed so distinct tables never share a schema.table name).
|
|
59
|
+
# One transaction with a transaction-local statement timeout and a savepoint per query,
|
|
60
|
+
# so a failing query drops only its fields. Other adapters or any error return {}.
|
|
61
|
+
def database_stats
|
|
62
|
+
::ActiveRecord::Base.connection_pool.with_connection do |connection|
|
|
63
|
+
next {} unless connection.adapter_name.to_s.match?(/postg/i)
|
|
64
|
+
|
|
65
|
+
connection.transaction(requires_new: true) do
|
|
66
|
+
connection.execute("SET LOCAL statement_timeout = #{STATEMENT_TIMEOUT_MS}")
|
|
67
|
+
summary = guarded_rows(connection, SUMMARY_SQL)&.first
|
|
68
|
+
tables = guarded_rows(connection, TABLES_SQL)
|
|
69
|
+
replicas = guarded_rows(connection, REPLICAS_SQL)&.first if primary?(summary)
|
|
70
|
+
|
|
71
|
+
stats = summary_stats(summary)
|
|
72
|
+
table_list = table_stats(tables)
|
|
73
|
+
stats[:largest_tables] = table_list if table_list
|
|
74
|
+
replica_stats = replicas_stats(replicas)
|
|
75
|
+
stats[:replication] = replica_stats if replica_stats
|
|
76
|
+
stats
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
rescue StandardError
|
|
80
|
+
{}
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Runs one query behind a savepoint; a failure rolls back to it and returns nil.
|
|
84
|
+
def guarded_rows(connection, sql)
|
|
85
|
+
connection.transaction(requires_new: true) { connection.select_rows(sql) }
|
|
86
|
+
rescue StandardError
|
|
87
|
+
nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def primary?(summary)
|
|
91
|
+
summary.is_a?(Array) && [false, "f"].include?(summary[6])
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def summary_stats(row)
|
|
95
|
+
return {} unless row.is_a?(Array) && row.size == 8
|
|
96
|
+
|
|
97
|
+
size, in_use, max, oldest, idle, xid_age, recovery, lag = row
|
|
98
|
+
stats = {}
|
|
99
|
+
put(stats, :size_bytes, count(size))
|
|
100
|
+
in_use = count(in_use)
|
|
101
|
+
max = count(max)
|
|
102
|
+
stats[:connections] = {in_use: in_use, max: max} if in_use && max&.positive?
|
|
103
|
+
put(stats, :oldest_transaction_age_seconds, count(oldest))
|
|
104
|
+
put(stats, :idle_in_transaction, count(idle))
|
|
105
|
+
put(stats, :xid_age, count(xid_age))
|
|
106
|
+
stats[:replication] = {role: "replica", lag_seconds: count(lag)} if [true, "t"].include?(recovery)
|
|
107
|
+
stats
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def table_stats(rows)
|
|
111
|
+
return nil unless rows.is_a?(Array) && !rows.empty?
|
|
112
|
+
|
|
113
|
+
tables = rows.first(5).filter_map do |name, total, live, dead|
|
|
114
|
+
next unless name.is_a?(String)
|
|
115
|
+
|
|
116
|
+
{name: name, total_bytes: count(total), live_rows: count(live), dead_rows: count(dead)}
|
|
117
|
+
end
|
|
118
|
+
tables.empty? ? nil : tables
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def replicas_stats(row)
|
|
122
|
+
return nil unless row.is_a?(Array)
|
|
123
|
+
|
|
124
|
+
replicas = count(row[0])
|
|
125
|
+
return nil unless replicas&.positive?
|
|
126
|
+
|
|
127
|
+
{role: "primary", replicas: replicas, lag_seconds: count(row[1])}
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def count(value)
|
|
131
|
+
number = Integer(value, exception: false) if value.is_a?(Integer) || value.is_a?(String)
|
|
132
|
+
number if number && number >= 0
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def put(hash, key, value)
|
|
136
|
+
hash[key] = value unless value.nil?
|
|
20
137
|
end
|
|
21
138
|
|
|
22
139
|
# ConnectionPool#migration_context exists on 7.1+; older shapes fall back to the connection.
|
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.
|
|
4
|
+
version: 0.2.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
|
+
date: 2026-11-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: railties
|