Add request context to Sentry

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-09-10 11:31:29 +02:00
parent acbe0e6c59
commit 28a2efc6eb
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773

View file

@ -25,11 +25,26 @@ defmodule Mobilizon.Web.Auth.Context do
defp set_user_information_in_context(conn) do defp set_user_information_in_context(conn) do
context = %{ip: conn.remote_ip |> :inet.ntoa() |> to_string()} context = %{ip: conn.remote_ip |> :inet.ntoa() |> to_string()}
user_agent = Plug.Conn.get_req_header(conn, "user-agent") |> List.first()
{conn, context} = {conn, context} =
case Guardian.Plug.current_resource(conn) do case Guardian.Plug.current_resource(conn) do
%User{id: user_id, email: user_email} = user -> %User{id: user_id, email: user_email} = user ->
if SentryAdapter.enabled?() do if SentryAdapter.enabled?() do
Sentry.Context.set_user_context(%{id: user_id, name: user_email}) Sentry.Context.set_user_context(%{id: user_id, name: user_email})
Sentry.Context.set_request_context(%{
url: Plug.Conn.request_url(conn),
method: conn.method,
headers: %{
"User-Agent": user_agent
},
query_string: conn.query_string,
env: %{
REQUEST_ID: Plug.Conn.get_resp_header(conn, "x-request-id") |> List.first(),
SERVER_NAME: conn.host
}
})
end end
context = Map.put(context, :current_user, user) context = Map.put(context, :current_user, user)
@ -40,14 +55,7 @@ defmodule Mobilizon.Web.Auth.Context do
{conn, context} {conn, context}
end end
context = context = if is_nil(user_agent), do: context, else: Map.put(context, :user_agent, user_agent)
case get_req_header(conn, "user-agent") do
[user_agent | _] ->
Map.put(context, :user_agent, user_agent)
_ ->
context
end
put_private(conn, :absinthe, %{context: context}) put_private(conn, :absinthe, %{context: context})
end end