# `Huginn.Clickhouse.Telemetry`
[🔗](https://github.com/nyo16/huginn/blob/v0.6.0/lib/huginn/clickhouse/telemetry.ex#L1)

Telemetry events emitted by Huginn, and an optional default logger.

Every request issued through `Huginn.Clickhouse.Client` is wrapped in a
[`:telemetry`](https://hexdocs.pm/telemetry) span:

| Event | Measurements | Metadata |
| ----- | ------------ | -------- |
| `[:huginn, :query, :start]` | `:system_time`, `:monotonic_time` | `:method`, `:sql`, `:query_id`, `:pool`, `:telemetry_span_context` |
| `[:huginn, :query, :stop]` | `:duration`, `:monotonic_time` | start metadata + `:stats` and `:rows`, or `:error`, or `:halted` |
| `[:huginn, :query, :exception]` | `:duration`, `:monotonic_time` | start metadata + `:kind`, `:reason`, `:stacktrace` |
| `[:huginn, :query, :retry]` | `:attempt`, `:delay` | `:method`, `:sql`, `:query_id`, `:pool`, `:error` |

`:method` is one of `:query`, `:insert`, `:insert_stream`, `:stream_query`,
`:stream_io`. `:query_id` is the ID the query runs under on the server. It
is generated up front unless you pass `:query_id`. Durations are in
`:native` time units (use `System.convert_time_unit/3`).

`:stop` metadata depends on how the request ended:

  * success - `:stats` is the server's final statistics (or `nil`) and
    `:rows` is `stats.rows`, the number of result rows (only when the
    server sent statistics)
  * failure - `:error` is the `Huginn.Error`
  * a stream the consumer stopped early - `halted: true`

`stream_query/2` and `stream_io/3` are lazy: their `:start` is emitted when
enumeration begins, and `:stop` (or `:exception`) when it ends, so
`:duration` covers the whole enumeration.

`[:huginn, :query, :retry]` fires before each retry of a `:connection`
failure. `:attempt` is the attempt that just failed (1-based), `:delay` is
the backoff in milliseconds.

## Default logger

Attach a ready-made handler that logs each completed request:

    Huginn.attach_default_logger()
    # or with a level
    Huginn.attach_default_logger(:debug)

Detach it with `detach_default_logger/0`.

# `attach_default_logger`

```elixir
@spec attach_default_logger(Logger.level()) :: :ok | {:error, :already_exists}
```

Attaches a default `Logger` handler for Huginn query events.

`level` is the log level used for successful queries (default: `:info`);
failures and exceptions are always logged at `:error`. Returns `:ok`, or
`{:error, :already_exists}` if already attached.

# `detach_default_logger`

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

Detaches the default logger attached by `attach_default_logger/1`.

---

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