|
| 1 | +# @postgresai/telemetry |
| 2 | + |
| 3 | +Telemetry reporter for PostgresAI monitoring instances. A small TS+Bun |
| 4 | +service that runs on each monitoring host and posts an hourly |
| 5 | +mini-healthcheck to the platform. |
| 6 | + |
| 7 | +## What it collects |
| 8 | + |
| 9 | +Each tick gathers four signals and POSTs them to the platform: |
| 10 | + |
| 11 | +| Signal | Source | |
| 12 | +|---|---| |
| 13 | +| OOM events in the lookback window | `journalctl -k --since "<lookback>"` | |
| 14 | +| Faulty containers (exited / dead / restarting / unhealthy) | `docker ps -a --format '{{json .}}'` | |
| 15 | +| Free RAM | `MemAvailable` from `/proc/meminfo` (falls back to `MemFree`) | |
| 16 | +| Free disk | `fs.statfs` on the configured mount | |
| 17 | + |
| 18 | +The companion platform-side hypertable, RPC, alert evaluator, and |
| 19 | +dispatcher live in `postgres-ai/platform-all!365`. |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +| Var | Required | Default | |
| 24 | +|---|---|---| |
| 25 | +| `PGAI_PLATFORM_API_URL` | yes | — | |
| 26 | +| `PGAI_API_TOKEN` | yes | — | |
| 27 | +| `PGAI_MONITORING_INSTANCE_ID` | yes | — | |
| 28 | +| `PGAI_TELEMETRY_DISK_PATH` | no | `/` | |
| 29 | +| `PGAI_TELEMETRY_MEMINFO_PATH` | no | `/proc/meminfo` | |
| 30 | +| `PGAI_TELEMETRY_OOM_LOOKBACK` | no | `24 hours ago` | |
| 31 | +| `PGAI_TELEMETRY_INTERVAL_SEC` | no | `3600` (min `60`) | |
| 32 | + |
| 33 | +`PGAI_API_TOKEN` is the existing PostgresAI checkup double-base64 token. |
| 34 | + |
| 35 | +## Build and run locally |
| 36 | + |
| 37 | +```sh |
| 38 | +cd telemetry |
| 39 | +bun install --frozen-lockfile |
| 40 | +bun test |
| 41 | +bun run typecheck |
| 42 | +bun run start # actually starts reporting |
| 43 | +``` |
| 44 | + |
| 45 | +## Run in a container |
| 46 | + |
| 47 | +```sh |
| 48 | +docker build -t postgresai-telemetry telemetry |
| 49 | +docker run --rm \ |
| 50 | + -e PGAI_PLATFORM_API_URL=https://postgres.ai/api/v1 \ |
| 51 | + -e PGAI_API_TOKEN=... \ |
| 52 | + -e PGAI_MONITORING_INSTANCE_ID=... \ |
| 53 | + --read-only \ |
| 54 | + -v /proc:/host/proc:ro \ |
| 55 | + -v /:/host/disk:ro \ |
| 56 | + -v /var/run/docker.sock:/var/run/docker.sock \ |
| 57 | + postgresai-telemetry |
| 58 | +``` |
| 59 | + |
| 60 | +## Deployment requirements |
| 61 | + |
| 62 | +The agent must read host kernel logs, host memory, the host filesystem, |
| 63 | +and ask the local Docker daemon for its container list. Mount these: |
| 64 | + |
| 65 | +| Host path | Container path | Mode | |
| 66 | +|---|---|---| |
| 67 | +| `/proc` | `/host/proc` | read-only | |
| 68 | +| `/` (or data volume) | `/host/disk` | read-only | |
| 69 | +| `/var/run/docker.sock` | `/var/run/docker.sock` | read-write | |
| 70 | + |
| 71 | +## Threat model |
| 72 | + |
| 73 | +Mounting `/var/run/docker.sock` is **root-equivalent on the host**. |
| 74 | +Anyone who execs into this container — or compromises any of its |
| 75 | +dependencies — can launch privileged containers and take over the |
| 76 | +monitoring host. |
| 77 | + |
| 78 | +Recommended mitigations: |
| 79 | + |
| 80 | +- Prefer a docker-socket proxy (e.g. |
| 81 | + [`tecnativa/docker-socket-proxy`](https://github.com/Tecnativa/docker-socket-proxy)) |
| 82 | + restricted to `CONTAINERS=1` so only `GET /containers/json` is exposed. |
| 83 | +- Drop all Linux capabilities the agent doesn't need |
| 84 | + (`--cap-drop ALL --cap-add ...`). |
| 85 | +- Run as a non-root UID inside the container; the `oven/bun` base image |
| 86 | + ships a `bun` user. |
| 87 | +- `PGAI_TELEMETRY_MEMINFO_PATH` and `PGAI_TELEMETRY_DISK_PATH` are |
| 88 | + security-sensitive: an actor who can flip them can turn the heartbeat |
| 89 | + into an arbitrary-file-read primitive. Keep them under config-management |
| 90 | + control. |
| 91 | + |
| 92 | +## API contract |
| 93 | + |
| 94 | +`POST /rpc/monitoring_instance_telemetry_report` with a JSON body: |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "api_token": "<double-base64 token>", |
| 99 | + "instance_id": "<uuid>", |
| 100 | + "oom_count_24h": 0, |
| 101 | + "faulty_containers": ["cadvisor"], |
| 102 | + "free_ram_bytes": 8589934592, |
| 103 | + "free_disk_bytes": 100000000000, |
| 104 | + "metadata": { "collected_at": "2026-04-28T09:00:00.000Z" } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +All `*_bytes` fields are byte counts. `metadata` is an open-ended |
| 109 | +JSON object that today carries `collected_at` (ISO 8601 UTC). |
| 110 | + |
| 111 | +## Operational notes |
| 112 | + |
| 113 | +- **Startup tick**: the agent reports once on startup, then on each |
| 114 | + `PGAI_TELEMETRY_INTERVAL_SEC` boundary. |
| 115 | +- **Graceful shutdown**: SIGTERM / SIGINT cancel the in-flight sleep |
| 116 | + immediately. Shutdown latency is bounded by the current tick (not the |
| 117 | + interval). |
| 118 | +- **Per-collector failure isolation**: each of the four collectors logs |
| 119 | + a warning on failure and reports a safe default (`0` / `[]`) so a |
| 120 | + single broken signal doesn't silence the heartbeat. |
0 commit comments