# Phoenix

> Send distributed traces to Last9 from an Phoenix app using OpenTelemetry

Source: https://last9.io/docs/integrations/frameworks/elixir/phoenix/

Phoenix is a web framework for the Elixir programming language. This comprehensive guide will help you instrument your Phoenix application with OpenTelemetry, seamlessly sending the traces to Last9. You can also check out the example application on [GitHub↗](https://github.com/last9/opentelemetry-examples/tree/main/elixir/phoenix).

## Pre-requisites

1. You have a Phoenix application.

2. You have signed up for [Last9](https://app.last9.io), created a cluster, and obtained the following OTLP credentials:

   1. `endpoint` - `https://otlp.last9.io`

   2. `auth_header` - Obtain it from the Last9 dashboard.

## Install OpenTelemetry packages

To perform the instrumentation, add the following packages to the `mix.exs` file:

```elixir
defp deps do
  [
    {:opentelemetry, "~> 1.4.0"},
    {:opentelemetry_api, "~> 1.3.0"},
    {:opentelemetry_ecto, "~> 1.0"},
    {:opentelemetry_exporter, "~> 1.7.0"},
    {:opentelemetry_phoenix, "~> 1.2.0"}
  ]
end
```

Now, run the following command to install the dependencies:

```bash
mix deps.get
```

## Setup instrumentation using OpenTelemetry

Add the necessary environment variables that can be obtained from the Last9 cluster's write endpoint section details.

```bash
export OTEL_SERVICE_NAME=phoenix-app-service
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.last9.io
export OTEL_EXPORTER_OTLP_AUTH_HEADER="Basic <BASIC_AUTH_HEADER>"
```

Next, in config/config.exs, add the following configuration:

```elixir
config :opentelemetry, :processors,
  otel_batch_processor: %{
    exporter:
      {:opentelemetry_exporter,
       %{
         endpoints: [System.get_env("OTEL_EXPORTER_OTLP_ENDPOINT") || "http://localhost:4317"],
         headers: [{"Authorization", System.get_env("OTEL_EXPORTER_OTLP_AUTH_HEADER")}]
       }}
  }

config :opentelemetry, :resource, service: %{name: System.get_env("OTEL_SERVICE_NAME")}
config :opentelemetry_ecto, :tracer,
  repos: [:timeline, :repo] # Add the repositories you want to trace
config :opentelemetry_phoenix, :tracer, service_name: System.get_env("OTEL_SERVICE_NAME")
```

Also, add the following in your `endpoint.ex` file:

```elixir
defmodule PhoenixAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :phoenix_app
    # ...
    plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
    # ...
  end
```

This is require by the `opentelemetry_phoenix` package to trace the Phoenix requests.

## Visualize the traces in Last9

Once the application runs with the above code, it will start pushing traces to Last9. You can see the result in action by looking at the APM dashboard in Last9.

![Image](../../../../../../assets/content/docs/integrations/frameworks/elixir/phoenix/phoenix-traces.png)

---

## Troubleshooting

Please get in touch with us on [Discord](https://discord.com/invite/Q3p2EEucx9) or [Email](mailto:support@last9.io) if you have any questions.
