Move Sentry request context to new module and load it everytime

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-03-29 11:27:53 +02:00
parent 3c8a2a1313
commit 4a8064f24f
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 36 additions and 22 deletions

View File

@ -26,22 +26,6 @@ defmodule Mobilizon.Web.Auth.Context do
user_agent = conn |> Plug.Conn.get_req_header("user-agent") |> List.first()
if Application.get_env(:sentry, :dsn) != nil do
Sentry.Context.set_request_context(%{
url: Plug.Conn.request_url(conn),
method: conn.method,
headers: %{
"User-Agent": user_agent,
Referer: conn |> Plug.Conn.get_req_header("referer") |> List.first()
},
query_string: conn.query_string,
env: %{
REQUEST_ID: conn |> Plug.Conn.get_resp_header("x-request-id") |> List.first(),
SERVER_NAME: conn.host
}
})
end
{conn, context} =
case Guardian.Plug.current_resource(conn) do
%User{id: user_id, email: user_email} = user ->

View File

@ -0,0 +1,26 @@
defmodule Mobilizon.Web.RequestContext do
@moduledoc """
Module to put some context into the request
"""
@spec put_request_context(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
def put_request_context(%Plug.Conn{} = conn, _opts \\ []) do
if Application.get_env(:sentry, :dsn) != nil do
Sentry.Context.set_request_context(%{
url: Plug.Conn.request_url(conn),
method: conn.method,
headers: %{
"User-Agent": conn |> Plug.Conn.get_req_header("user-agent") |> List.first(),
Referer: conn |> Plug.Conn.get_req_header("referer") |> List.first()
},
query_string: conn.query_string,
env: %{
REQUEST_ID: conn |> Plug.Conn.get_resp_header("x-request-id") |> List.first(),
SERVER_NAME: conn.host
}
})
end
conn
end
end

View File

@ -3,9 +3,11 @@ defmodule Mobilizon.Web.Router do
Router for mobilizon app
"""
use Mobilizon.Web, :router
import Mobilizon.Web.RequestContext
pipeline :graphql do
# plug(:accepts, ["json"])
plug(:put_request_context)
plug(Mobilizon.Web.Auth.Pipeline)
plug(Mobilizon.Web.Plugs.SetLocalePlug)
end
@ -22,29 +24,35 @@ defmodule Mobilizon.Web.Router do
end
pipeline :host_meta do
plug(:put_request_context)
plug(:accepts, ["xrd-xml"])
end
pipeline :well_known do
plug(:put_request_context)
plug(:accepts, ["json", "jrd-json"])
end
pipeline :activity_pub_signature do
plug(:put_request_context)
plug(Mobilizon.Web.Plugs.HTTPSignatures)
plug(Mobilizon.Web.Plugs.MappedSignatureToIdentity)
end
pipeline :relay do
plug(:put_request_context)
plug(Mobilizon.Web.Plugs.HTTPSignatures)
plug(Mobilizon.Web.Plugs.MappedSignatureToIdentity)
plug(:accepts, ["activity-json", "json"])
end
pipeline :activity_pub do
plug(:put_request_context)
plug(:accepts, ["activity-json"])
end
pipeline :activity_pub_and_html do
plug(:put_request_context)
plug(:accepts, ["html", "activity-json"])
plug(:put_secure_browser_headers)
@ -57,14 +65,13 @@ defmodule Mobilizon.Web.Router do
end
pipeline :atom_and_ical do
plug(:put_request_context)
plug(:put_secure_browser_headers)
plug(:accepts, ["atom", "ics", "html", "xml"])
end
pipeline :exports do
end
pipeline :browser do
plug(:put_request_context)
plug(Plug.Static, at: "/", from: "priv/static")
plug(Mobilizon.Web.Plugs.SetLocalePlug)
@ -78,9 +85,6 @@ defmodule Mobilizon.Web.Router do
plug(:put_secure_browser_headers)
end
pipeline :remote_media do
end
scope "/exports", Mobilizon.Web do
pipe_through(:browser)
get("/:format/:file", ExportController, :export)