Skip to content

Commit

Permalink
Only fetch LiveView socket info if root
Browse files Browse the repository at this point in the history
Closes #733.
  • Loading branch information
whatyouhide committed May 31, 2024
1 parent 62f9bac commit ae5710c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/sentry/live_view_hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
data: params
})

if uri = get_connect_info(socket, :uri) do
if uri = get_connect_info_if_root(socket, :uri) do
Context.set_request_context(%{url: URI.to_string(uri)})
end

if user_agent = get_connect_info(socket, :user_agent) do
if user_agent = get_connect_info_if_root(socket, :user_agent) do
Context.set_extra_context(%{user_agent: user_agent})
end

# :peer_data returns t:Plug.Conn.Adapter.peer_data/0.
# https://hexdocs.pm/plug/Plug.Conn.Adapter.html#t:peer_data/0
if ip_address = socket |> get_connect_info(:peer_data) |> get_safe_ip_address() do
if ip_address = socket |> get_connect_info_if_root(:peer_data) |> get_safe_ip_address() do
Context.set_user_context(%{ip_address: ip_address})
end

Expand Down Expand Up @@ -132,6 +132,13 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
{:cont, socket}
end

defp get_connect_info_if_root(socket, key) do
case socket.parent_pid do
nil -> get_connect_info(socket, key)
pid when is_pid(pid) -> nil
end
end

defp maybe_attach_hook_handle_params(socket) do
case socket.parent_pid do
nil -> attach_hook(socket, __MODULE__, :handle_params, &handle_params_hook/3)
Expand Down
4 changes: 1 addition & 3 deletions test/sentry/live_view_hook_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ defmodule Sentry.LiveViewHookTest do
end

test "attaches the right context", %{conn: conn} do
conn =
conn
|> Plug.Conn.put_req_header("user-agent", "sentry-testing 1.0")
conn = Plug.Conn.put_req_header(conn, "user-agent", "sentry-testing 1.0")

{:ok, view, html} = live(conn, "/hook_test")
assert html =~ "<h1>Testing Sentry hooks</h1>"
Expand Down

0 comments on commit ae5710c

Please sign in to comment.