Elixir SDK for LLM observability - instrument once, observe everywhere
Overview
The Untrace Elixir SDK provides zero-latency LLM observability with automatic instrumentation for all major LLM providers. Built on OpenTelemetry standards, it captures comprehensive trace data and routes it to your chosen observability platforms.
Quick Start
Start tracing LLM calls in minutes
Framework Support
Phoenix, LiveView, and OTP applications
OTP Integration
Built for Elixir’s actor model and supervision trees
Examples
Real-world examples and best practices
Installation
Add untrace_sdk to your list of dependencies in mix.exs:
def deps do [ {:untrace_sdk, "~> 0.1.2"} ]end
Then run:
mix deps.get
Quick Start
Basic Setup
# Initialize the client{:ok, client} = Untrace.Client.start_link(api_key: "your-api-key")# Send a trace event{:ok, trace} = Untrace.Client.trace(client, %{ event_type: "llm_call", data: %{ model: "gpt-4", prompt: "Hello, world!", response: "Hello! How can I help you today?", tokens_used: 25 }, metadata: %{ user_id: "user123", session_id: "session456" }})IO.puts("Trace created: #{trace.id}")# Stop the clientUntrace.Client.stop(client)
Synchronous Usage
# Initialize the client{:ok, client} = Untrace.Client.start_link(api_key: "your-api-key")# Send a trace event{:ok, trace} = Untrace.Client.trace(client, %{ event_type: "llm_call", data: %{ model: "gpt-4", prompt: "Hello, world!", response: "Hello! How can I help you today?" }})IO.puts("Trace created: #{trace.id}")# Stop the clientUntrace.Client.stop(client)
GenServer Usage
# Start as part of your supervision treechildren = [ {Untrace.Client, api_key: "your-api-key", name: :untrace_client}]Supervisor.start_link(children, strategy: :one_for_one)# Use the named process{:ok, trace} = Untrace.Client.trace(:untrace_client, %{ event_type: "llm_call", data: %{model: "gpt-4", prompt: "Hello!"}})
Configuration
Client Configuration
%{ api_key: "your-api-key", # Required base_url: "https://untrace.dev/api", # Optional, defaults to https://untrace.dev/api timeout: 30_000, # Optional, defaults to 30 seconds name: :untrace_client # Optional, for named process}
Environment Variables
The SDK supports configuration via environment variables:
defmodule MyApp.TelemetryHandlers do require Logger def handle_trace_sent(event, measurements, metadata, _config) do Logger.info("Trace sent: #{metadata.trace_id}") # Send to your monitoring system send_to_monitoring(:trace_sent, measurements, metadata) end def handle_error(event, measurements, metadata, _config) do Logger.error("Untrace error: #{metadata.error}") # Send to your error tracking system send_to_error_tracking(:untrace_error, measurements, metadata) end defp send_to_monitoring(event, measurements, metadata) do # Your monitoring logic here end defp send_to_error_tracking(event, measurements, metadata) do # Your error tracking logic here endend# Attach handlers:telemetry.attach_many( "my-app-telemetry", [ {[:untrace, :trace, :sent], &MyApp.TelemetryHandlers.handle_trace_sent/4}, {[:untrace, :error], &MyApp.TelemetryHandlers.handle_error/4} ], nil)
Examples
OpenAI Integration
defmodule MyApp.OpenAIService do require Logger def chat(prompt) do {:ok, trace} = Untrace.Client.trace(:untrace_client, %{ event_type: "llm_call", data: %{ model: "gpt-4", prompt: prompt } }) try do # Make OpenAI API call (pseudo-code) response = call_openai_api(prompt) # Update trace with response Untrace.Client.update_trace(:untrace_client, trace.id, %{ response: response, tokens_used: 100, finish_reason: "stop" }) {:ok, response} rescue error -> # Record error in trace Untrace.Client.update_trace(:untrace_client, trace.id, %{ error: Exception.message(error), error_type: error.__struct__.__name__ }) Logger.error("OpenAI API error: #{Exception.message(error)}") {:error, error} end end defp call_openai_api(prompt) do # Your OpenAI API call implementation "Generated response for: #{prompt}" endend
defmodule MyApp.StreamProcessor do def process_stream(stream) do stream |> Stream.map(&process_item/1) |> Stream.run() end defp process_item(item) do {:ok, trace} = Untrace.Client.trace(:untrace_client, %{ event_type: "stream_processing", data: %{item: item} }) try do result = process_item_logic(item) Untrace.Client.update_trace(:untrace_client, trace.id, %{ result: result, status: "completed" }) result rescue error -> Untrace.Client.update_trace(:untrace_client, trace.id, %{ error: Exception.message(error), status: "failed" }) raise error end end defp process_item_logic(item) do # Your processing logic here "Processed: #{item}" endend
OTP Integration
Supervision Tree
defmodule MyApp.Application do use Application def start(_type, _args) do children = [ # Start Untrace client {Untrace.Client, api_key: System.get_env("UNTRACE_API_KEY"), name: :untrace_client}, # Your other children MyApp.LLMProcessor, MyApp.BatchProcessor ] Supervisor.start_link(children, strategy: :one_for_one) endend
Dynamic Supervision
defmodule MyApp.DynamicProcessor do use DynamicSupervisor def start_link(init_arg) do DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__) end def init(_init_arg) do DynamicSupervisor.init(strategy: :one_for_one) end def start_processor(prompt) do child_spec = %{ id: make_ref(), start: {MyApp.Processor, :start_link, [prompt]}, restart: :temporary } DynamicSupervisor.start_child(__MODULE__, child_spec) endenddefmodule MyApp.Processor do use GenServer def start_link(prompt) do GenServer.start_link(__MODULE__, prompt) end def init(prompt) do {:ok, trace} = Untrace.Client.trace(:untrace_client, %{ event_type: "dynamic_processing", data: %{prompt: prompt} }) # Process the prompt send(self(), :process) {:ok, %{prompt: prompt, trace: trace}} end def handle_info(:process, state) do # Process the prompt result = call_llm_api(state.prompt) Untrace.Client.update_trace(:untrace_client, state.trace.id, %{ result: result, status: "completed" }) {:stop, :normal, state} endend
Best Practices
1. Use Named Processes
# Good: Use named processes for easy access{:ok, _pid} = Untrace.Client.start_link(api_key: "your-api-key", name: :untrace_client)Untrace.Client.trace(:untrace_client, trace_data)# Avoid: Managing PIDs manually{:ok, pid} = Untrace.Client.start_link(api_key: "your-api-key")Untrace.Client.trace(pid, trace_data)
defmodule MyApp.Tracing do def with_trace(event_type, data, metadata \\ %{}, fun) do {:ok, trace} = Untrace.Client.trace(:untrace_client, %{ event_type: event_type, data: data, metadata: metadata }) try do result = fun.() Untrace.Client.update_trace(:untrace_client, trace.id, %{status: "completed"}) {:ok, result} rescue error -> Untrace.Client.update_trace(:untrace_client, trace.id, %{ error: Exception.message(error), status: "failed" }) {:error, error} end endend
4. Initialize in Application
# Good: Initialize in application supervision treedefmodule MyApp.Application do use Application def start(_type, _args) do children = [ {Untrace.Client, api_key: System.get_env("UNTRACE_API_KEY"), name: :untrace_client}, # ... other children ] Supervisor.start_link(children, strategy: :one_for_one) endend