Reapply sentry context (#3675)
* Reapply "Sentry context in live views (#3672)" This reverts commit 5449fead160064b8a0081c458cc5dcd34399eb0b. * Make sure `:selection_made` is handled in `GoalSettings.Form` That was a bit unepexcted.. normally `handle_info` is injected by the LiveView use macro and it discards any message gracefully. After switching to `use PlausibleWeb, :live_view` we're also using `PlausibleWeb.Live.Flash` that happens to inject its own receive clause for closing the flash. Which then renders the original, overridable, `handle_info` catch-all obsolete. * Update LV SentryContext only on connected sockets (first mount already has the right context coming from Sentry plug) * Make sure Live.ChoosePlan passes `current_user_id` session keypull/3681/head r2024.01.09-310
parent
c87b165aef
commit
24a8aa2821
@ -0,0 +1,78 @@
|
||||
defmodule PlausibleWeb.Live.SentryContext do
|
||||
@moduledoc """
|
||||
This module tries to supply LiveViews with some common Sentry context
|
||||
(without it, there is practically none).
|
||||
|
||||
Use via `use PlausibleWeb.Live.SentryContext` in your LiveView module,
|
||||
or preferably via `use PlausibleWeb, :live_view`.
|
||||
|
||||
In case you have multiple LiveViews, there is `use PlausibleWeb, live_view: :no_sentry_context`
|
||||
exposed that allows you to skip using this module. This is because
|
||||
only the root LiveView has access to `connect_info` and an exception will be
|
||||
thrown otherwise.
|
||||
"""
|
||||
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
on_mount PlausibleWeb.Live.SentryContext
|
||||
end
|
||||
end
|
||||
|
||||
def on_mount(:default, _params, session, socket) do
|
||||
if Phoenix.LiveView.connected?(socket) do
|
||||
peer = Phoenix.LiveView.get_connect_info(socket, :peer_data)
|
||||
uri = Phoenix.LiveView.get_connect_info(socket, :uri)
|
||||
|
||||
user_agent =
|
||||
Phoenix.LiveView.get_connect_info(socket, :user_agent)
|
||||
|
||||
socket_host =
|
||||
case socket.host_uri do
|
||||
:not_mounted_at_router -> :not_mounted_at_router
|
||||
%URI{host: host} -> host
|
||||
end
|
||||
|
||||
request_context =
|
||||
%{
|
||||
host: socket_host,
|
||||
env: %{
|
||||
"REMOTE_ADDR" => get_ip(peer),
|
||||
"REMOTE_PORT" => peer && peer.port,
|
||||
"SEVER_NAME" => uri && uri.host
|
||||
}
|
||||
}
|
||||
|
||||
request_context =
|
||||
if user_agent do
|
||||
Map.merge(request_context, %{
|
||||
headers: %{
|
||||
"User-Agent" => user_agent
|
||||
}
|
||||
})
|
||||
else
|
||||
request_context
|
||||
end
|
||||
|
||||
Sentry.Context.set_request_context(request_context)
|
||||
|
||||
user_id = session["current_user_id"]
|
||||
|
||||
if user_id do
|
||||
Sentry.Context.set_user_context(%{
|
||||
id: user_id
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
{:cont, socket}
|
||||
end
|
||||
|
||||
defp get_ip(%{address: addr}) do
|
||||
case :inet.ntoa(addr) do
|
||||
{:error, _} -> ""
|
||||
address -> to_string(address)
|
||||
end
|
||||
end
|
||||
|
||||
defp get_ip(_), do: ""
|
||||
end
|
||||
@ -1,4 +1,4 @@
|
||||
<%= live_render(@conn, PlausibleWeb.Live.ChoosePlan,
|
||||
id: "choose-plan",
|
||||
session: %{"user_id" => @user.id}
|
||||
session: %{"current_user_id" => @user.id}
|
||||
) %>
|
||||
|
||||
@ -0,0 +1,78 @@
|
||||
defmodule PlausibleWeb.Live.SentryContextTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
import Phoenix.LiveViewTest
|
||||
|
||||
defmodule SampleLV do
|
||||
use PlausibleWeb, :live_view
|
||||
|
||||
def mount(_params, %{"test" => test_pid}, socket) do
|
||||
socket = assign(socket, test_pid: test_pid)
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
ok computer
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("get_sentry_context", _params, socket) do
|
||||
context = Sentry.Context.get_all()
|
||||
send(socket.assigns.test_pid, {:context, context})
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
describe "sentry context" do
|
||||
test "basic shape", %{conn: conn} do
|
||||
context_hook(conn)
|
||||
assert_receive {:context, context}
|
||||
|
||||
assert %{
|
||||
extra: %{},
|
||||
request: %{
|
||||
env: %{
|
||||
"REMOTE_ADDR" => "127.0.0.1",
|
||||
"REMOTE_PORT" => port,
|
||||
"SEVER_NAME" => "www.example.com"
|
||||
},
|
||||
host: :not_mounted_at_router
|
||||
},
|
||||
user: %{},
|
||||
tags: %{},
|
||||
breadcrumbs: []
|
||||
} = context
|
||||
|
||||
assert is_integer(port)
|
||||
end
|
||||
|
||||
test "user-agent is included", %{conn: conn} do
|
||||
conn
|
||||
|> put_req_header("user-agent", "Firefox")
|
||||
|> context_hook()
|
||||
|
||||
assert_receive {:context, context}
|
||||
assert context.request.headers["User-Agent"] == "Firefox"
|
||||
end
|
||||
|
||||
test "user_id is included", %{conn: conn} do
|
||||
context_hook(conn, %{"current_user_id" => 172})
|
||||
|
||||
assert_receive {:context, context}
|
||||
assert context.user.id == 172
|
||||
end
|
||||
end
|
||||
|
||||
defp context_hook(conn, extra_session \\ %{}) do
|
||||
lv = get_liveview(conn, extra_session)
|
||||
assert render(lv) =~ "ok computer"
|
||||
render_hook(lv, :get_sentry_context, %{})
|
||||
end
|
||||
|
||||
defp get_liveview(conn, extra_session) do
|
||||
{:ok, lv, _html} =
|
||||
live_isolated(conn, SampleLV, session: Map.merge(%{"test" => self()}, extra_session))
|
||||
|
||||
lv
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue