|
| 1 | +--- |
| 2 | +title: Set Up Tracing |
| 3 | +sidebar_title: Tracing |
| 4 | +description: "Learn how to enable tracing in your app and discover valuable performance insights of your application." |
| 5 | +sidebar_order: 30 |
| 6 | +--- |
| 7 | + |
| 8 | +With [tracing](/product/insights/overview/), Sentry tracks your software performance, measuring metrics like throughput and latency, and displaying the impact of errors across multiple systems. |
| 9 | + |
| 10 | +<Alert> |
| 11 | + |
| 12 | +Tracing in Elixir SDK is available starting from 11.0.0 and it's currently in Beta. |
| 13 | + |
| 14 | +</Alert> |
| 15 | + |
| 16 | +## Install Dependencies |
| 17 | + |
| 18 | +Sentry's Elixir SDK uses OpenTelemetry for tracing. Add the required dependencies to your `mix.exs`: |
| 19 | + |
| 20 | +```elixir |
| 21 | +def deps do |
| 22 | + [ |
| 23 | + # Sentry SDK |
| 24 | + {:sentry, "~> 11.0"}, |
| 25 | + |
| 26 | + # OpenTelemetry core packages |
| 27 | + {:opentelemetry, "~> 1.5"}, |
| 28 | + {:opentelemetry_api, "~> 1.4"}, |
| 29 | + {:opentelemetry_exporter, "~> 1.0"}, |
| 30 | + {:opentelemetry_semantic_conventions, "~> 1.27"}, |
| 31 | + |
| 32 | + # Instrumentation libraries (choose what you need) |
| 33 | + {:opentelemetry_phoenix, "~> 2.0"}, # for Phoenix |
| 34 | + {:opentelemetry_bandit, "~> 0.1"}, # for Bandit (Phoenix 1.7+) |
| 35 | + {:opentelemetry_ecto, "~> 1.2"}, # for Ecto |
| 36 | + |
| 37 | + # ... your other dependencies |
| 38 | + ] |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +## Configure Sentry |
| 43 | + |
| 44 | +Enable tracing in your Sentry configuration: |
| 45 | + |
| 46 | +```elixir |
| 47 | +# config/config.exs or config/dev.exs |
| 48 | +config :sentry, |
| 49 | + dsn: "___PUBLIC_DSN___", |
| 50 | + traces_sample_rate: 1.0 # Adjust for production |
| 51 | +``` |
| 52 | + |
| 53 | +## Configure OpenTelemetry |
| 54 | + |
| 55 | +Set up OpenTelemetry to send traces to Sentry: |
| 56 | + |
| 57 | +```elixir |
| 58 | +# config/config.exs |
| 59 | +config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []} |
| 60 | + |
| 61 | +config :opentelemetry, sampler: {Sentry.OpenTelemetry.Sampler, []} |
| 62 | +``` |
| 63 | + |
| 64 | +## Set Up Phoenix Instrumentation |
| 65 | + |
| 66 | +In your `application.ex`, set up the OpenTelemetry instrumentation: |
| 67 | + |
| 68 | +```elixir |
| 69 | +# lib/my_app/application.ex |
| 70 | +defmodule MyApp.Application do |
| 71 | + use Application |
| 72 | + |
| 73 | + def start(_type, _args) do |
| 74 | + # Set up OpenTelemetry instrumentation |
| 75 | + OpentelemetryBandit.setup() # for Bandit (Phoenix 1.7+) |
| 76 | + # OR OpentelemetryPhoenix.setup(adapter: :cowboy2) # for Cowboy |
| 77 | + |
| 78 | + OpentelemetryPhoenix.setup(adapter: :bandit) |
| 79 | + OpentelemetryEcto.setup([:my_app, :repo], db_statement: :enabled) |
| 80 | + |
| 81 | + # Optional: Set up Oban instrumentation |
| 82 | + # OpentelemetryOban.setup() |
| 83 | + |
| 84 | + children = [ |
| 85 | + # ... your supervision tree |
| 86 | + ] |
| 87 | + |
| 88 | + opts = [strategy: :one_for_one, name: MyApp.Supervisor] |
| 89 | + Supervisor.start_link(children, opts) |
| 90 | + end |
| 91 | +end |
| 92 | +``` |
| 93 | + |
| 94 | +## Advanced Sampling |
| 95 | + |
| 96 | +For more control over sampling, you can use a sampling function: |
| 97 | + |
| 98 | +```elixir |
| 99 | +config :sentry, |
| 100 | + dsn: "___PUBLIC_DSN___", |
| 101 | + traces_sampler: fn sampling_context -> |
| 102 | + case sampling_context.transaction_context.op do |
| 103 | + "http.server" -> 0.1 # Sample 10% of HTTP requests |
| 104 | + _ -> 0.05 # Sample 5% of other operations |
| 105 | + end |
| 106 | + end |
| 107 | +``` |
| 108 | + |
| 109 | +Learn more about tracing <PlatformLink to="/configuration/options/#tracing-options">options</PlatformLink>. |
0 commit comments