Skip to content

Erlang/Elixir

Instrument your Erlang and Elixir applications to send traces, logs, and metrics to Kopai.

Add to your mix.exs:

defp deps do
[
{:opentelemetry_api, "~> 1.5"},
{:opentelemetry, "~> 1.7"},
{:opentelemetry_exporter, "~> 1.10"},
{:opentelemetry_cowboy, "~> 1.0"} # for auto HTTP instrumentation
]
end

Add to your rebar.config:

{deps, [
{opentelemetry_api, "~> 1.5"},
{opentelemetry, "~> 1.7"},
{opentelemetry_exporter, "~> 1.10"}
]}.
config/runtime.exs
config :opentelemetry,
span_processor: :batch,
traces_exporter: :otlp
config :opentelemetry_exporter,
otlp_protocol: :http_protobuf,
otlp_endpoint: System.get_env("OTEL_EXPORTER_OTLP_ENDPOINT")
# In your application module
defmodule MyApp.Application do
use Application
require OpenTelemetry.Tracer, as: Tracer
def start(_type, _args) do
# Initialize auto-instrumentation for Cowboy
:opentelemetry_cowboy.setup()
children = [
# your supervision tree
]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
# In your handler modules
defmodule MyApp.Handler do
require OpenTelemetry.Tracer, as: Tracer
def handle_request do
Tracer.with_span "process_request" do
Tracer.set_attributes([
{"http.method", "GET"},
{"custom.attribute", "value"}
])
# ... your code
end
end
end

Set the required environment variables and run:

Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"
export OTEL_SERVICE_NAME="my-service"
mix run --no-halt
VariableDescription
OTEL_EXPORTER_OTLP_ENDPOINTKopai OTLP endpoint
OTEL_SERVICE_NAMEName shown in Kopai UI

For a complete working example:

Elixir Example