Skip to content

Commit

Permalink
fix: Improve errors logged out and erpc error handling (#1226)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Stas <[email protected]>
  • Loading branch information
filipecabaco and abc3 authored Nov 18, 2024
1 parent b2803d6 commit 31401d2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 41 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lib/realtime/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,19 @@ 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])
|> Keyword.put(: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

Expand Down
53 changes: 22 additions & 31 deletions lib/realtime/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/realtime_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 31401d2

Please sign in to comment.