diff --git a/README.md b/README.md index 589660c04..bb81833dd 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,8 @@ This is the list of operational codes that can help you understand your deployme | ErrorExecutingTransaction | Error executing a database transaction in tenant database | | SynInitializationError | Our framework to syncronize processes has failed to properly startup a connection to the database | | JanitorFailedToDeleteOldMessages | Scheduled task for realtime.message cleanup was unable to run | -| UnknownError | An unknown error occurred | +| UnknownErrorOnController | An error we are not handling correctly was triggered on a controller | +| UnknownErrorOnChannel | An error we are not handling correctly was triggered on a channel | ## License diff --git a/lib/realtime/database.ex b/lib/realtime/database.ex index 05a426767..a33403a42 100644 --- a/lib/realtime/database.ex +++ b/lib/realtime/database.ex @@ -237,14 +237,18 @@ defmodule Realtime.Database do def transaction(db_conn, func, opts) when node() == node(db_conn), do: transaction_catched(db_conn, func, opts) - def transaction(db_conn, func, opts), - do: Rpc.enhanced_call(node(db_conn), __MODULE__, :transaction, [db_conn, func, opts]) + def transaction(db_conn, func, opts) do + metadata = Keyword.take(Logger.metadata(), [:project_id, :tenant_id]) + metadata = Keyword.put(metadata, :target, node(db_conn)) - defp transaction_catched(db_conn, func, opts) do + Rpc.enhanced_call(node(db_conn), __MODULE__, :transaction, [db_conn, func, opts, metadata]) + end + + defp transaction_catched(db_conn, func, opts, metadata \\ []) do Postgrex.transaction(db_conn, func, opts) rescue e -> - log_error("ErrorExecutingTransaction", e) + log_error("ErrorExecutingTransaction", e, metadata) {:error, e} end diff --git a/lib/realtime/rpc.ex b/lib/realtime/rpc.ex index 1564db0ab..0cb52b283 100644 --- a/lib/realtime/rpc.ex +++ b/lib/realtime/rpc.ex @@ -36,38 +36,29 @@ defmodule Realtime.Rpc do def enhanced_call(node, mod, func, args \\ [], opts \\ []) do timeout = Keyword.get(opts, :timeout, Application.get_env(:realtime, :rpc_timeout)) - try do - :timer.tc(fn -> :erpc.call(node, mod, func, args, timeout) end) - catch - kind, reason -> - log_error("ErrorOnRpcCall", %{target: node, mod: mod, func: func, error: {kind, reason}}) - {:error, "RPC call error"} - else - {_, {:EXIT, reason}} -> - log_error("ErrorOnRpcCall", %{target: node, mod: mod, func: func, error: {:EXIT, reason}}, - mod: mod, - func: func, - target: node - ) + with {latency, {status, _} = response} <- + :timer.tc(fn -> :erpc.call(node, mod, func, args, timeout) end) do + Telemetry.execute( + [:realtime, :rpc], + %{latency: latency, success?: status == :ok}, + %{mod: mod, func: func, target_node: node, origin_node: node()} + ) - {:error, "RPC call error"} - - {latency, response} -> - Telemetry.execute( - [:realtime, :rpc], - %{latency: latency}, - %{ - mod: mod, - func: func, - target_node: node, - origin_node: node() - } - ) - - case response do - {status, _} when status in [:ok, :error] -> response - _ -> {:error, response} - end + case response do + {status, _} when status in [:ok, :error] -> response + _ -> {:error, response} + end end + catch + kind, reason -> + log_error( + "ErrorOnRpcCall", + %{target: node, mod: mod, func: func, error: {kind, reason}}, + mod: mod, + func: func, + target: node + ) + + {:error, "RPC call error"} end end diff --git a/lib/realtime_web/channels/realtime_channel.ex b/lib/realtime_web/channels/realtime_channel.ex index e51483e24..cc2b015d2 100644 --- a/lib/realtime_web/channels/realtime_channel.ex +++ b/lib/realtime_web/channels/realtime_channel.ex @@ -141,10 +141,10 @@ defmodule RealtimeWeb.RealtimeChannel do ) {:error, error} -> - Logging.log_error_message(:error, "UnknownError", error) + Logging.log_error_message(:error, "UnknownErrorOnChannel", error) error -> - Logging.log_error_message(:error, "UnknownError", error) + Logging.log_error_message(:error, "UnknownErrorOnChannel", error) end end @@ -419,7 +419,7 @@ defmodule RealtimeWeb.RealtimeChannel do {:error, :too_many_joins} error -> - Logging.log_error_message(:error, "UnknownError", error) + Logging.log_error_message(:error, "UnknownErrorOnCounter", error) {:error, error} end end diff --git a/lib/realtime_web/controllers/fallback_controller.ex b/lib/realtime_web/controllers/fallback_controller.ex index 5ad81e005..8e95b0571 100644 --- a/lib/realtime_web/controllers/fallback_controller.ex +++ b/lib/realtime_web/controllers/fallback_controller.ex @@ -68,7 +68,7 @@ defmodule RealtimeWeb.FallbackController do end def call(conn, response) do - log_error("UnknownError", response) + log_error("UnknownErrorOnController", response) conn |> put_status(:unprocessable_entity) diff --git a/mix.exs b/mix.exs index 6f9cfa6db..d62e1a432 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do def project do [ app: :realtime, - version: "2.33.48", + version: "2.33.49", elixir: "~> 1.16.0", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod,