Skip to content

Commit a60a294

Browse files
committed
Clean up docs for Sentry.PlugContext
1 parent 755773e commit a60a294

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

lib/sentry/plug_context.ex

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
defmodule Sentry.PlugContext do
22
@moduledoc """
3+
A **Plug** for adding request context to Sentry events.
4+
35
This module adds Sentry context metadata during the request in a Plug
4-
application.
6+
application. It includes defaults for scrubbing sensitive data, and options for
7+
customizing such behavior.
8+
9+
## Usage
10+
11+
You can use this module in a Plug pipeline to add Sentry metadata:
512
6-
It includes defaults for scrubbing sensitive data, and options for customizing it
7-
by default. It is intended for usage with `Sentry.PlugCapture` as metadata added
8-
here will appear in events captured.
13+
plug Sentry.PlugContext
914
10-
## Scrubbing `POST` Body Params
15+
However, this module is generally intended to be used with `Sentry.PlugCapture`:
16+
this plug will add context metadata to the request, while `Sentry.PlugCapture` will
17+
capture raised exceptions and errors and report them to Sentry with the added metadata.
18+
19+
### Scrubbing `POST` Body Params
1120
1221
In order to send `POST` body parameters you should first scrub them of sensitive
1322
information. By default, they will be scrubbed with `default_body_scrubber/1`. This
@@ -127,36 +136,34 @@ defmodule Sentry.PlugContext do
127136
@default_plug_request_id_header "x-request-id"
128137

129138
@doc false
130-
@spec build_request_interface_data(Plug.Conn.t(), keyword()) :: map()
139+
@spec build_request_interface_data(Plug.Conn.t(), keyword()) :: Sentry.Context.request_context()
131140
def build_request_interface_data(conn, opts) do
132141
body_scrubber = Keyword.get(opts, :body_scrubber, {__MODULE__, :default_body_scrubber})
133-
134142
header_scrubber = Keyword.get(opts, :header_scrubber, {__MODULE__, :default_header_scrubber})
135-
136143
cookie_scrubber = Keyword.get(opts, :cookie_scrubber, {__MODULE__, :default_cookie_scrubber})
137144

138-
request_id = Keyword.get(opts, :request_id_header) || @default_plug_request_id_header
139-
140145
remote_address_reader =
141146
Keyword.get(opts, :remote_address_reader, {__MODULE__, :default_remote_address_reader})
142147

148+
request_id_header = Keyword.get(opts, :request_id_header, @default_plug_request_id_header)
149+
143150
conn =
144151
Plug.Conn.fetch_cookies(conn)
145152
|> Plug.Conn.fetch_query_params()
146153

147154
%{
148155
url: Plug.Conn.request_url(conn),
149156
method: conn.method,
150-
data: handle_data(conn, body_scrubber),
157+
data: apply_fun_with_conn(conn, body_scrubber),
151158
query_string: conn.query_string,
152-
cookies: handle_data(conn, cookie_scrubber),
153-
headers: handle_data(conn, header_scrubber),
159+
cookies: apply_fun_with_conn(conn, cookie_scrubber),
160+
headers: apply_fun_with_conn(conn, header_scrubber),
154161
env: %{
155-
"REMOTE_ADDR" => handle_data(conn, remote_address_reader),
162+
"REMOTE_ADDR" => apply_fun_with_conn(conn, remote_address_reader),
156163
"REMOTE_PORT" => remote_port(conn),
157164
"SERVER_NAME" => conn.host,
158165
"SERVER_PORT" => conn.port,
159-
"REQUEST_ID" => Plug.Conn.get_resp_header(conn, request_id) |> List.first()
166+
"REQUEST_ID" => conn |> Plug.Conn.get_resp_header(request_id_header) |> List.first()
160167
}
161168
}
162169
end
@@ -166,15 +173,11 @@ defmodule Sentry.PlugContext do
166173
def default_remote_address_reader(conn) do
167174
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do
168175
[header_value | _rest] ->
169-
header_value
170-
|> String.split(",")
171-
|> hd()
172-
|> String.trim()
176+
[address | _rest] = String.split(header_value, ",", parts: 2)
177+
String.trim(address)
173178

174179
[] ->
175-
conn.remote_ip
176-
|> :inet.ntoa()
177-
|> case do
180+
case :inet.ntoa(conn.remote_ip) do
178181
{:error, _} -> ""
179182
address -> to_string(address)
180183
end
@@ -188,15 +191,9 @@ defmodule Sentry.PlugContext do
188191
end
189192
end
190193

191-
defp handle_data(_conn, nil), do: %{}
192-
193-
defp handle_data(conn, {module, fun}) do
194-
apply(module, fun, [conn])
195-
end
196-
197-
defp handle_data(conn, fun) when is_function(fun) do
198-
fun.(conn)
199-
end
194+
defp apply_fun_with_conn(_conn, _function = nil), do: %{}
195+
defp apply_fun_with_conn(conn, {module, fun}), do: apply(module, fun, [conn])
196+
defp apply_fun_with_conn(conn, fun) when is_function(fun, 1), do: fun.(conn)
200197

201198
@doc """
202199
Scrubs **all** cookies off of the request.

0 commit comments

Comments
 (0)