# Set Up Tracing | Sentry for Elixir

With [tracing](https://docs.sentry.io/product/insights/overview.md), Sentry tracks your software performance, measuring metrics like throughput and latency, and displaying the impact of errors across multiple systems.

Tracing in Elixir SDK is available starting from 11.0.0 and it's currently in Beta.

## [Install Dependencies](https://docs.sentry.io/.md#install-dependencies)

Sentry's Elixir SDK uses OpenTelemetry for tracing. Add the required dependencies to your `mix.exs`:

```elixir
def deps do
  [
    # Sentry SDK
    {:sentry, "~> 11.0"},

    # OpenTelemetry core packages
    {:opentelemetry, "~> 1.5"},
    {:opentelemetry_api, "~> 1.4"},
    {:opentelemetry_exporter, "~> 1.0"},
    {:opentelemetry_semantic_conventions, "~> 1.27"},

    # Instrumentation libraries (choose what you need)
    {:opentelemetry_phoenix, "~> 2.0"},      # for Phoenix
    {:opentelemetry_bandit, "~> 0.1"},       # for Bandit (Phoenix 1.7+)
    {:opentelemetry_ecto, "~> 1.2"},         # for Ecto

    # ... your other dependencies
  ]
end
```

## [Configure Sentry](https://docs.sentry.io/.md#configure-sentry)

Enable tracing in your Sentry configuration:

```elixir
# config/config.exs or config/dev.exs
config :sentry,
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  traces_sample_rate: 1.0  # Adjust for production
```

## [Configure OpenTelemetry](https://docs.sentry.io/.md#configure-opentelemetry)

Set up OpenTelemetry to send traces to Sentry:

```elixir
# config/config.exs
config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []}

config :opentelemetry, sampler: {Sentry.OpenTelemetry.Sampler, []}
```

## [Set Up Phoenix Instrumentation](https://docs.sentry.io/.md#set-up-phoenix-instrumentation)

In your `application.ex`, set up the OpenTelemetry instrumentation:

```elixir
# lib/my_app/application.ex
defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    # Set up OpenTelemetry instrumentation
    OpentelemetryBandit.setup()           # for Bandit (Phoenix 1.7+)
    # OR OpentelemetryPhoenix.setup(adapter: :cowboy2)  # for Cowboy

    OpentelemetryPhoenix.setup(adapter: :bandit)
    OpentelemetryEcto.setup([:my_app, :repo], db_statement: :enabled)

    # Optional: Set up Oban instrumentation
    # OpentelemetryOban.setup()

    children = [
      # ... your supervision tree
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
```

## [Advanced Sampling](https://docs.sentry.io/.md#advanced-sampling)

For more control over sampling, you can use a sampling function:

```elixir
config :sentry,
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  traces_sampler: fn sampling_context ->
    case sampling_context.transaction_context.op do
      "http.server" -> 0.1  # Sample 10% of HTTP requests
      _ -> 0.05             # Sample 5% of other operations
    end
  end
```

Learn more about tracing [options](https://docs.sentry.io/platforms/elixir/configuration/options.md#tracing-options).
