The server's side of a test in its trace (Elixir only): the app under test runs in the test's VM, so its request and LiveView processes can report to the trace of the test whose browser they serve.
For a test that traces (trace not :off), a process that belongs to it adds:
- its
Loggeroutput, on the trace's Console tab (:errorand up as errors), as the test'sserver-logattachment, and under# Server loginerror-context.mdof a failure; - spans (a request, a LiveView event, a query) as steps under the browser action or step in
flight when they started, a span that failed (
:error) shown as failed. A failed span doesn't fail the test.
A server process doesn't know its test: adopt/1 tells it, from a process that leads to the
test (the test's own, a Task of it, or the Ecto sandbox owner it started). A plug or a
LiveView hook calls it with what the browser sent, such as the Ecto sandbox metadata in the
user agent (see The server in the trace). The processes it starts with
$callers (its Tasks, assign_async) belong to the same test. Fluffy does all of this for a
Phoenix app.
The spans come from telemetry handlers (this library doesn't depend on :telemetry), which run
in the process that emits the event: start_span/0 and stop_span/3 for an event with a
:start and a :stop, span/4 for one that reports its duration at its end:
def handle_event([:my_app, :repo, :query], %{total_time: time}, metadata, _config) do
if ServerTrace.tracing?() do
stop = System.monotonic_time()
ServerTrace.span(metadata.query, stop - time, stop, params: [params: metadata.params])
end
endOutside a traced test each function returns at once, after a process dictionary read.
The log lines are those the configured Logger level lets through: with
config :logger, level: :warning (Phoenix's config/test.exs), warnings and errors. To trace
more without more console output, lower the level and raise the console handler's:
config :logger, level: :debug
config :logger, :default_handler, level: :warningPlaywright.Test adds a :logger handler for this when its pool starts (and removes it when
it stops); a line of a process that belongs to no traced test costs it two process dictionary
reads. It formats lines with the console's formatter, and doesn't change what other handlers
and ExUnit.CaptureLog get.
Summary
Types
A span of start_span/0, or nil outside a traced test.
:params, a map or keyword list shown in the step's Call tab (values inspected unless they
are strings), and :error, a message that marks the step as failed (an exception, a 5xx).
Functions
Makes the calling process (a request, a LiveView) belong to the test of pid, for the rest of
its life or until forget/0: pid is the test's process, or one with it in its $callers or
$ancestors (one level), such as the Ecto sandbox owner of the test. Returns the test's pid,
or nil when pid leads to no running test that traces (and then calls forget/0). Adopted
for another test, the process drops the spans it left open for the last one.
Undoes adopt/1: a process serving several tests in turn (an HTTP keep-alive connection)
calls it where a request ends. Its open spans are dropped.
A span that already ended, from start to stop (System.monotonic_time/0 units), as a step
title with options (span_option/0): for an event that reports its duration at its end.
Its parent is the process's innermost open span, else the test's innermost browser action or
step that started before start and is still in flight (one that ended in between is missed).
Starts a span in the calling process: its parent is the process's innermost open span, else
the test's innermost browser action or step in flight. Returns the span for stop_span/3, or
nil outside a traced test.
Ends the span of start_span/0 (nil does nothing) as a step title of the test's trace,
with options (span_option/0).
Whether the calling process belongs to a test that traces: a guard for costly event data.
Types
@type span() :: String.t() | nil
A span of start_span/0, or nil outside a traced test.
:params, a map or keyword list shown in the step's Call tab (values inspected unless they
are strings), and :error, a message that marks the step as failed (an exception, a 5xx).
Functions
Makes the calling process (a request, a LiveView) belong to the test of pid, for the rest of
its life or until forget/0: pid is the test's process, or one with it in its $callers or
$ancestors (one level), such as the Ecto sandbox owner of the test. Returns the test's pid,
or nil when pid leads to no running test that traces (and then calls forget/0). Adopted
for another test, the process drops the spans it left open for the last one.
@spec forget() :: :ok
Undoes adopt/1: a process serving several tests in turn (an HTTP keep-alive connection)
calls it where a request ends. Its open spans are dropped.
@spec span(String.t(), integer(), integer(), [span_option()]) :: :ok
A span that already ended, from start to stop (System.monotonic_time/0 units), as a step
title with options (span_option/0): for an event that reports its duration at its end.
Its parent is the process's innermost open span, else the test's innermost browser action or
step that started before start and is still in flight (one that ended in between is missed).
@spec start_span() :: span()
Starts a span in the calling process: its parent is the process's innermost open span, else
the test's innermost browser action or step in flight. Returns the span for stop_span/3, or
nil outside a traced test.
@spec stop_span(span(), String.t(), [span_option()]) :: :ok
Ends the span of start_span/0 (nil does nothing) as a step title of the test's trace,
with options (span_option/0).
@spec tracing?() :: boolean()
Whether the calling process belongs to a test that traces: a guard for costly event data.