# `Juggernaut.Telemetry`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/telemetry.ex#L1)

The `:telemetry` events Juggernaut emits. Names and metadata keys are a public API: a
rename is a breaking change.

| Event | Measurements | Metadata |
| --- | --- | --- |
| `[:juggernaut, :run, :start \| :stop \| :exception]` | `duration` on stop (native) | `run_id`, `lane`, `scope`; stop adds `verdict`, `exit_code`, `counts` |
| `[:juggernaut, :phase, :start \| :stop \| :exception]` | `duration` | `run_id`, `phase` |
| `[:juggernaut, :mutant, :stop]` | `duration` (native), `tests` | `run_id`, `mutant_id`, `operator`, `status`, `kill_kind`, `selection`, `worker`, `reused?` |
| `[:juggernaut, :worker, :up \| :recycle \| :down]` | `duration` (native) | `run_id`, `worker`, `reason` |
| `[:juggernaut, :cache, :lookup]` | `hits`, `misses`, `recorded_ms` | `run_id` |

Spans use `:telemetry.span/3`, so start carries `monotonic_time` and `system_time`,
and a `telemetry_span_context` pairs start with stop. Stop metadata always includes the
start metadata. Mutant events are emitted by the controller, never by a worker node
(a dying worker emits nothing).

Handlers run in the emitting process: attach cheap ones. `attach_forwarder/2` sends
every event to a process as `{:juggernaut_telemetry, event, measurements, metadata}`.

# `attach_forwarder`

```elixir
@spec attach_forwarder(term(), pid()) :: :ok | {:error, :already_exists}
```

Forwards every Juggernaut event to `pid` as
`{:juggernaut_telemetry, event, measurements, metadata}`.

# `cache_lookup`

```elixir
@spec cache_lookup(String.t(), %{
  hits: non_neg_integer(),
  misses: non_neg_integer(),
  recorded_ms: non_neg_integer()
}) :: :ok
```

Emits the result-cache lookup counts of a run.

# `detach`

```elixir
@spec detach(term()) :: :ok | {:error, :not_found}
```

Detaches a handler attached with `attach_forwarder/2`.

# `events`

```elixir
@spec events() :: [[atom()]]
```

Every event name Juggernaut emits.

# `mutant_stop`

```elixir
@spec mutant_stop(String.t(), map()) :: :ok
```

Emits the outcome of one mutant. `info` holds `mutant_id`, `operator`, `status`,
`kill_kind`, `selection`, `worker`, `reused?`, `duration_ms` and `tests` (the number
of tests run).

# `span_phase`

```elixir
@spec span_phase(String.t(), Juggernaut.Phase.name(), (-&gt; term())) ::
  {term(), non_neg_integer()}
```

Runs `fun` inside a phase span and returns `{result, duration_ms}`.

# `span_run`

```elixir
@spec span_run(map(), (-&gt; {term(), map()})) :: term()
```

Runs `fun` inside the run span. `meta` needs `run_id`, `lane` and `scope`; `fun`
returns `{result, stop_meta}` (usually `verdict`, `exit_code`, `counts`). Returns
`result`.

# `worker`

```elixir
@spec worker(
  :up | :recycle | :down,
  String.t(),
  non_neg_integer() | :boot,
  String.t() | nil,
  non_neg_integer()
) :: :ok
```

Emits a worker event: `:up` (with its boot time), `:recycle` or `:down`.

---

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