Lesson 8 of 9 · 25m
Observability and Production Debugging on the BEAM
Use telemetry, logging, process inspection, memory metrics, tracing, and remote tools to diagnose BEAM systems without guessing.
The BEAM gives you unusually good production visibility, but only if you wire it intentionally. Too many teams deploy Phoenix with request logs and little else. Then memory balloons, a mailbox grows, or a process spins, and everyone stares at CPU graphs as if the answer will appear through vibes.
Telemetry is the backbone
Phoenix, Ecto, and many libraries emit :telemetry events. Use telemetry_metrics to expose operational measurements:
Telemetry.Metrics.summary("phoenix.endpoint.stop.duration", unit: {:native, :millisecond})
Telemetry.Metrics.summary("ecto.repo.query.total_time", unit: {:native, :millisecond})
Telemetry.Metrics.summary("vm.memory.total", unit: :byte)Logs should carry correlation
Structured logs beat prose when the system is under stress:
Logger.metadata(request_id: request_id, account_id: account.id, room_id: room.id)
Logger.info("message accepted")Do not log tokens, cookies, private message bodies, or entire inbound payloads. A security-sensitive system becomes hard to operate when engineers are afraid to read their own logs.
Inspect the living node before restarting it
In iex or a remote shell, selective process inspection can identify memory and queue hotspots:
Process.list()
|> Enum.map(fn pid -> {pid, Process.info(pid, [:memory, :message_queue_len, :current_function])} end)
|> Enum.sort_by(fn {_pid, info} -> info[:memory] end, :desc)
|> Enum.take(10)Learn the memory categories
recon is the standard production-inspection toolbox. Erlang shell examples worth recognizing include:
recon_alloc:memory(used).
recon:proc_count(memory, 10).
recon:proc_count(message_queue_len, 10).
recon:inet_count(oct, 10).Memory may be process heaps, binaries, ETS, atoms, code, or allocator fragmentation. "BEAM uses RSS" is not a diagnosis.
Trace narrowly and rehearse failure
Tracing every message on a busy node can destroy the node. A good trace has a narrow process, function, time budget, and question. OpenTelemetry adds distributed causality across HTTP, LiveView, jobs, and database calls, but still needs sampling and cardinality control.
In staging, kill room processes, block dependencies, restart nodes, and observe recovery. The goal is not zero errors. It is bounded errors, useful logs, clean restarts, and no hidden corruption.
Field notes
- Atom exhaustion is real; never call
String.to_atom/1on unbounded external input. - Large binaries can keep more memory alive than expected. Measure before optimizing copies.
- Protect and audit remote-shell access; prefer read-only diagnostics in production.
- Metrics without ownership rot quickly after architectural changes.
- Crash reports are gold. Do not swallow them with blanket rescues.
Was this lesson helpful?