Skip to content

Commit

Permalink
feat: add metrics for active proxy clients (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Dec 10, 2024
1 parent b53e45e commit ac88c9d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ config :logger, :console,
:type,
:app_name,
:peer_ip,
:local
:local,
:proxy
]

# Use Jason for JSON parsing in Phoenix
Expand Down
3 changes: 2 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ config :logger, :console,
:type,
:app_name,
:peer_ip,
:local
:local,
:proxy
]

# Set a higher stacktrace during development. Avoid configuring such
Expand Down
4 changes: 4 additions & 0 deletions lib/supavisor/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ defmodule Supavisor.Application do
keys: :duplicate,
name: Supavisor.Registry.TenantClients,
partitions: System.schedulers_online()},
{Registry,
keys: :duplicate,
name: Supavisor.Registry.TenantProxyClients,
partitions: System.schedulers_online()},
{Cluster.Supervisor, [topologies, [name: Supavisor.ClusterSupervisor]]},
Supavisor.Repo,
# Start the Telemetry supervisor
Expand Down
17 changes: 11 additions & 6 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Supavisor.ClientHandler do
@switch_active_count Application.compile_env(:supavisor, :switch_active_count)
@subscribe_retries Application.compile_env(:supavisor, :subscribe_retries)
@timeout_subscribe 500
@clients_registry Supavisor.Registry.TenantClients
@proxy_clients_registry Supavisor.Registry.TenantProxyClients

alias Supavisor.{
DbHandler,
Expand Down Expand Up @@ -248,7 +250,7 @@ defmodule Supavisor.ClientHandler do
local: data.local
)

Registry.register(Supavisor.Registry.TenantClients, id, [])
Registry.register(@clients_registry, id, [])

{:ok, addr} = HandlerHelpers.addr_from_sock(sock)

Expand Down Expand Up @@ -424,9 +426,6 @@ defmodule Supavisor.ClientHandler do
:proxy ->
case Supavisor.get_pool_ranch(data.id) do
{:ok, %{port: port, host: host}} ->
# Not sure if manager is alive yet
# manager_ref = Process.monitor(opts.workers.manager)

auth =
Map.merge(data.auth, %{
port: port,
Expand All @@ -437,8 +436,8 @@ defmodule Supavisor.ClientHandler do
upstream_verify: nil
})

# {:keep_state, %{data | auth: auth, manager: manager_ref},
# {:next_event, :internal, :connect_db}}
Logger.metadata(proxy: true)
set_proxy_registry(data.id)

{:keep_state, %{data | auth: auth}, {:next_event, :internal, :connect_db}}

Expand Down Expand Up @@ -1156,4 +1155,10 @@ defmodule Supavisor.ClientHandler do
HandlerHelpers.activate(data.sock)
0
end

@spec set_proxy_registry(Supavisor.id()) :: {:ok, pid()} | {:error, term()}
defp set_proxy_registry(id) do
Registry.unregister(@clients_registry, id)
Registry.register(@proxy_clients_registry, id, [])
end
end
40 changes: 40 additions & 0 deletions lib/supavisor/monitoring/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Supavisor.PromEx.Plugins.Tenant do

[
concurrent_connections(poll_rate),
concurrent_proxy_connections(poll_rate),
concurrent_tenants(poll_rate)
]
end
Expand Down Expand Up @@ -219,6 +220,45 @@ defmodule Supavisor.PromEx.Plugins.Tenant do
)
end

defp concurrent_proxy_connections(poll_rate) do
Polling.build(
:supavisor_concurrent_proxy_connections,
poll_rate,
{__MODULE__, :execute_tenant_proxy_metrics, []},
[
last_value(
[:supavisor, :proxy, :connections, :active],
event_name: [:supavisor, :proxy, :connections],
description: "The total count of active proxy clients for a tenant.",
measurement: :active,
tags: @tags
)
]
)
end

def execute_tenant_proxy_metrics do
Registry.select(Supavisor.Registry.TenantProxyClients, [{{:"$1", :_, :_}, [], [:"$1"]}])
|> Enum.frequencies()
|> Enum.each(&emit_proxy_telemetry_for_tenant/1)
end

@spec emit_proxy_telemetry_for_tenant({S.id(), non_neg_integer()}) :: :ok
def emit_proxy_telemetry_for_tenant({{{type, tenant}, user, mode, db_name, search_path}, count}) do
:telemetry.execute(
[:supavisor, :proxy, :connections],
%{active: count},
%{
tenant: tenant,
user: user,
mode: mode,
type: type,
db_name: db_name,
search_path: search_path
}
)
end

defp concurrent_tenants(poll_rate) do
Polling.build(
:supavisor_concurrent_tenants,
Expand Down

0 comments on commit ac88c9d

Please sign in to comment.