From 28a2efc6ebef75ae7484afafeff99c4c8bb4b68c Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 10 Sep 2021 11:31:29 +0200 Subject: [PATCH] Add request context to Sentry Signed-off-by: Thomas Citharel --- lib/web/auth/context.ex | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/web/auth/context.ex b/lib/web/auth/context.ex index 1d472156c..9e96dac25 100644 --- a/lib/web/auth/context.ex +++ b/lib/web/auth/context.ex @@ -25,11 +25,26 @@ defmodule Mobilizon.Web.Auth.Context do defp set_user_information_in_context(conn) do context = %{ip: conn.remote_ip |> :inet.ntoa() |> to_string()} + user_agent = Plug.Conn.get_req_header(conn, "user-agent") |> List.first() + {conn, context} = case Guardian.Plug.current_resource(conn) do %User{id: user_id, email: user_email} = user -> if SentryAdapter.enabled?() do 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 context = Map.put(context, :current_user, user) @@ -40,14 +55,7 @@ defmodule Mobilizon.Web.Auth.Context do {conn, context} end - context = - case get_req_header(conn, "user-agent") do - [user_agent | _] -> - Map.put(context, :user_agent, user_agent) - - _ -> - context - end + context = if is_nil(user_agent), do: context, else: Map.put(context, :user_agent, user_agent) put_private(conn, :absinthe, %{context: context}) end