From 68ede5cbd07f472d71c827a34e8a1d4535954f74 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 14 Apr 2026 15:16:27 +0200 Subject: [PATCH] feat(elixir): Add propagator, LiveView tracing, and Plug.Telemetry note Document Sentry.OpenTelemetry.Propagator for distributed tracing via sentry-trace and baggage headers. Add LiveView trace propagation setup using LiveViewPropagator and Plug.LiveViewContext. Include note about OpentelemetryPhoenix requiring Plug.Telemetry in the endpoint. Co-Authored-By: Claude --- docs/platforms/elixir/tracing/index.mdx | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/platforms/elixir/tracing/index.mdx b/docs/platforms/elixir/tracing/index.mdx index 257cc74617e04..e94552b17e022 100644 --- a/docs/platforms/elixir/tracing/index.mdx +++ b/docs/platforms/elixir/tracing/index.mdx @@ -60,11 +60,32 @@ Set up OpenTelemetry to send traces to Sentry: config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []} config :opentelemetry, sampler: {Sentry.OpenTelemetry.Sampler, []} + +# Enable distributed tracing across services via sentry-trace and baggage headers +config :opentelemetry, + text_map_propagators: [ + :trace_context, + :baggage, + Sentry.OpenTelemetry.Propagator + ] ``` ## Set Up Phoenix Instrumentation -In your `application.ex`, set up the OpenTelemetry instrumentation: +In your `application.ex`, set up the OpenTelemetry instrumentation. + + + + +`OpentelemetryPhoenix` requires your Phoenix endpoint to include `Plug.Telemetry` in order to correctly trace endpoint calls. Make sure your endpoint contains: + +```elixir +plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] +``` + +This is included by default in the Phoenix endpoint template. + + ```elixir # lib/my_app/application.ex @@ -72,6 +93,9 @@ defmodule MyApp.Application do use Application def start(_type, _args) do + # Set up LiveView trace propagation (must be BEFORE OpentelemetryPhoenix) + Sentry.OpenTelemetry.LiveViewPropagator.setup() + # Set up OpenTelemetry instrumentation OpentelemetryBandit.setup() # for Bandit (Phoenix 1.7+) # OR OpentelemetryPhoenix.setup(adapter: :cowboy2) # for Cowboy @@ -92,6 +116,20 @@ defmodule MyApp.Application do end ``` +Then add `Sentry.Plug.LiveViewContext` to your router's browser pipeline, after `:fetch_session`. This stores the trace context in the session so that LiveView processes can restore it during `mount`, `handle_params`, and `handle_event` callbacks. + +```elixir {filename:lib/my_app_web/router.ex} +pipeline :browser do + plug :accepts, ["html"] + plug :fetch_session + plug :fetch_live_flash + plug :put_root_layout, html: {MyAppWeb.Layouts, :root} + plug :protect_from_forgery + plug :put_secure_browser_headers + plug Sentry.Plug.LiveViewContext +end +``` + ## Advanced Sampling For more control over sampling, you can use a sampling function: