Skip to content

Commit 8710c9b

Browse files
committed
fix: gather connected metrics for all tenants
1 parent ac87c47 commit 8710c9b

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

lib/realtime/metrics_cleaner.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ defmodule Realtime.MetricsCleaner do
3535
Process.send_after(self(), :check, interval)
3636
end
3737

38-
@table_name :"syn_registry_by_name_Elixir.Realtime.Tenants.Connect"
3938
@metrics_table Realtime.PromEx.Metrics
4039
@filter_spec [{{{:_, %{tenant: :"$1"}}, :_}, [], [:"$1"]}]
41-
@tenant_id_spec [{{:"$1", :_, :_, :_, :_, :_}, [], [:"$1"]}]
4240
defp loop_and_cleanup_metrics_table do
43-
tenant_ids = :ets.select(@table_name, @tenant_id_spec)
41+
tenant_ids = Realtime.Tenants.Connect.list_tenants()
4442

4543
:ets.select(@metrics_table, @filter_spec)
4644
|> Enum.uniq()

lib/realtime/monitoring/prom_ex/plugins/tenant.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ defmodule Realtime.PromEx.Plugins.Tenant do
8989
end
9090

9191
def execute_tenant_metrics do
92-
tenants = Tenants.list_connected_tenants(Node.self())
92+
tenants = Tenants.Connect.list_tenants()
9393

9494
for t <- tenants do
9595
count = UsersCounter.tenant_users(Node.self(), t)

lib/realtime/tenants.ex

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ defmodule Realtime.Tenants do
1515
alias Realtime.Tenants.Migrations
1616
alias Realtime.UsersCounter
1717

18-
@doc """
19-
Gets a list of connected tenant `external_id` strings in the cluster or a node.
20-
"""
21-
@spec list_connected_tenants(atom()) :: [String.t()]
22-
def list_connected_tenants(node) do
23-
UsersCounter.scopes()
24-
|> Enum.flat_map(fn scope -> :syn.group_names(scope, node) end)
25-
end
26-
2718
@doc """
2819
Gets the database connection pid managed by the Tenants.Connect process.
2920

lib/realtime/tenants/connect.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ defmodule Realtime.Tenants.Connect do
3838
connected_users_bucket: [1],
3939
check_connect_region_interval: nil
4040

41+
@tenant_id_spec [{{:"$1", :_, :_, :_, :_, :_}, [], [:"$1"]}]
42+
@spec list_tenants() :: [binary]
43+
def list_tenants() do
44+
:syn_registry_by_name
45+
|> :syn_backbone.get_table_name(__MODULE__)
46+
|> :ets.select(@tenant_id_spec)
47+
end
48+
4149
@doc "Check if Connect has finished setting up connections"
4250
def ready?(tenant_id) do
4351
case whereis(tenant_id) do

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
44
def project do
55
[
66
app: :realtime,
7-
version: "2.66.3",
7+
version: "2.66.4",
88
elixir: "~> 1.18",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/realtime/monitoring/prom_ex/plugins/tenant_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ defmodule Realtime.PromEx.Plugins.TenantTest do
7575

7676
on_exit(fn -> :telemetry.detach(__MODULE__) end)
7777

78+
{:ok, _} = Realtime.Tenants.Connect.lookup_or_start_connection(tenant.external_id)
7879
{:ok, node} = Clustered.start(@aux_mod)
7980
%{tenant: tenant, node: node}
8081
end

test/realtime/tenants/connect_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ defmodule Realtime.Tenants.ConnectTest do
5050
end
5151
end
5252

53+
describe "list_tenants/0" do
54+
test "lists all tenants with active connections", %{tenant: tenant1} do
55+
tenant2 = Containers.checkout_tenant(run_migrations: true)
56+
assert {:ok, _} = Connect.lookup_or_start_connection(tenant1.external_id)
57+
assert {:ok, _} = Connect.lookup_or_start_connection(tenant2.external_id)
58+
59+
list_tenants = Connect.list_tenants() |> MapSet.new()
60+
tenants = MapSet.new([tenant1.external_id, tenant2.external_id])
61+
62+
assert MapSet.subset?(tenants, list_tenants)
63+
end
64+
end
65+
5366
describe "handle cold start" do
5467
test "multiple processes connecting calling Connect.connect", %{tenant: tenant} do
5568
parent = self()

0 commit comments

Comments
 (0)