# `Playwright.Test.ServerTrace`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.7/lib/playwright/test/server_trace.ex#L1)

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 `Logger` output, on the trace's Console tab (`:error` and up as errors), as the test's
    `server-log` attachment, and under `# Server log` in `error-context.md` of 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](testing-with-exunit.md#the-server-in-the-trace)). The processes it starts with
`$callers` (its `Task`s, `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
    end

Outside 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: :warning

`Playwright.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.

# `span`

```elixir
@type span() :: String.t() | nil
```

A span of `start_span/0`, or `nil` outside a traced test.

# `span_option`

```elixir
@type span_option() :: {:params, map() | keyword()} | {:error, String.t() | nil}
```

`:params`, a map or keyword list shown in the step's Call tab (values `inspect`ed unless they
are strings), and `:error`, a message that marks the step as failed (an exception, a 5xx).

# `adopt`

```elixir
@spec adopt(pid()) :: pid() | nil
```

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.

# `forget`

```elixir
@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.

# `span`

```elixir
@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` (`t: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).

# `start_span`

```elixir
@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.

# `stop_span`

```elixir
@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` (`t:span_option/0`).

# `tracing?`

```elixir
@spec tracing?() :: boolean()
```

Whether the calling process belongs to a test that traces: a guard for costly event data.

---

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