# `PhoenixReplay.Session.Monitor`
[🔗](https://github.com/elixir-vibe/phoenix_replay/blob/v0.6.2/lib/phoenix_replay/session/monitor.ex#L1)

Follows each recorded session from its first event to storage.

Each recorded process is monitored. While it runs, the monitor checks
its sessions periodically and writes a session's buffered events to
storage as a chunk, with `PhoenixReplay.Session.Flusher`, once
`PhoenixReplay.Session.TailSampling` decides to keep it and `:flush` says a
chunk is due. Keeping is decided from the events seen so far, which are
observed incrementally, and never turns back, so nothing is written that
would be discarded later.

When the process exits abnormally, its exit reason is recorded as an
`:exit` event. The session is then discarded, or saved by
`PhoenixReplay.Session.Finalizer` in a supervised task, after any flush
in flight. The task closes the buffer once it saved the recording or gave
up, and emits its `PhoenixReplay.Telemetry` event after that, so a
handler observes the finished state. Sessions being saved are marked in
the buffer, so a restarted monitor leaves them to their task.

On shutdown the monitor waits for saves and flushes in flight. On start
it re-attaches to every session already in
`PhoenixReplay.Session.Buffer`, so a restart loses no recordings.

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Gives the monitor time to drain on shutdown: the application waits for
recordings being saved or written when it stops.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the monitor registered under its module name.

# `watch`

```elixir
@spec watch(pid(), PhoenixReplay.Recording.id()) :: :ok
```

Starts monitoring the session recorded by `pid`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
