View Source Sentry.OpenTelemetry.LiveViewPropagator (Sentry v12.0.3)
Telemetry handler that propagates OpenTelemetry context to LiveView processes.
This module attaches telemetry handlers for LiveView lifecycle events
(mount, handle_params, handle_event) that run BEFORE opentelemetry_phoenix
creates spans, ensuring the correct parent trace context is attached.
Why This Is Needed
When a browser makes an HTTP request with distributed tracing headers, the trace context is correctly extracted for the initial request. However, Phoenix LiveView spawns new BEAM processes for WebSocket connections that handle lifecycle callbacks.
opentelemetry_phoenix uses telemetry handlers to create spans for these events.
If we don't inject the parent context BEFORE those handlers run, each LiveView
span becomes a new root trace instead of being nested under the original HTTP request.
This module solves this by:
- Using
Sentry.Plug.LiveViewContextto store trace context in the session during the initial HTTP request - Attaching telemetry handlers with higher priority (registered first) than
opentelemetry_phoenix - Extracting the context from the session and attaching it before
opentelemetry_phoenixcreates spans
Usage
Call setup/0 in your application's start function, BEFORE calling
OpentelemetryPhoenix.setup/1:
def start(_type, _args) do
# Set up Sentry's LiveView context propagation FIRST
Sentry.OpenTelemetry.LiveViewPropagator.setup()
# Then set up OpentelemetryPhoenix
OpentelemetryPhoenix.setup(adapter: :bandit)
children = [
# ...
]
Supervisor.start_link(children, strategy: :one_for_one)
endAlso add Sentry.Plug.LiveViewContext to your router pipeline:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
# ... other plugs
plug Sentry.Plug.LiveViewContext
endAvailable since v12.0.0.
Summary
Functions
Attaches telemetry handlers for LiveView context propagation.
Detaches the telemetry handlers. Mainly useful for testing.
Functions
@spec setup() :: :ok
Attaches telemetry handlers for LiveView context propagation.
Must be called BEFORE OpentelemetryPhoenix.setup/1 to ensure handlers
run in the correct order.
@spec teardown() :: :ok | {:error, :not_found}
Detaches the telemetry handlers. Mainly useful for testing.