Skip to content

Commit

Permalink
feat: setting log level per tenant via options (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Jan 26, 2024
1 parent 51176fd commit 298ffd2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.22
1.1.23
27 changes: 16 additions & 11 deletions lib/supavisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ defmodule Supavisor do

@registry Supavisor.Registry.Tenants

@spec start_dist(id, secrets, Node.t() | boolean()) ::
{:ok, pid()} | {:error, any()}
def start_dist(id, secrets, force_node \\ false) do
@spec start_dist(id, secrets, keyword()) :: {:ok, pid()} | {:error, any()}
def start_dist(id, secrets, options \\ []) do
options = Keyword.validate!(options, log_level: nil, force_node: false)
log_level = Keyword.fetch!(options, :log_level)
force_node = Keyword.fetch!(options, :force_node)

case get_global_sup(id) do
nil ->
node = if force_node, do: force_node, else: determine_node(id)

if node == node() do
Logger.debug("Starting local pool for #{inspect(id)}")
start_local_pool(id, secrets)
start_local_pool(id, secrets, log_level)
else
Logger.debug("Starting remote pool for #{inspect(id)}")
H.rpc(node, __MODULE__, :start_local_pool, [id, secrets])
H.rpc(node, __MODULE__, :start_local_pool, [id, secrets, log_level])
end

pid ->
Expand Down Expand Up @@ -200,8 +203,8 @@ defmodule Supavisor do
Enum.at(nodes, index)
end

@spec start_local_pool(id, secrets) :: {:ok, pid} | {:error, any}
def start_local_pool({{type, tenant}, _user, _mode, _db_name} = id, secrets) do
@spec start_local_pool(id, secrets, atom()) :: {:ok, pid} | {:error, any}
def start_local_pool({{type, tenant}, _user, _mode, _db_name} = id, secrets, log_level \\ nil) do
Logger.debug("Starting pool(s) for #{inspect(id)}")

user = elem(secrets, 1).().alias
Expand All @@ -221,12 +224,12 @@ defmodule Supavisor do
%T.Tenant{} = tenant ->
Map.put(tenant, :replica_type, :write)
end
|> supervisor_args(id, secrets)
|> supervisor_args(id, secrets, log_level)
end)

DynamicSupervisor.start_child(
{:via, PartitionSupervisor, {Supavisor.DynamicSupervisor, id}},
{Supavisor.TenantSupervisor, %{id: id, replicas: opts}}
{Supavisor.TenantSupervisor, %{id: id, replicas: opts, log_level: log_level}}
)
|> case do
{:error, {:already_started, pid}} -> {:ok, pid}
Expand All @@ -245,7 +248,8 @@ defmodule Supavisor do
defp supervisor_args(
tenant_record,
{tenant, user, mode, db_name} = id,
{method, secrets}
{method, secrets},
log_level
) do
%{
db_host: db_host,
Expand Down Expand Up @@ -303,7 +307,8 @@ defmodule Supavisor do
default_parameter_status: ps,
max_clients: max_clients,
client_idle_timeout: client_idle_timeout,
default_pool_strategy: default_pool_strategy
default_pool_strategy: default_pool_strategy,
log_level: log_level
}
end

Expand Down
16 changes: 13 additions & 3 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ defmodule Supavisor.ClientHandler do
db_name: nil,
last_query: nil,
heartbeat_interval: 0,
connection_start: System.monotonic_time()
connection_start: System.monotonic_time(),
log_level: nil
}

:gen_statem.enter_loop(__MODULE__, [hibernate_after: 5_000], :exchange, data)
Expand Down Expand Up @@ -139,7 +140,15 @@ defmodule Supavisor.ClientHandler do
Logger.debug("Client startup message: #{inspect(hello)}")
{type, {user, tenant_or_alias, db_name}} = HH.parse_user_info(hello.payload)

{:keep_state, data,
log_level =
case hello.payload["options"]["log_level"] do
nil -> nil
level -> String.to_existing_atom(level)
end

H.set_log_level(log_level)

{:keep_state, %{data | log_level: log_level},
{:next_event, :internal, {:hello, {type, {user, tenant_or_alias, db_name}}}}}

{:error, error} ->
Expand Down Expand Up @@ -274,7 +283,8 @@ defmodule Supavisor.ClientHandler do
def handle_event(:internal, :subscribe, _, data) do
Logger.debug("Subscribe to tenant #{inspect(data.id)}")

with {:ok, sup} <- Supavisor.start_dist(data.id, data.auth_secrets),
with {:ok, sup} <-
Supavisor.start_dist(data.id, data.auth_secrets, log_level: data.log_level),
{:ok, opts} <- Supavisor.subscribe(sup, data.id) do
Process.monitor(opts.workers.manager)
data = Map.merge(data, opts.workers)
Expand Down
1 change: 1 addition & 0 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule Supavisor.DbHandler do
@impl true
def init(args) do
Process.flag(:trap_exit, true)
H.set_log_level(args.log_level)
H.set_max_heap_size(150)

{_, tenant} = args.tenant
Expand Down
9 changes: 9 additions & 0 deletions lib/supavisor/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Supavisor.Helpers do
@moduledoc false
require Logger

@spec check_creds_get_ver(map) :: {:ok, String.t() | nil} | {:error, String.t()}

Expand Down Expand Up @@ -343,4 +344,12 @@ defmodule Supavisor.Helpers do
max_heap_words = div(max_heap_size * 1024 * 1024, :erlang.system_info(:wordsize))
Process.flag(:max_heap_size, %{size: max_heap_words})
end

@spec set_log_level(atom()) :: :ok
def set_log_level(nil), do: :ok

def set_log_level(level) when is_atom(level) do
Logger.notice("Setting log level to #{inspect(level)}")
Logger.put_process_level(self(), level)
end
end
2 changes: 2 additions & 0 deletions lib/supavisor/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Supavisor.Manager do

alias Supavisor.Protocol.Server
alias Supavisor.Tenants
alias Supavisor.Helpers, as: H

@check_timeout 120_000

Expand Down Expand Up @@ -33,6 +34,7 @@ defmodule Supavisor.Manager do

@impl true
def init(args) do
H.set_log_level(args.log_level)
tid = :ets.new(__MODULE__, [:public])

[args | _] = Enum.filter(args.replicas, fn e -> e.replica_type == :write end)
Expand Down
3 changes: 2 additions & 1 deletion test/supavisor/db_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Supavisor.DbHandlerTest do
user_alias: "test_user_alias",
user: "user",
mode: :transaction,
replica_type: :single
replica_type: :single,
log_level: nil
}

{:ok, :connect, data, {_, next_event, _}} = Db.init(args)
Expand Down

0 comments on commit 298ffd2

Please sign in to comment.