Collaboration servers
Record rooms, connections, people, memory, and refusals on a Hocuspocus or custom Yjs collaboration server, and serve them to Prometheus or OpenTelemetry.
@docx-editor.dev/telemetry is available in version 2.29.0 and later.Server metrics record rooms, connections, people, memory, load and store times, and refusals, without room names.
Instrument Hocuspocus
Wrap your configuration with withCollaborationTelemetry. It requires @hocuspocus/server 4.6 or later:
import { Server } from '@hocuspocus/server';
import { withCollaborationTelemetry } from '@docx-editor.dev/telemetry/hocuspocus';
import { metricsHandler, prometheusRegistry } from '@docx-editor.dev/telemetry/prometheus';
const metrics = prometheusRegistry();
const { config, rooms } = withCollaborationTelemetry(
{ port: 1234, onAuthenticate, onLoadDocument, beforeHandleMessage, onStoreDocument },
{ exporters: [metrics] }
);
const server = new Server(config);
app.get('/metrics', metricsHandler(metrics));
app.get('/admin/rooms', requireAdmin, (_request, response) => response.json(rooms.list()));Hooks keep their arguments, return values, and errors. Load and store durations include all hooks and extensions in the wrapped configuration. Hocuspocus flushes telemetry at shutdown.
Serve metrics securely
Without a web framework, use serveMetrics as the Hocuspocus onRequest:
import { requireBearerToken, serveMetrics } from '@docx-editor.dev/telemetry/hocuspocus';
const server = new Server({
...config,
onRequest: serveMetrics(metrics, {
rooms: { telemetry: rooms, path: '/rooms' },
authorize: requireBearerToken(process.env.METRICS_TOKEN),
}),
});| Option | Description |
|---|---|
path | Scrape path. Default /metrics. |
rooms | Serves rooms.list() at path. Requires authorize. |
authorize | Returns false to answer 403. requireBearerToken fails startup on an empty token. |
next | Your own onRequest, for other paths. |
sampleBeforeScrape | Samples rooms before a scrape, at most every 5 seconds. |
| Server | Handler |
|---|---|
Node http, Express | metricsHandler(metrics) |
| Next.js, Hono, Bun, Deno | fetchMetricsHandler(metrics). Never cache: set dynamic = 'force-dynamic' in Next.js. |
| Fastify | Call reply.hijack(), then metricsHandler(metrics)(request.raw, reply.raw). |
Pass merge: [register] to either handler to include your prom-client metrics.
Instrument another Yjs server
Call roomTelemetry from your room lifecycle:
import { roomTelemetry } from '@docx-editor.dev/telemetry/server';
const rooms = roomTelemetry({ exporters: [metrics] });
const room = rooms.open(ydoc, { name: roomName, awareness });
// Load through the handle so saved state does not count as fresh edits.
await room.time('load', () => loadRoom(ydoc));
room.connected();
room.disconnected(); // Connection closed.
rooms.rejected(error); // Admission refused.
room.close(); // Room unloaded.Classify refusals
The label comes from the error's reason, then its code. Refusals in onConnect, onAuthenticate, and onTokenSync count as admission refusals.
| Refusal | Label |
|---|---|
Collaboration failure code, or a value in reasons | That value |
unauthorized, forbidden, permission-denied, unknown-room, rate-limited | That reason |
Unauthorized or Forbidden close event or error message | unauthorized or forbidden |
| Rejection without an error | permission-denied |
| Anything else | other |
Map free-text errors with reasonOf: (error) => string | undefined.
Limits
- Only hooks and extensions in the wrapped configuration are observed, not ones added later.
- Room telemetry needs an exporter with
metric, such asotelExporterorprometheusRegistry.sentryExportergets nothing. - Hook error messages reach the console and Sentry breadcrumbs. Drop the
consolecategory inbeforeBreadcrumbif they can name users or rooms. - For counting rules, see Rooms, people, and memory.
See also
Server agents
Record anonymous diagnostics from agents that edit documents through the editing API, on a server or in the browser.
Rooms, people, and memory
How collaboration server telemetry counts people and memory per room, finds large rooms without room names in metrics, and labels metrics by room.