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() 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} = {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 ->

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