View Source Sentry.PlugCapture (Sentry v14.0.0)
Ensures proper error reporting for Plug applications that use Cowboy.
It is intended for usage with Sentry.PlugContext, which adds relevant request
metadata to the Sentry context before errors are captured.
Only for Cowboy
Sentry.PlugCaptureis only recommended for Cowboy applications. For applications running on Bandit, which is the most recent default webserver in Phoenix,Sentry.PlugContextshould be enough, and usingSentry.PlugCapturemight result in duplicate errors.
Usage
With Phoenix
In a Phoenix application, it is important to use this module before
the Phoenix endpoint itself. It should be added to your endpoint.ex file:
defmodule MyApp.Endpoint
use Sentry.PlugCapture
use Phoenix.Endpoint, otp_app: :my_app
# ...
endWith Plug
In a Plug application, you can add this module below your router:
defmodule MyApp.PlugRouter do
use Plug.Router
use Sentry.PlugCapture
# ...
end
use Sentry.PlugCaptureWhen you
use Sentry.PlugCapture, Sentry overrides yourPlug.call/2callback and adds capturing errors and reporting to Sentry. You can still re-override that callback afteruse Sentry.PlugCaptureif you need to.
Scrubbing Sensitive Data
Since v9.1.0
Scrubbing sensitive data in
Sentry.PlugCaptureis available since v9.1.0 of this library.
Like Sentry.PlugContext, this module also supports scrubbing sensitive data
out of errors. However, this module has to do some guessing to figure
out if there are Plug.Conn structs to scrub. Right now, the strategy we
use follows these steps:
- if the error is
Phoenix.ActionClauseError, we scrub thePlug.Connin theargsfield of that exception, and mirror that conn's scrubbed params onto the action's standalone params argument so both are redacted consistently
Scrubbing goes through the same Sentry.Scrubber implementation as
Sentry.PlugContext, so it honors the per-field scrubbers (:body_scrubber,
:header_scrubber, :cookie_scrubber, :url_scrubber) configured on
Sentry.PlugContext for the current request.
Otherwise, we don't perform any scrubbing. To configure scrubbing, you can use the
:scrubber option (see below).
Options
:scrubber(since v9.1.0) - a term of type{module, function, args}that will be invoked to scrub sensitive data fromPlug.Connstructs. ThePlug.Connstruct is prepended toargsbefore invoking the function, so that the final function will be called asapply(module, function, [conn | args]). The function must return aPlug.Connstruct; if it returns anything else, or if it crashes, scrubbing falls back to the built-in scrubber (see Crashing Callbacks below). By default, the built-in scrubber delegates toSentry.Scrubber.scrub/1, which honors any:body_scrubber,:header_scrubber,:cookie_scrubber, or:url_scrubberopts configured onSentry.PlugContextfor the current request. When noSentry.PlugContexthas run, falls back to the defaults defined bySentry.Scrubber.scrub/2:- scrubs all cookies (
cookiesandreq_cookies) - drops sensitive request headers (
authorization,authentication,cookie) - scrubs
paramsandbody_paramsthrough the configuredbody_scrubber(defaulting to the sensitive params inSentry.Scrubber.default_param_keys/0; anilbody_scrubberempties both), and scrubs the same sensitive params inquery_paramsandpath_params - derives
request_path,path_infoandquery_stringfrom the URL the configuredurl_scrubberreturns, so a scrubber that redacts a path segment redacts it here too;query_stringis scrubbed against the sensitive params either way - clears
assigns(where auth libraries store user structs and tokens) - reduces
privateto an allow-list of framework metadata, dropping everything else (notably the decoded session under:plug_session); configurable via thescrubber: [conn_private_allow_list: ...]option
- scrubs all cookies (
Crashing Callbacks
Available since 14.0.0.
If anything in the capture path raises, throws, or exits - the :scrubber
callback, the scrubbing of the exception, or the reporting itself - this
module still re-raises the application's original exception, unchanged.
Only the reporting degrades, and only as far as the failure forces:
| Failure | What Sentry still reports |
|---|---|
The :scrubber crashes, or returns something other than a Plug.Conn | The event, with the conn scrubbed by the built-in scrubber, Sentry.Scrubber.scrub/1 |
Scrubbing a Phoenix.ActionClauseError fails for any other reason | The event, with each of the exception's arguments scrubbed on its own, without mirroring the conn's scrubbed params onto the action's params argument |
| Capturing the event itself fails | Nothing - the error-level log is the only record of the error |
See the Crashing Callbacks section of
the Sentry documentation for how the failure is logged and why a crashed
scrubber sends data that only it was dropping.