@docx-editor.dev/telemetry/prometheus
Prometheus: an in-process registry in the text exposition format, and /metrics handlers for Node-style servers ([metricsHandler](metricsHandler)) and Fetch-style servers ([fetchMetricsHandler](fetchMetricsHandler)). No dependency on prom-client; a prom-client registry merges into the same endpoint.
Functions (3)
fetchMetricsHandlerfunctionSource ↗
A Fetch-style handler that returns a Response, for Next.js route handlers, Hono, Bun, Deno, and workers. It ignores its argument, so a Hono context works as well as a Request.
declare function fetchMetricsHandler(registry: PrometheusTextSource, options?: MetricsHandlerOptions): (request?: unknown) => Promise<Response>;```ts
// app/metrics/route.ts
export const GET = fetchMetricsHandler(metrics);
```metricsHandlerfunctionSource ↗
A Node-style handler, (request, response), for Node http, Express, and Koa (ctx.res). For Fastify, hijack the reply and pass reply.raw. For Fetch-style servers, use [fetchMetricsHandler](fetchMetricsHandler). Protect the route as you protect other operational endpoints.
declare function metricsHandler(registry: PrometheusTextSource, options?: MetricsHandlerOptions): (request: unknown, response: NodeResponseLike) => Promise<void>;```ts
app.get('/metrics', metricsHandler(metrics, { merge: [promClient.register] })); // Express
fastify.get('/metrics', (request, reply) => {
reply.hijack();
return metricsHandler(metrics)(request.raw, reply.raw);
});
```prometheusRegistryfunctionSource ↗
Collect metrics in memory and render them for Prometheus to scrape.
declare function prometheusRegistry(): PrometheusRegistry;```ts
import { prometheusRegistry, metricsHandler } from '@docx-editor.dev/telemetry/prometheus';
const metrics = prometheusRegistry();
// Pass `metrics` as an exporter, then serve it:
app.get('/metrics', metricsHandler(metrics));
```Interfaces (5)
MetricsHandlerOptionsinterfaceSource ↗
Options for [metricsHandler](metricsHandler) and [fetchMetricsHandler](fetchMetricsHandler).
interface MetricsHandlerOptions| Member | Type | Summary |
|---|---|---|
| merge? | readonly PrometheusTextSource[] | More sources rendered into the same response, such as your `prom-client` registry: `merge: [register]`. |
NodeResponseLikeinterfaceSource ↗
The part of a Node ServerResponse the handler writes to.
interface NodeResponseLike| Member | Type | Summary |
|---|---|---|
| end | | |
| setHeader | | |
| statusCode? | number |
PrometheusRegistryinterfaceSource ↗
The exporter returned by [prometheusRegistry](prometheusRegistry).
interface PrometheusRegistry extends TelemetryExporter, PrometheusTextSource| Member | Type | Summary |
|---|---|---|
| metrics | | Every recorded metric in the Prometheus text format. |
| reset | | Forget every recorded value. |
PrometheusTextSourceinterfaceSource ↗
Anything that renders Prometheus text, such as a prom-client Registry.
interface PrometheusTextSource| Member | Type | Summary |
|---|---|---|
| metrics | |
TelemetryExporterinterfaceSource ↗
Where telemetry goes. Implement event, metric, or both.
An exporter never throws into the editor or the server: a failure is caught and reported once through onExporterError.
interface TelemetryExporter```ts
const toMyLogger: TelemetryExporter = {
name: 'my-logger',
event: (event) => myLogger.warn(event.kind, { code: 'code' in event ? event.code : undefined }),
};
```| Member | Type | Summary |
|---|---|---|
| attach | | Called once when a telemetry client or room server takes the exporter. An exporter that holds events, such as a batching one, reads `consented()` before it sends, so events queued before consent was withdrawn are discarded, not sent. |
| declare | | Start a series at zero before its first measurement. A scraper that sees a counter first at 1 cannot count that first increase, so the room server declares its known series when it starts. Optional: an exporter without it creates each series at its first measurement. |
| event | | |
| flush | | Send anything buffered. Called by `flush()` and when a page is hidden. |
| forget | | Drop a series that no longer exists, such as a closed room's per-room gauge. Optional: an exporter without it keeps the series until it expires on its own. |
| metric | | |
| name | string | Shown in `onExporterError` reports. |
| shutdown | | Flush and release resources. Called by `shutdown()`. |
Variables (1)
PROMETHEUS_CONTENT_TYPEconstSource ↗
The Prometheus text exposition content type.
PROMETHEUS_CONTENT_TYPE = "text/plain; version=0.0.4; charset=utf-8"