View Source Sentry.LiveViewHook (Sentry v13.2.0)

A module that provides a Phoenix.LiveView hook to add Sentry context and breadcrumbs.

Available since v10.5.0.

This module sets context and breadcrumbs for the live view process through Sentry.Context. It sets things like:

  • The request URL
  • The user agent and user's IP address
  • Breadcrumbs for events that happen within LiveView

To get the most complete request context, you'll need to fetch information from the LiveView's WebSocket. You can do that when calling the socket/3 macro in your Phoenix endpoint. For example:

socket "/live", Phoenix.LiveView.Socket,
  websocket: [connect_info: [:peer_data, :uri, :user_agent]]

Each key in connect_info enriches the Sentry context as follows:

  • :uri — overrides the request URL with the WebSocket connect URI
  • :user_agent — adds the user agent to extra context
  • :peer_data — adds the client's IP address to user context

The hook still works when these keys are omitted, but the corresponding context fields will not be populated.

Examples

defmodule MyApp.UserLive do
  use Phoenix.LiveView

  on_mount Sentry.LiveViewHook

  # ...
end

You can do the same at the router level:

live_session :default, on_mount: Sentry.LiveViewHook do
  scope "..." do
    # ...
  end
end

You can also set this in your MyAppWeb module, so that all LiveViews that use MyAppWeb, :live_view will have this hook.

Scrubbing Sensitive Data

Available since v13.1.0.

LiveView events and handle_params calls frequently carry user-submitted form data, which may include passwords or other sensitive values. Before storing this data in breadcrumbs, this hook scrubs it using Sentry.Scrubber.scrub/2. URI query strings stored in breadcrumbs are scrubbed via Sentry.Scrubber.scrub_url/2.

To customize the scrubbing logic, pass a :scrubber option when attaching the hook. The scrubber must be a {module, function, args} tuple; the breadcrumb data map is prepended to args before invoking the function, which must return a map.

on_mount {Sentry.LiveViewHook, scrubber: {MyApp.Scrubber, :scrub, []}}

The default scrubber is equivalent to:

{Sentry.LiveViewHook, :default_scrubber, []}

The scrubber is resolved once at on_mount time and applies to every breadcrumb recorded for the lifetime of the LiveView process.

Summary

Functions

The default scrubber applied to LiveView breadcrumb data.

Functions

Link to this function

default_scrubber(data)

View Source (since 13.1.0)
@spec default_scrubber(map()) :: map()

The default scrubber applied to LiveView breadcrumb data.

Delegates to Sentry.Scrubber.scrub/2 with the default sensitive parameter keys.