Skip to content

Commit 6274b61

Browse files
authored
Only fetch LiveView socket info if root (#734)
Closes #733.
1 parent 62f9bac commit 6274b61

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/sentry/live_view_hook.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
6868
data: params
6969
})
7070

71-
if uri = get_connect_info(socket, :uri) do
71+
if uri = get_connect_info_if_root(socket, :uri) do
7272
Context.set_request_context(%{url: URI.to_string(uri)})
7373
end
7474

75-
if user_agent = get_connect_info(socket, :user_agent) do
75+
if user_agent = get_connect_info_if_root(socket, :user_agent) do
7676
Context.set_extra_context(%{user_agent: user_agent})
7777
end
7878

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

@@ -132,6 +132,13 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
132132
{:cont, socket}
133133
end
134134

135+
defp get_connect_info_if_root(socket, key) do
136+
case socket.parent_pid do
137+
nil -> get_connect_info(socket, key)
138+
pid when is_pid(pid) -> nil
139+
end
140+
end
141+
135142
defp maybe_attach_hook_handle_params(socket) do
136143
case socket.parent_pid do
137144
nil -> attach_hook(socket, __MODULE__, :handle_params, &handle_params_hook/3)

test/sentry/live_view_hook_test.exs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule SentryTest.Live do
66
def render(assigns) do
77
~H"""
88
<h1>Testing Sentry hooks</h1>
9+
<.live_component module={SentryTest.LiveComponent} id="lc" />
910
"""
1011
end
1112

@@ -22,6 +23,14 @@ defmodule SentryTest.Live do
2223
end
2324
end
2425

26+
defmodule SentryTest.LiveComponent do
27+
use Phoenix.LiveComponent
28+
29+
def render(assigns) do
30+
~H"<p>I'm a LiveComponent</p>"
31+
end
32+
end
33+
2534
defmodule SentryTest.Router do
2635
use Phoenix.Router
2736
import Phoenix.LiveView.Router
@@ -64,9 +73,7 @@ defmodule Sentry.LiveViewHookTest do
6473
end
6574

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

7178
{:ok, view, html} = live(conn, "/hook_test")
7279
assert html =~ "<h1>Testing Sentry hooks</h1>"
@@ -113,6 +120,12 @@ defmodule Sentry.LiveViewHookTest do
113120
assert info_breadcrumb.message == ~s(:test_message)
114121
end
115122

123+
test "works with live components", %{conn: conn} do
124+
{:ok, _view, html} = live(conn, "/hook_test")
125+
assert html =~ "<h1>Testing Sentry hooks</h1>"
126+
assert html =~ "I&#39;m a LiveComponent"
127+
end
128+
116129
defp get_sentry_context(view) do
117130
{:dictionary, pdict} = Process.info(view.pid, :dictionary)
118131

0 commit comments

Comments
 (0)