fix(collab-server): stop /metrics reporting a stale peer count for an unloaded room

Merged
PE
3mo ago
+64-2

Summary

  • peersGauge (collab_room_peers) in packages/collab-server/src/server.ts is keyed by roomId — an identifier the connecting peer picks (the websocket URL path). Every /metrics scrape re-set the gauge for each currently-loaded room, but nothing ever removed a series for a room that had since unloaded, so collab_room_peers{room="<id>"} kept reporting that room's last-known (non-zero) peer count forever, and the registry grew one label series per distinct room id ever visited over the life of a long-running server, instead of tracking only currently-loaded rooms.
  • MetricsRegistry's gauge now exposes reset() (packages/collab-server/src/metrics.ts), and the /metrics handler resets peersGauge before repopulating it from the live room list on each scrape (packages/collab-server/src/server.ts).

Evidence

RED (new test, before the fix): a room is loaded with one peer (collab_room_peers{room="ghost-room"} 1), the peer disconnects and the room idle-unloads (collab_rooms 0, room no longer in roomManager.list()), but the next /metrics scrape still contains collab_room_peers{room="ghost-room"} 1 — a stale, wrong value for a room that no longer exists.

GREEN (after the fix): the same scrape no longer mentions room="ghost-room" once the room has unloaded.

Full @ifc-lite/collab-server suite: 186/186 passing. node scripts/check-module-size.mjs: OK (exit 0), server.ts at exactly its 677-line budget.

Test plan

  • pnpm exec vitest run in packages/collab-server — 186/186 passing, including the new regression test in test/metrics.test.ts
  • node scripts/check-module-size.mjs — OK
  • Changeset added (@ifc-lite/collab-server is published)

https://claude.ai/code/session_01QPHChk3Ve9N519A4kY7436

Summary by CodeRabbit

  • Bug Fixes

    • Fixed metrics reporting so unloaded rooms no longer retain stale peer-count entries.
    • Room peer metrics now reflect only currently active rooms.
  • Tests

    • Added coverage confirming that peer metrics are removed after a room unloads.
  • Documentation

    • Documented the metrics update in the release changeset.