Reads what the toolbar captured, without a browser: from IEx, a remote shell,
or an agent that evaluates code in the running app (such as Tidewave's
project_eval). See ADR 13.
alias PhoenixDevToolbar.Report
Report.latest(path: "/posts") |> Report.summary()
#=> %{queries: %{count: 23, failed: 0, total_us: 4180, slowest: [...]},
# n_plus_one: [%{sql: "SELECT ... WHERE post_id = ?", count: 20, ...}], ...}Sessions and targets
A session is one page view: a dead render request (kind: :request) or a
LiveView process (kind: :live_view). sessions/1 lists them newest first,
latest/1 returns the newest that matches.
The reading functions take a target: a sid, a session from sessions/1, or
a list of sids. A request's target includes the LiveView sessions that joined
from it (its connected mount), so the dead render and the connected mount read
as one page; pass children: false to read the request alone. A LiveView
reached by live navigation is a session of its own.
What you get
Plain maps with atom keys, described by the types below. Durations are in
microseconds, at is a DateTime. Values are already truncated and redacted
when captured: you read what the panel shows. New keys may be added; renaming
or removing one is a breaking change noted in the CHANGELOG.
Limits
Only what the Store still holds is readable: events are capped per session
and expire after the TTL (10 minutes by default, limits: in the config). A
session whose events were dropped or trimmed says so (dropped, trimmed).
While the toolbar is disabled every function returns [] or nil.
Every read runs in the caller's process and copies at most a few sessions' capped events: nothing runs in the host's request or LiveView processes. EXPLAIN and the process actions are not part of this API.
Summary
Types
One LiveView or LiveComponent callback (:mount, :handle_params,
:handle_event, :handle_info, :update, ...). queries and query_us
count the queries run in it, its tasks included. changes is the assigns
diff it produced (see PhoenixDevToolbar.Truncate.changes/0).
A callback/0 without params, assigns changes and times: what summary/2 lists by default.
An option of compare/3 and markdown/2.
One side of a comparison/0.
Two pages side by side (compare/3). Findings are matched by key, not by
position, so an edit that only moves a line does not change them:
n_plus_one on SQL plus origin, errors on source plus origin, both
without the line number, plus the first line of the message with pids,
references, ports and numbers normalized. fixed ones were only in
before, new ones only in after, still counts the distinct ones in
both (a count only: a group dropping from 50 repetitions to 10 is still).
Each list holds distinct keys, at most 20; fixed_total and
new_total count them all. An error carries count, its occurrences.
before and after say which kind and sids were read; a warning
is present when the two differ in kind, session count or path, or when
either side lost events (dropped, trimmed).
A failure: a request exception, a failed callback or render, a failed query. Request, LiveView and Ecto errors carry a formatted stacktrace in call order (LiveView: cut after the last host frame plus one, ADR 07). stack_args, parallel to stacktrace (the call arguments of a frame, redacted and bounded), is present only when the developer set stack_args: [agents: true] (ADR 03).
An error/0 with its origin (its first host frame, else its first frame; nil when there is none).
Findings fixed, new and kept between two pages.
An option of markdown/2.
A repeated query: the same SQL from the same origin run count times in one
callback, in callbacks callbacks so far.
One Ecto query. rows is set on success, error on failure. times holds
Ecto's measurements (:query_time, :queue_time, :decode_time, ...) in
microseconds. origin is the first stacktrace frame outside Ecto (nil unless
the repo has stacktrace: true); when the stacktrace has no frame left (a
query inside Task.async or start_async, whose fn -> Repo.all(q) end
tail calls the repo) it is the task's function (Mod.fun/arity), else
"task in View.callback" (plus the event name) of the callback that started
the task, else nil. span is the callback the query ran in:
see callbacks/2. n_plus_one? marks the query that reached the N+1
threshold and every repetition after it, as the Ecto tab does.
An error_brief/0 with its at time and the path of its session, from recent_errors/1.
An option of recent_errors/1.
The dead render request: its route and response.
One page view. owner is the LiveView process (nil for a request), parent
the session it came from (the request for a connected mount, the previous
LiveView after live navigation). events is the number stored now.
An option of sessions/1.
A session id.
A page at a glance. sids are the sessions read; session is the first.
Each slowest list holds at most 5 items, slowest first. Items are
briefs unless summary/2 got verbose: true.
dropped and trimmed add up the events lost to the caps.
callbacks counts real callbacks only. unattributed counts the queries of
LiveView sessions that no callback or render owns (usually handle_info or
PubSub reloads), with its top 5 origins by time.
An option of summary/2.
Count and time of one kind of work, on one side of a comparison/0.
The same work on both pages: delta is after minus before, so negative means less.
A sid, a session from sessions/1, or a list of sids.
Functions
Every LiveView and LiveComponent callback of target, oldest first. Option: children:.
Two pages side by side, for checking a change: what got faster, which N+1
and errors were fixed, which are new. Built from two summary/2 calls, so
before and after are targets and the options (threshold:, children:)
pass through. A connected mount reads as its request. Nil when either page
is gone. See comparison/0 for the matching rules and the warning.
Every failure of target, oldest first. Option: children:.
The newest session matching sessions/1 options, or nil.
A page as Markdown, for an agent or a bug report, or nil when no session of
target is left. Built from summary/2, so it holds at most what the summary
lists: the request line, query and callback totals, the N+1 findings, the
slowest queries and callbacks, unattributed origins and errors. Empty
sections are left out, table cells are escaped to one line, SQL that is long
or multi-line goes in a fenced block. Options: children:, threshold:,
panel_url: (a link to the page in the panel: the report ends with a footer
naming the page's sid and the link, so a pasted report leads back to it).
The repeated queries of target, most recent first.
Every query of target, oldest first.
Options: children:, threshold: (see n_plus_one/2).
The newest failures across every page the Store holds, newest first, as
recent_error/0 items: an error_brief/0 plus the session path, the
page_sid (its root: a connected mount reads as its request) and
the error's at. It sees LiveView callbacks (a handle_info crash, another
tab) that no page lookup would point at.
Lists the sessions the Store holds, newest first.
A page at a glance, or nil when no session of target is left.
The slowest queries and callbacks and the errors are compact by default
(query_brief/0, callback_brief/0, error_brief/0): read the detail
with queries/2, callbacks/2 and errors/2.
Options: verbose: (true returns the full query/0, callback/0 and
error/0 items), children: (see the module docs), threshold: (see n_plus_one/2).
Types
@type callback() :: %{ inferred: boolean(), sid: sid(), seq: pos_integer(), span: non_neg_integer() | nil, at: DateTime.t(), duration_us: non_neg_integer() | nil, callback: atom(), view: module() | nil, component: module() | nil, id: String.t() | nil, event: String.t() | nil, origin: String.t() | nil, params: String.t() | nil, changes: PhoenixDevToolbar.Truncate.changes() | nil, queries: non_neg_integer(), query_us: non_neg_integer(), error: String.t() | nil }
One LiveView or LiveComponent callback (:mount, :handle_params,
:handle_event, :handle_info, :update, ...). queries and query_us
count the queries run in it, its tasks included. changes is the assigns
diff it produced (see PhoenixDevToolbar.Truncate.changes/0).
LiveView emits no telemetry for handle_info, so inferred: true entries
stand for it: the queries of a LiveView span no callback owns, split where two
consecutive queries are more than 100 ms apart. Queries run by the LiveView
process are callback: :handle_info (also PubSub reloads); queries run by a
task (assign_async, start_async, Task.start) are callback: :async.
They have no duration, event, params or changes, and origin is the origin
of the first query (see query/0; nil on real callbacks). Several
handle_info within 100 ms in one span merge into one entry; one that runs no
query is invisible.
@type callback_brief() :: %{ seq: pos_integer(), sid: sid(), view: module() | nil, component: module() | nil, callback: atom(), event: String.t() | nil, inferred: boolean(), origin: String.t() | nil, duration_us: non_neg_integer() | nil, queries: non_neg_integer(), query_us: non_neg_integer(), error: String.t() | nil }
A callback/0 without params, assigns changes and times: what summary/2 lists by default.
@type compare_opt() :: {:children, boolean()} | {:threshold, pos_integer() | false}
An option of compare/3 and markdown/2.
@type compared_side() :: %{ sid: sid(), path: String.t() | nil, started_at: DateTime.t(), kind: :request | :live_view, sids: [sid()], dropped: non_neg_integer(), trimmed: non_neg_integer() }
One side of a comparison/0.
@type comparison() :: %{ :before => compared_side(), :after => compared_side(), :queries => tally_change(), :callbacks => tally_change(), :renders => tally_change(), :unattributed => tally_change(), :n_plus_one => findings(n_plus_one()), :errors => findings(error_brief() | error()), optional(:warning) => String.t() }
Two pages side by side (compare/3). Findings are matched by key, not by
position, so an edit that only moves a line does not change them:
n_plus_one on SQL plus origin, errors on source plus origin, both
without the line number, plus the first line of the message with pids,
references, ports and numbers normalized. fixed ones were only in
before, new ones only in after, still counts the distinct ones in
both (a count only: a group dropping from 50 repetitions to 10 is still).
Each list holds distinct keys, at most 20; fixed_total and
new_total count them all. An error carries count, its occurrences.
before and after say which kind and sids were read; a warning
is present when the two differ in kind, session count or path, or when
either side lost events (dropped, trimmed).
@type error() :: %{ :sid => sid(), :seq => pos_integer(), :at => DateTime.t(), :source => :request | :live_view | :ecto, :message => String.t(), :origin => String.t() | nil, :stacktrace => [String.t()], optional(:stack_args) => [String.t() | nil] }
A failure: a request exception, a failed callback or render, a failed query. Request, LiveView and Ecto errors carry a formatted stacktrace in call order (LiveView: cut after the last host frame plus one, ADR 07). stack_args, parallel to stacktrace (the call arguments of a frame, redacted and bounded), is present only when the developer set stack_args: [agents: true] (ADR 03).
@type error_brief() :: %{ seq: pos_integer(), sid: sid(), source: :request | :live_view | :ecto, message: String.t(), origin: String.t() | nil }
An error/0 with its origin (its first host frame, else its first frame; nil when there is none).
@type findings(item) :: %{ fixed: [item], fixed_total: non_neg_integer(), new: [item], new_total: non_neg_integer(), still: non_neg_integer() }
Findings fixed, new and kept between two pages.
@type markdown_opt() :: compare_opt() | {:panel_url, String.t() | nil}
An option of markdown/2.
@type n_plus_one() :: %{ sql: String.t() | nil, origin: String.t() | nil, count: pos_integer(), callbacks: pos_integer(), sids: [sid()] }
A repeated query: the same SQL from the same origin run count times in one
callback, in callbacks callbacks so far.
@type query() :: %{ sid: sid(), seq: pos_integer(), span: non_neg_integer() | nil, at: DateTime.t(), duration_us: non_neg_integer() | nil, repo: String.t() | nil, sql: String.t() | nil, params: String.t() | nil, source: String.t() | nil, rows: non_neg_integer() | nil, error: String.t() | nil, times: %{required(atom()) => non_neg_integer()}, origin: String.t() | nil, stacktrace: [String.t()], n_plus_one?: boolean() }
One Ecto query. rows is set on success, error on failure. times holds
Ecto's measurements (:query_time, :queue_time, :decode_time, ...) in
microseconds. origin is the first stacktrace frame outside Ecto (nil unless
the repo has stacktrace: true); when the stacktrace has no frame left (a
query inside Task.async or start_async, whose fn -> Repo.all(q) end
tail calls the repo) it is the task's function (Mod.fun/arity), else
"task in View.callback" (plus the event name) of the callback that started
the task, else nil. span is the callback the query ran in:
see callbacks/2. n_plus_one? marks the query that reached the N+1
threshold and every repetition after it, as the Ecto tab does.
@type query_brief() :: %{ seq: pos_integer(), sid: sid(), origin: String.t() | nil, duration_us: non_neg_integer() | nil, rows: non_neg_integer() | nil, source: String.t() | nil, n_plus_one?: boolean(), error: String.t() | nil }
A query/0 without SQL, params, times and stacktrace: what summary/2 lists by default.
@type recent_error() :: %{ seq: pos_integer(), sid: sid(), source: :request | :live_view | :ecto, message: String.t(), origin: String.t() | nil, at: DateTime.t(), path: String.t() | nil, page_sid: sid() }
An error_brief/0 with its at time and the path of its session, from recent_errors/1.
@type recent_errors_opt() :: {:since, sid() | DateTime.t()} | {:limit, non_neg_integer() | nil}
An option of recent_errors/1.
@type request() :: %{ method: String.t() | nil, path: String.t() | nil, route: String.t() | nil, handler: String.t() | nil, status: non_neg_integer() | nil, duration_us: non_neg_integer() | nil }
The dead render request: its route and response.
@type session() :: %{ sid: sid(), kind: :request | :live_view, parent: sid() | nil, view: module() | nil, path: String.t() | nil, owner: pid() | nil, started_at: DateTime.t(), events: non_neg_integer(), dropped: non_neg_integer(), trimmed: non_neg_integer() }
One page view. owner is the LiveView process (nil for a request), parent
the session it came from (the request for a connected mount, the previous
LiveView after live navigation). events is the number stored now.
@type sessions_opt() :: {:path, String.t() | Regex.t()} | {:view, module()} | {:kind, :request | :live_view} | {:limit, non_neg_integer() | nil} | {:since, sid() | DateTime.t()}
An option of sessions/1.
@type sid() :: String.t()
A session id.
@type summary() :: %{ sids: [sid()], session: session(), request: request() | nil, queries: %{ count: non_neg_integer(), failed: non_neg_integer(), total_us: non_neg_integer(), slowest: [query_brief() | query()] }, n_plus_one: [n_plus_one()], unattributed: %{ count: non_neg_integer(), total_us: non_neg_integer(), origins: [ %{ origin: String.t() | nil, count: pos_integer(), total_us: non_neg_integer() } ] }, callbacks: %{ count: non_neg_integer(), total_us: non_neg_integer(), slowest: [callback_brief() | callback()] }, renders: %{count: non_neg_integer(), total_us: non_neg_integer()}, errors: [error_brief() | error()], dropped: non_neg_integer(), trimmed: non_neg_integer() }
A page at a glance. sids are the sessions read; session is the first.
Each slowest list holds at most 5 items, slowest first. Items are
briefs unless summary/2 got verbose: true.
dropped and trimmed add up the events lost to the caps.
callbacks counts real callbacks only. unattributed counts the queries of
LiveView sessions that no callback or render owns (usually handle_info or
PubSub reloads), with its top 5 origins by time.
@type summary_opt() :: {:verbose, boolean()} | {:children, boolean()} | {:threshold, pos_integer() | false}
An option of summary/2.
@type tally() :: %{count: non_neg_integer(), total_us: non_neg_integer()}
Count and time of one kind of work, on one side of a comparison/0.
@type tally_change() :: %{ before: tally(), after: tally(), delta: %{count: integer(), total_us: integer()} }
The same work on both pages: delta is after minus before, so negative means less.
A sid, a session from sessions/1, or a list of sids.
Functions
Every LiveView and LiveComponent callback of target, oldest first. Option: children:.
@spec compare(target(), target(), [compare_opt()]) :: comparison() | nil
Two pages side by side, for checking a change: what got faster, which N+1
and errors were fixed, which are new. Built from two summary/2 calls, so
before and after are targets and the options (threshold:, children:)
pass through. A connected mount reads as its request. Nil when either page
is gone. See comparison/0 for the matching rules and the warning.
Every failure of target, oldest first. Option: children:.
@spec latest([sessions_opt()]) :: session() | nil
The newest session matching sessions/1 options, or nil.
@spec markdown(target(), [markdown_opt()]) :: String.t() | nil
A page as Markdown, for an agent or a bug report, or nil when no session of
target is left. Built from summary/2, so it holds at most what the summary
lists: the request line, query and callback totals, the N+1 findings, the
slowest queries and callbacks, unattributed origins and errors. Empty
sections are left out, table cells are escaped to one line, SQL that is long
or multi-line goes in a fenced block. Options: children:, threshold:,
panel_url: (a link to the page in the panel: the report ends with a footer
naming the page's sid and the link, so a pasted report leads back to it).
@spec n_plus_one(target(), keyword()) :: [n_plus_one()]
The repeated queries of target, most recent first.
Options: children:, threshold: (repetitions in one callback, at least 2,
or false). The threshold defaults to the Ecto plugin's n_plus_one: option.
Every query of target, oldest first.
Options: children:, threshold: (see n_plus_one/2).
@spec recent_errors([recent_errors_opt()]) :: [recent_error()]
The newest failures across every page the Store holds, newest first, as
recent_error/0 items: an error_brief/0 plus the session path, the
page_sid (its root: a connected mount reads as its request) and
the error's at. It sees LiveView callbacks (a handle_info crash, another
tab) that no page lookup would point at.
Options: :since (a sid: errors after that page started, the page's own
sessions, its request and connected mount, never listed, so a fix that
leaves the old crash behind reads as fixed and the old tab's later events
do not hide other pages; a DateTime: errors after it; an unknown sid or
any other value gives []) and :limit (default 20, nil for all). Reads
the events of at most the capped number of sessions the Store holds.
@spec sessions([sessions_opt()]) :: [session()]
Lists the sessions the Store holds, newest first.
Options: :path (exact string or a Regex), :view (module), :kind
(:request or :live_view), :limit, :since (a sid: only sessions that
started after that page, a connected mount reading as its request; the
request's own mount is listed since it started later, use compare/3 to
read a whole page; a DateTime: only sessions that started after it; an
unknown sid or any other value gives [], never a raise).
@spec summary(target(), [summary_opt()]) :: summary() | nil
A page at a glance, or nil when no session of target is left.
The slowest queries and callbacks and the errors are compact by default
(query_brief/0, callback_brief/0, error_brief/0): read the detail
with queries/2, callbacks/2 and errors/2.
Options: verbose: (true returns the full query/0, callback/0 and
error/0 items), children: (see the module docs), threshold: (see n_plus_one/2).