---
title: Signals
description: Every metric, span and log event the server emits, and the privacy rule with the tests that enforce it.
sidebar:
  icon: waves
---

All of it is in `server/app/telemetry.py`. `server/app/__main__.py` calls `telemetry.setup()` once, then `telemetry.instrument(app)`, and wraps each suggestion request in `telemetry.record_suggestion()`.

## Identity

| Setting | Value | Set by |
|---|---|---|
| `service.name` | `patent-server` | `OTEL_SERVICE_NAME` (compose, task definition) |
| `deployment.environment.name` | `local`, `dev`, `staging`, `prod` | `OTEL_RESOURCE_ATTRIBUTES` |
| `service.version` | The release commit SHA | Baked into the image: `server/Dockerfile` `ARG RELEASE_SHA`, passed by `scripts/ci/build-server-image.sh` from `GITHUB_SHA`. The task definition does not carry it, because a release only swaps the image. |
| Export | OTLP/HTTP to `OTEL_EXPORTER_OTLP_ENDPOINT` | Unset or empty: no exporter is created at all |
| Trace sampling | Parent-based ratio, `OTEL_TRACES_SAMPLER_ARG` | Local 1.0; `trace_sample_ratio` in `envs/*/terraform.tfvars`: dev 1.0, staging 1.0, prod 0.1 (starting values) |

## Metrics

Meter `patent.server`. HTTP server metrics come from the FastAPI instrumentation; the httpx client metrics are switched off.

| Instrument | Type | Unit | Attributes | Prometheus name | In CloudWatch |
|---|---|---|---|---|---|
| `suggestions.requests` | Counter | `{request}` | `outcome` | `suggestions_requests_total` | Yes, `Patent/Server`, dimension `outcome` |
| `suggestions.duration` | Histogram, buckets 0.25 to 60 s | `s` | `outcome` | `suggestions_duration_seconds_*` | No |
| `suggestions.in_flight` | UpDownCounter | `{request}` | none | `suggestions_in_flight` | No |
| `websocket.connections.active` | UpDownCounter | `{connection}` | none | `websocket_connections_active` | No |

The AWS collector drops every metric except `suggestions.requests` (`filter/alarm_metric` in `aws.yaml`). The EMF exporter writes one log event for every metric it receives, matched or not, so the others would cost log ingestion and feed no alarm.

| `outcome` | Meaning | Counts against the objective |
|---|---|---|
| `ok` | The model output validated as `Suggestions`; the reply is then sent | Yes (success) |
| `bad_request` | The client message did not parse or validate, or was not a text frame | No |
| `ai_error` | The model call raised, including timeouts | Yes (failure) |
| `invalid_response` | The model output did not validate as `Suggestions` | Yes (failure) |

`ok` is recorded when the response is produced, before it is sent. A client that has gone away does not count as an AI failure.

## Traces

- **One trace per suggestion request.** The root span is `suggestions.review`, started with an empty context. It carries `suggestions.request_id`, `suggestions.document.chars` (the length of the text sent to the model), `suggestions.issue_count`, `suggestions.outcome`, and `error.type` on failure.
- **The OpenAI call** is a child `POST` span from the httpx instrumentation: method, URL and status code. It records no headers and no bodies. For a streamed response it ends when the headers arrive, not when the stream ends.
- **No span for `/healthz` or `/ws`.** The ASGI instrumentation would make one span for the whole WebSocket connection, which can last hours. Both paths are excluded (`excluded_urls="/healthz$,/ws$"`), and each message starts its own trace instead.
- **HTTP routes** (`/api/document`, `/api/save`) get one server span each, with no per-message child spans.

## Logs

JSON lines on stdout from the standard `logging` module, one event per line. Every line has `timestamp`, `level`, `event`, `trace_id`, `span_id` and `service.version`.

| Event | Extra fields |
|---|---|
| `suggestion.completed` | `outcome`, `request_id`, `document.chars`, `issue_count` |
| `suggestion.failed` | `outcome`, `request_id`, `document.chars`, `error.type` |
| `websocket.opened`, `websocket.closed` | `client.address` |
| `audit.document.read`, `audit.document.saved` | `document.id`, `document.chars`, `client.address` |

Two more events, `telemetry.setup_failed` and `telemetry.instrument_failed`, appear only if telemetry itself fails to start. The app keeps serving.

Uvicorn's own lines stay plain text, for example `INFO: 172.18.0.1:53996 - "GET /healthz HTTP/1.1" 200 OK`. They carry the client address and path, never a body.

## The privacy rule

:::danger[No document text leaves the process]
No log line, span, span event, status message or metric attribute carries document text, model output or an exception message. Only exception **types** are recorded, because a Pydantic message can quote the model output, and a SQLAlchemy message quotes its bound parameters, which on a save are the document.
:::

| Path | How |
|---|---|
| Suggestion spans | `record_exception=False`; the status is the exception type |
| HTTP route spans from the FastAPI instrumentation | It records exceptions with message and stack trace. `ScrubbingSpanExporter` removes `exception.message` and `exception.stacktrace` and cuts the status to the type before export. |
| Uvicorn's "Exception in ASGI application" traceback | The `DropExceptionDetail` filter on `uvicorn.error` replaces the traceback with the type |
| Log formatter | `JsonFormatter` writes `error.type`, never `exc_info` text |

The tests in `server/tests/test_telemetry.py` enforce it, with no network. A FakeAI that raises with a marker in its message, a FakeAI that returns invalid JSON containing the marker, and a client message containing the marker all go through the WebSocket. A route raises with the marker too. Then:

- `test_no_document_text_or_exception_message_leaves_the_process` checks every log line, span attribute, span event, status message, resource attribute and metric.
- `test_an_unhandled_route_exception_leaks_no_message` first shows the raw route span does contain the marker, so the test cannot pass trivially. It then checks the exported span and the uvicorn log line.

The local run found the smoke test's document text in none of 95 exported spans, the server logs, the collector log or the Prometheus series. See [the evidence](/alerting-example#evidence).
