sentry v7.2.4 Sentry.Plug
Provides basic functionality to handle Plug.ErrorHandler
Usage
Add the following to your router.ex:
use Plug.ErrorHandler
use Sentry.Plug
Note that using Sentry.Plug will override default behaviour of Plug.ErrorHandler - it will no longer write "Something went wrong" as a response. If you want to retain this behaviour, or in general to add custom logic on top of sending event to sentry, you can do it like this:
defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
super(conn, error)
send_resp(conn, conn.status, "Something went wrong")
end
Sending Post Body Params
In order to send post body parameters you should first scrub them of sensitive
information. By default, they will be scrubbed with
Sentry.Plug.default_body_scrubber/1
. It can be overridden by passing
the body_scrubber
option, which accepts a Plug.Conn
and returns a map
to send. Setting :body_scrubber
to nil
will not send any data back.
If you would like to make use of Sentry's default scrubber behavior in a custom
scrubber, it can be called directly. An example configuration may look like
the following:
def scrub_params(conn) do
# Makes use of the default body_scrubber to avoid sending password
# and credit card information in plain text. To also prevent sending
# our sensitive "my_secret_field" and "other_sensitive_data" fields,
# we simply drop those keys.
Sentry.Plug.default_body_scrubber(conn)
|> Map.drop(["my_secret_field", "other_sensitive_data"])
end
Then pass it into Sentry.Plug:
use Sentry.Plug, body_scrubber: &scrub_params/1
You can also pass it in as a {module, fun}
like so:
use Sentry.Plug, body_scrubber: {MyModule, :scrub_params}
Please Note: If you are sending large files you will want to scrub them out.
Headers Scrubber
By default Sentry will scrub Authorization and Authentication headers from all
requests before sending them. It can be configured similarly to the body params
scrubber, but is configured with the :header_scrubber
key.
def scrub_headers(conn) do
# default is: Sentry.Plug.default_header_scrubber(conn)
#
# We do not want to include Content-Type or User-Agent in reported
# headers, so we drop them.
Enum.into(conn.req_headers, %{})
|> Map.drop(["content-type", "user-agent"])
end
Then pass it into Sentry.Plug:
use Sentry.Plug, header_scrubber: &scrub_headers/1
It can also be passed in as a {module, fun}
like so:
use Sentry.Plug, header_scrubber: {MyModule, :scrub_headers}
Cookie Scrubber
By default Sentry will scrub all cookies before sending events.
It can be configured similarly to the headers scrubber, but is configured with the :cookie_scrubber
key.
To configure scrubbing, we can set all configuration keys:
use Sentry.Plug, header_scrubber: &scrub_headers/1, body_scrubber: &scrub_params/1, cookie_scrubber: &scrub_cookies/1
Including Request Identifiers
If you're using Phoenix, Plug.RequestId, or another method to set a request ID
response header, and would like to include that information with errors
reported by Sentry.Plug, the :request_id_header
option allows you to set
which header key Sentry should check. It will default to "x-request-id",
which Plug.RequestId (and therefore Phoenix) also default to.
use Sentry.Plug, request_id_header: "application-request-id"
Collect User Feedback after Error
Sentry allows collecting user feedback after they hit an error, and is configured under the
:collect_feedback
key. Information and configuration options are available
here.
When enabled, if a Plug request experiences an error that Sentry is reporting and the request
accepts the content-type "text/html" or "/", the feedback form will be rendered.
The feedback form can be enabled by passing [enabled: true]
. The form also supports
all of the configuration in the Sentry documentation linked above. These options can be
passed as a configuration map under the :options
key. Example:
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "Sorry about that!"}]
Link to this section Summary
Link to this section Functions
build_request_interface_data(conn, opts)
build_request_interface_data(Plug.Conn.t(), keyword()) :: map()