airi/server/apps/api/Dockerfile
RainbowBird a0d1b2ce03
Some checks failed
CI / Type Check (push) Blocked by required conditions
CI / Detect changes (push) Waiting to run
CI / Lint (push) Blocked by required conditions
CI / Build Test (stage-web) (push) Blocked by required conditions
CI / Build Test (stage-tamagotchi) (push) Blocked by required conditions
CI / Build Test (ui-playgrounds) (push) Blocked by required conditions
CI / Build Test (stage-tamagotchi-godot) (push) Blocked by required conditions
CI / Unit Test (others) (push) Blocked by required conditions
CI / Unit Test (api-server-1) (push) Blocked by required conditions
CI / Unit Test (api-server-2) (push) Blocked by required conditions
CI / Unit Test (stage-tamagotchi) (push) Blocked by required conditions
CI / Unit Test (stage-ui-1) (push) Blocked by required conditions
CI / Unit Test (stage-ui-2) (push) Blocked by required conditions
CI / Unit Test (push) Blocked by required conditions
CI / Type Check (others) (push) Blocked by required conditions
CI / Type Check (desktop) (push) Blocked by required conditions
CI / Type Check (web) (push) Blocked by required conditions
CI / Check Provenance (push) Blocked by required conditions
Cloudflare Pages (Auth UI) / Deploy - ui-server-auth (push) Waiting to run
Cloudflare Workers / Build - docs (push) Waiting to run
Cloudflare Workers / Deploy - stage-web (push) Blocked by required conditions
Release Tamagotchi / Plan Updates (push) Waiting to run
Release Tamagotchi / Build (push) Blocked by required conditions
Release Tamagotchi / Build-1 (push) Blocked by required conditions
Release Tamagotchi / Build-2 (push) Blocked by required conditions
Release Tamagotchi / Build-3 (push) Blocked by required conditions
Release Tamagotchi / Build-4 (push) Blocked by required conditions
Release Tamagotchi / Publish Bundle (push) Blocked by required conditions
Release Tamagotchi / Promote Canary to Stable (push) Blocked by required conditions
Release Tamagotchi / Publish GitCode Release Mirror (push) Blocked by required conditions
Release Tamagotchi / Publish Distribution (push) Blocked by required conditions
Release Tamagotchi / Publish Distribution-1 (push) Blocked by required conditions
Release Tamagotchi / Publish Distribution-2 (push) Blocked by required conditions
Release Tamagotchi / Publish Distribution-3 (push) Blocked by required conditions
E2E Test - Tamagotchi (Linux) / X11 window render (Xvfb) (push) Waiting to run
E2E Test - Tamagotchi (Linux) / Wayland window render (weston headless) (push) Waiting to run
Update Nix pnpmDeps Hash / update (push) Waiting to run
Crowdin upload changed translations / upload-translations-to-crowdin (push) Has been cancelled
feat(api-server): report GenAI metrics with the OTel semantic convention names (#3450)
## Summary

The API gateway reported GenAI metrics with names that are not in the
OpenTelemetry GenAI semantic conventions. It also did not report cache
tokens, reasoning tokens, or cost. Postgres already had this data, but
Prometheus did not. This PR uses the convention names from
[`open-telemetry/semantic-conventions-genai`](https://github.com/open-telemetry/semantic-conventions-genai)
and adds the missing values. Then an OpenRouter-style usage dashboard
can use only Prometheus. Top users stay in Postgres, because `user_id`
is a high-cardinality label.

The GenAI constants now live only in `@proj-airi/trace-kit`. trace-kit
already had 17 hand-copied GenAI attribute names. The GenAI conventions
moved to a new repository that has no release and no npm package. The
GenAI exports of `@opentelemetry/semantic-conventions` are deprecated
and use the old names. So a script in trace-kit generates
`src/conventions/semconv.ts` from a pinned commit (`e6658f1`,
2026-11-06) of the registry YAML. The 17 old copies have the same
values. An upgrade changes the commit and runs `pnpm -F
@proj-airi/trace-kit generate:semconv`. The diff of the generated file
then shows each renamed name. The API imports the names from the new
`@proj-airi/trace-kit/semconv` entry. That entry has only constants, so
the server does not load the trace runtime.

The dashboards change in a paired PR. Apply it to Grafana after this PR
deploys:

- [airi-deploy#5](https://github.com/proj-airi/airi-deploy/pull/5)
feat(grafana): read the GenAI semantic convention metrics and add spend
panels · 2026-11-08 · @luoling8192

## Change map

| Metric or attribute | Before | After | Description |
| --- | --- | --- | --- |
| Chat and Responses duration | `gen_ai.client.operation.duration` |
`gen_ai.client.inference.duration` | The conventions say that inference
must not use `operation.duration` |
| Request count | `gen_ai.client.operation.count` | Removed | Use the
`_count` series of the duration histograms |
| Input and output tokens | `gen_ai.client.token.usage.input/output` |
`gen_ai.client.inference.usage.input_tokens/output_tokens` | Convention
names, with `gen_ai.token.modality=unknown` |
| Cache and reasoning tokens | Not reported |
`gen_ai.client.inference.usage.cache_read.input_tokens`,
`cache_write.input_tokens`, `reasoning.output_tokens` | Values from
`extractUsageFromBody` |
| First chunk latency | `gen_ai.client.first_token.duration` |
`gen_ai.client.inference.time_to_first_chunk` | Chat, Responses, and the
TTS WebSocket |
| Cost | Not reported | `airi.gen_ai.client.cost` (USD) | The
conventions have no cost metric. The value is the upstream cost that the
provider reports, not the Flux charge |
| Provider label | `provider` | `gen_ai.provider.name` | Gateway
`airi.gen_ai.gateway.*` counters keep `provider` |
| Other labels | None | `gen_ai.response.model`, and `error.type` when
the status is 400 or more | `http.response.status_code` stays |
| TTS duration | `gen_ai.client.operation.duration` | Same | TTS is not
an inference operation |
| GenAI constants | Strings in `utils/observability.ts`, and 17 hand
copies in trace-kit | Generated once in trace-kit, imported from
`@proj-airi/trace-kit/semconv` | `error.type` comes from
`@opentelemetry/semantic-conventions`. The trace-kit test that compared
copies with the deprecated npm exports is removed |
| Span attributes | Input and output tokens | Also
`gen_ai.usage.cache_read.input_tokens`, `cache_write.input_tokens`,
`reasoning.output_tokens` | |

Token and cost counters record only positive values, as the conventions
require. A missing or zero value records nothing.

## Boundaries and risks

| Invariant or boundary | Failure mode | Protection | Evidence or gap |
| --- | --- | --- | --- |
| Dashboards in `proj-airi/airi-deploy` read the old names | Panels go
empty after deploy |
[airi-deploy#5](https://github.com/proj-airi/airi-deploy/pull/5) moves
them | Apply that PR to Grafana right after this deploy |
| Grafana alert rules read the old names | Alerts stop firing | None. No
repository has the rules | Unverified. Check the rules in Grafana |
| Generated constants match the pinned registry | A hand edit or a stale
file gives wrong names | The file is generated, and the generator throws
on duplicate names | `render.test.ts`. Regenerating in rc gave no diff |
| trace-kit consumers | Removing `SEMCONV_VERSION` or changing the root
exports breaks a consumer | Nothing read `SEMCONV_VERSION`. The 17 names
keep their values | Typecheck of trace-kit dependents in rc, see
Verification |
| Docker build of the API | The image misses trace-kit | Both API
Dockerfiles copy and build `packages/stream-kit` and
`packages/trace-kit` | No Docker build ran. Checked by reading the
Dockerfiles |
| Cost matches the provider receipt | Cost is recorded as the Flux
charge, or is lost when the wallet cannot pay | `costUsd` comes from
`price.costReceipt.usage.costUsd`, before settlement | `route.test.ts`:
a drained wallet still records `0.038` USD |
| Label cardinality | High-cardinality labels | Only model, provider,
operation, status, and error type. No user ID | Code review |
| The conventions are at `development` stability | Names can change
again | None | Accepted risk |

## Verification

All checks ran in an rc Workspace, on a clean clone with this diff
applied.


- `pnpm -F @proj-airi/api-server typecheck`: passed.
- `pnpm -F @proj-airi/api-server exec vitest run`: 88 files passed and 2
skipped. 975 tests passed and 4 skipped.
- `pnpm exec eslint <changed files>`: 0 errors. The warnings that remain
were there before this change.
- `pnpm install --frozen-lockfile` (full workspace): passed. The
lockfile change is the API link to trace-kit and the trace-kit
devDependencies.
- `pnpm -F @proj-airi/trace-kit build`: passed, with `dist/semconv.mjs`.
- `turbo run test:run` for trace-kit, ui-trace, and testing-audio: 28
tasks passed. trace-kit 73 tests, ui-trace 17 tests.
- `turbo run typecheck --filter=...@proj-airi/trace-kit
--filter=@proj-airi/api-server --continue`: 10 of 13 passed.
`airi-plugin-game-chess`, `stage-tamagotchi`, and
`stage-tamagotchi-kirie` failed on errors in `electron-vueuse` and Vue
pages. The same three fail on the merge base `b51672dc8` in the same
Workspace.
- Full API suite on the final head: 974 passed, 1 failed. The failure is
a 5 s timeout in `mock-db.test.ts` under node load. The file passed
alone. Earlier runs had the same timeouts in PGlite tests.
- New tests in `telemetry.test.ts` cover each new counter, its
attributes, the skip of zero values, and `error.type`.

I did not check the exported names on a live Prometheus. With the OTLP
translation, I expect `airi_gen_ai_client_cost_total` and
`gen_ai_client_inference_usage_input_tokens_total`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 19:32:21 +00:00

39 lines
1.5 KiB
Docker

FROM node:24.22.1-alpine
WORKDIR /app
RUN addgroup -S airi && adduser -S airi -G airi
RUN corepack enable
ENV pnpm_config_store_dir=/root/.pnpm-store
COPY pnpm-lock.yaml pnpm-workspace.yaml package.json tsconfig.json ./
COPY patches/ ./patches/
# Manifests come first, so a source change does not install dependencies again.
COPY server/apps/api/package.json server/apps/api/package.json
COPY server/packages/auth-shared/package.json server/packages/auth-shared/package.json
COPY server/packages/http-error-shared/package.json server/packages/http-error-shared/package.json
COPY server/packages/server-sdk-shared/package.json server/packages/server-sdk-shared/package.json
COPY packages/stream-kit/package.json packages/stream-kit/package.json
COPY packages/trace-kit/package.json packages/trace-kit/package.json
RUN --mount=type=cache,id=pnpm-store,target=/root/.pnpm-store \
pnpm install --frozen-lockfile --ignore-scripts
COPY server/apps/api server/apps/api
COPY server/packages/auth-shared server/packages/auth-shared
COPY server/packages/http-error-shared server/packages/http-error-shared
COPY server/packages/server-sdk-shared server/packages/server-sdk-shared
COPY packages/stream-kit packages/stream-kit
COPY packages/trace-kit packages/trace-kit
RUN pnpm -F @proj-airi/server-sdk-shared run build
RUN pnpm -F @proj-airi/stream-kit run build && pnpm -F @proj-airi/trace-kit run build
RUN pnpm -F @proj-airi/api-server run build
EXPOSE 3000
USER airi
CMD ["pnpm", "-F", "@proj-airi/api-server", "start"]