Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metrics for active proxy clients #508

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
@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 @@
local: data.local
)

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

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

Expand All @@ -275,7 +277,7 @@

key = {:secrets, tenant_or_alias, user}

case auth_secrets(info, user, key, :timer.hours(24)) do

Check warning on line 280 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 3).
{:ok, auth_secrets} ->
Logger.debug("ClientHandler: Authentication method: #{inspect(auth_secrets)}")

Expand Down Expand Up @@ -334,7 +336,7 @@
Cachex.get(Supavisor.Cache, key) == {:ok, nil} do
case auth_secrets(info, data.user, key, 15_000) do
{:ok, {method2, secrets2}} = value ->
if method != method2 or Map.delete(secrets.(), :client_key) != secrets2.() do

Check warning on line 339 in lib/supavisor/client_handler.ex

View workflow job for this annotation

GitHub Actions / Code style

Function body is nested too deep (max depth is 2, was 4).
Logger.warning("ClientHandler: Update secrets and terminate pool")

Cachex.update(
Expand Down Expand Up @@ -424,9 +426,6 @@
: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 @@
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 @@
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
Loading