# `SefazNfe.Telemetry`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/telemetry.ex#L1)

The `:telemetry` events this library emits, per operation and per UF.

There are three families. `events/0` lists every name, for
`:telemetry.attach_many/4`.

## Operation span: `[:sefaz_nfe, :operation, :start | :stop | :exception]`

One span around every public operation of `SefazNfe`: `:authorize`,
`:authorization_result`, `:service_status`, `:consult_protocol`, `:cancel`,
`:cce`, `:manifest`, `:void_numbers`, `:dist_dfe`, `:consult_taxpayer` and
`:epec`; `SefazNfe.CTe` adds `:cte_authorize`, `:cte_consult`,
`:cte_service_status`, `:cte_cancel`, `:cte_cce`, `:cte_delivery_proof`,
`:cte_cancel_delivery_proof`, `:cte_delivery_failure` and
`:cte_cancel_delivery_failure` (model 57, or 67), and `SefazNfe.MDFe` adds
`:mdfe_service_status`, `:mdfe_authorize`, `:mdfe_consult`, `:mdfe_pending`,
`:mdfe_cancel`, `:mdfe_close`, `:mdfe_include_driver` and `:mdfe_include_dfe`
(model 58). It covers the whole call,
so an answer refused before the network
(a missing key, a bad CNPJ, `tpAmb` against `:environment`) still emits
`:start` and `:stop`, with the failure as `:outcome`: callers can count
validation errors the same way they count SEFAZ ones. Building the metadata
is a handful of map reads, never an XML parse.

Measurements are the standard `:telemetry.span/3` ones: `:system_time` on
`:start`; `:duration` (native time units) and `:monotonic_time` on `:stop`
and `:exception`.

Metadata on `:start`:

  * `:operation` - atom, one of the names above;
  * `:uf` - `String.t() | nil`: the UF the call is about, upper case. `"AN"`
    for what the Ambiente Nacional serves (`:dist_dfe`, `:manifest`, `:epec`).
    `nil` when the caller gave none, or gave something that is not a UF;
  * `:environment` - `:production | :homologation | nil`;
  * `:model` - `55 | 65 | 57 | 67 | 58 | nil`: read from `<mod>` for `:authorize`, from the
    access key for `:consult_protocol`, `:cancel` and `:cce`, from `:model`
    (default 55) for the services that take no document. `nil` where it does
    not apply (`:dist_dfe`, `:consult_taxpayer`);
  * `:contingency` - `nil | :svc_an | :svc_rs | :svc_sp | :epec`. For
    `:authorize` it follows `tpEmis` (6, 7 and 4) and for `:cte_authorize`
    too (7 is SVC-RS, 8 is SVC-SP); `:epec` is the EPEC event itself; the rest
    take the caller's `:contingency` option. Never inferred from an access key.

`:stop` carries the same keys and adds:

  * `:outcome` - `:ok` for `{:ok, _}`; otherwise the tag of the error, the
    same rule `[:sefaz_nfe, :soap]` uses (`:timeout`, `:missing_keys`,
    `:invalid_tax_id`, `:circuit_open`, ...), or `:error` when it has none;
  * `:status` - `SefazNfe.Result.status/0` (`:authorized`, `:rejected`, ...),
    `nil` when the answer is not a `SefazNfe.Result` or the call failed;
  * `:c_stat` - the SEFAZ `cStat` as an integer, from a `SefazNfe.Result`, a
    `SefazNfe.DistDFe` or a `SefazNfe.TaxpayerLookup`; `nil` otherwise.

`:outcome` is the transport and validation verdict: a rejection is
`outcome: :ok` with `status: :rejected`, because SEFAZ answered.

### What is never in the metadata

No certificate, password, CSC, signed or unsigned XML, access key (`chNFe`),
CNPJ/CPF or state registration. The first four are secrets, and metadata goes
to loggers, APM agents and third-party dashboards. The last three are
unbounded: as a metric label each value is a new time series, which is how a
Prometheus gets taken down. `:uf`, `:host`, `:environment`, `:model` and `:outcome`
are all small closed sets, which is what makes them safe tags.

## SOAP span: `[:sefaz_nfe, :soap, :start | :stop | :exception]`

One per request actually sent, with `:endpoint`, `:host`, `:uf` and `:service`
(`:nfe_autorizacao`, ...), plus `:environment`, `:model` and `:contingency`
as above, and `:outcome` on `:stop`. `:host` is `"scheme://host:port"` of the
endpoint, lower case: the server actually called, which is what the breaker
is keyed by (about forty of them, so safe as a tag). A call refused by the circuit breaker
emits no SOAP span: see the breaker events. Only the total duration exists on
the default transport; see `SefazNfe.SOAP`.

## Circuit breaker

  * `[:sefaz_nfe, :circuit_breaker, :open]` - a host's breaker tripped (it
    reached the failure threshold, or a trial call failed after the cooldown);
  * `[:sefaz_nfe, :circuit_breaker, :close]` - a call reached SEFAZ and closed
    a breaker that was open;
  * `[:sefaz_nfe, :circuit_breaker, :rejected]` - a call was refused, fast,
    because the breaker is open.

Measurements are `%{count: 1}` and, for `:open` and `:close`, also
`:system_time`. Metadata is `%{host: String.t()}`: the breaker is per server, not per UF,
because one UF reaches several servers by model and many share SVRS.

The breaker is ETS plus atomic counters and is deliberately not linearised
(see `SefazNfe.CircuitBreaker`), so concurrent failures at the threshold can
produce a duplicate `:open`. Treat `:open` as "at least once per trip".

## Telemetry.Metrics / PromEx

`telemetry_metrics` is not a dependency of this library; this is for the
host's own `Telemetry.Metrics` list, or a `PromEx` plugin's `event_metrics`:

    import Telemetry.Metrics

    [
      counter("sefaz_nfe.operation.count",
        event_name: [:sefaz_nfe, :operation, :stop],
        tags: [:operation, :uf, :outcome]
      ),
      distribution("sefaz_nfe.operation.duration",
        event_name: [:sefaz_nfe, :operation, :stop],
        measurement: :duration,
        unit: {:native, :millisecond},
        tags: [:operation, :uf],
        reporter_options: [buckets: [50, 100, 250, 500, 1_000, 2_500, 5_000, 15_000, 30_000]]
      ),
      counter("sefaz_nfe.circuit_breaker.open.count",
        event_name: [:sefaz_nfe, :circuit_breaker, :open],
        tags: [:host]
      ),
      counter("sefaz_nfe.circuit_breaker.rejected.count",
        event_name: [:sefaz_nfe, :circuit_breaker, :rejected],
        tags: [:host]
      ),
      last_value("sefaz_nfe.circuit_breaker.open.system_time",
        event_name: [:sefaz_nfe, :circuit_breaker, :open],
        measurement: :system_time,
        unit: {:native, :second},
        tags: [:host]
      )
    ]

A `nil` tag value (`:uf` of a call that gave none) is reported as an empty
label by most reporters; use `tag_values:` to map it if that matters.

# `events`

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

Every event name this library emits.

---

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