# Telemetry

> Each use case in production, from Sypnia Telemetry.

Applies to: Sypnia Cloud (paid service).  
Source: https://cases.docs.sypnia.com/links/telemetry/

For agents: Telemetry link: spans with attributes sypnia.case=<case id> and sypnia.case.outcome=<Outcome|Failure|error> (set by Sypnorm and the runtime: every run is a span `usecase <case id>` with sypnia.case; with the OpenTelemetry API registered, sypnia.case.outcome goes on that span; with @sypnia/telemetry/node 0.4.0 or later, on that span too (setSpanAttributes); an older @sypnia/telemetry gets a second span `usecase <case id> → <Outcome>` with both attributes; by hand: setSpanAttributes from @sypnia/telemetry 0.4.0, or span.setAttribute of the OpenTelemetry API). Link setup: a map owner links a Telemetry project with PUT /v1/maps/{map}/telemetry { project, token, api? } (the token kept encrypted with AES-256-GCM under CASES_SECRET_KEY); Cases reads GET {api}/v1/cases?case=&project= for each case. Per case: runs per day, how runs end (each outcome and failure counted), errors, p50/p95 latency, outcomes seen vs declared (an undeclared ending is a map gap), recent problems. Events: the whole chain is one trace (traceparent travels in the event), each delivery's wait and attempts; dead letters and LoopStopped open problems and alert; WindowExpired for on_all. Navigation: every page_view, click, rage and dead click carries place (innermost data-sypnia-place; ids only); `cases check`, with map.yaml app.telemetry (or [cases] telemetry) and SYPNIA_TELEMETRY_TOKEN (read), reads 30 days of production: nav-missing-edge, nav-unreached, nav-unknown-route, case-unused, undeclared-outcome and unmapped; --no-telemetry skips it. Telemetry is the pilot of Sypnia: it uses every Sypnia product and every case of theirs reports to it. 

When a request serves a use case, its trace carries the attribute `sypnia.case` with the case id, and `sypnia.case.outcome` with how the run ended. Sypnorm and the runtime set both for you: every run is a span `usecase <case id>`. With the OpenTelemetry API registered in your service, the outcome goes on that same span. With [`@sypnia/telemetry/node`](https://telemetry.docs.sypnia.com/sdks/node/) 0.4.0 or later, it goes on that span too, through `setSpanAttributes`. An older `@sypnia/telemetry` records the ending as a second, short span `usecase <case id> → <Outcome>` that carries both attributes.

Other code sets them on the active span, with `setSpanAttributes` from `@sypnia/telemetry` 0.4.0 or later, or with the OpenTelemetry API that Sypnia Telemetry accepts:

```ts
import { setSpanAttributes } from '@sypnia/telemetry/node';
setSpanAttributes({ 'sypnia.case': 'place-order', 'sypnia.case.outcome': 'OrderPlaced' });

// or, with the OpenTelemetry API
import { trace } from '@opentelemetry/api';
trace.getActiveSpan()?.setAttribute('sypnia.case', 'place-order');
```

## Link it

A map's owner links a Telemetry project, with a Telemetry API token that can read it:

```bash
curl -X PUT https://cases.api.sypnia.com/v1/maps/la-espiga/telemetry \
  -H "Authorization: Bearer $SYPNIA_TOKEN" -H "Content-Type: application/json" \
  -d '{ "project": "la-espiga", "token": "<telemetry API token (read)>" }'
```

Cases keeps the token encrypted (AES-256-GCM) and reads each case's figures from Telemetry's [use cases endpoint](https://telemetry.docs.sypnia.com/api/traces/#list-use-cases), `GET /v1/cases?case=<case id>`. `DELETE` on the same path unlinks it. A token Telemetry refuses shows on the case as «not allowed», and a Telemetry that does not answer within the link's time budget as «not available right now»; the rest of the case still shows.

Linked, each case shows:

- **How often it runs**, and its trend.
- **How it ends**: each declared outcome and failure, counted. «Order for tomorrow ended in `SoldOut` 14 times today» is one number, with the words people read. For a case that starts with `on_all`, waits whose window closed first are counted here too, as `WindowExpired`.
- **Its errors and latency**: errors nobody declared, p50 and p95.
- **Endings seen against endings declared.** An error that keeps happening is often a failure the case should name: Cases suggests a proposal that adds it, with what people saw.
- **Its recent problems**, linked to Telemetry.

## Chains of events

An event carries its trace, so a whole chain is **one trace** in Telemetry: «Check the weather» at 4 pm, «Pause deliveries» a few milliseconds later, «Tell the baker» after that, each with the time its delivery waited and the attempts it took. Telemetry also opens a problem, and alerts the people who watch the project, when:

- a listener has **dead letters** ([Delivery](https://cases.docs.sypnia.com/runtime/delivery#dead-letters));
- a chain was cut with **`LoopStopped`** ([Loops](https://cases.docs.sypnia.com/runtime/loops));
- an `on_all` window closed before every outcome arrived (**`WindowExpired`**).

## Real clicks confirm the navigation map

With the browser SDK (`@sypnia/telemetry` 0.4.0 or later), every page view, click, rage click and dead click carries `place`: the id of the innermost `data-sypnia-place` around it, a name from your code, never the page's content ([Use cases and places](https://telemetry.docs.sypnia.com/concepts/use-cases-and-places/)). [The check](https://cases.docs.sypnia.com/contract/the-check) compares the last 30 days of production with the [navigation map](https://cases.docs.sypnia.com/map/navigation#generated-from-the-code-checked-by-real-clicks) and reports what does not match: a way people move that the tree lacks (`nav-missing-edge`), a place nobody reached (`nav-unreached`), a route with no place (`nav-unknown-route`), a case that never ran (`case-unused`), and, from what production ran, endings the case does not declare (`undeclared-outcome`) and cases the map does not have (`unmapped`). A move along the tree is fine: to a child, back up, to a sibling, to a root, or along a shortcut. Endings the runtime records itself (`error`, `WindowExpired`, `LoopStopped`) are never a gap.

The check reads Telemetry when the map names its project and a read token is at hand:

`cases/map.yaml`

```yaml
app:
  telemetry: la-espiga
```

```bash
SYPNIA_TELEMETRY_TOKEN=<telemetry API token (read)> npx @sypnia/cases check
```

`[cases] telemetry` in `sypnia.toml` names the project too, and `SYPNIA_TELEMETRY_API` points at another Telemetry engine. Without a token the check says so and goes on; `--no-telemetry` skips it.

## Sypnia uses it this way

Sypnia Telemetry is Sypnia's pilot: it uses every other Sypnia product, and every case of every product reports to it. When a case of Sypnia Data or Sypnia Git ends in a failure or a dead letter, Sypnia's own team sees it here first.
