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),
  }),
});
OptionDescription
pathScrape path. Default /metrics.
roomsServes rooms.list() at path. Requires authorize.
authorizeReturns false to answer 403. requireBearerToken fails startup on an empty token.
nextYour own onRequest, for other paths.
sampleBeforeScrapeSamples rooms before a scrape, at most every 5 seconds.
ServerHandler
Node http, ExpressmetricsHandler(metrics)
Next.js, Hono, Bun, DenofetchMetricsHandler(metrics). Never cache: set dynamic = 'force-dynamic' in Next.js.
FastifyCall 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.

RefusalLabel
Collaboration failure code, or a value in reasonsThat value
unauthorized, forbidden, permission-denied, unknown-room, rate-limitedThat reason
Unauthorized or Forbidden close event or error messageunauthorized or forbidden
Rejection without an errorpermission-denied
Anything elseother

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 as otelExporter or prometheusRegistry. sentryExporter gets nothing.
  • Hook error messages reach the console and Sentry breadcrumbs. Drop the console category in beforeBreadcrumb if they can name users or rooms.
  • For counting rules, see Rooms, people, and memory.

See also