Phoenix
Send distributed traces to Last9 from an Phoenix app using OpenTelemetry
Introduction
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↗.
Pre-requisites
-
You have a Phoenix application.
-
You have signed up for Last9, created a cluster, and obtained the following OTLP credentials:
-
endpoint
-https://otlp.last9.io
-
auth_header
- Obtain it from the Last9 dashboard.
-
Install OpenTelemetry packages
To perform the instrumentation, add the following packages to the mix.exs
file:
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:
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.
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:
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:
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.
Troubleshooting
Please get in touch with us on Discord or Email if you have any questions.