svelte-realtime 0.4.22 → 0.4.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -5
- package/package.json +2 -1
- package/server.d.ts +35 -8
- package/server.js +12 -5
package/README.md
CHANGED
|
@@ -1219,22 +1219,51 @@ export const message = createMessage({
|
|
|
1219
1219
|
|
|
1220
1220
|
Opt-in instrumentation for RPC calls, stream subscriptions, and cron executions. Zero overhead if not called.
|
|
1221
1221
|
|
|
1222
|
+
`live.metrics(registry)` is a one-time setup call. The top of `src/hooks.ws.{js,ts}` is a natural place, since it loads once when the server boots. Pair it with the `createMetrics()` registry from `svelte-adapter-uws-extensions/prometheus`:
|
|
1223
|
+
|
|
1222
1224
|
```js
|
|
1225
|
+
// src/hooks.ws.js
|
|
1223
1226
|
import { live } from 'svelte-realtime/server';
|
|
1224
|
-
import {
|
|
1227
|
+
import { createMetrics } from 'svelte-adapter-uws-extensions/prometheus';
|
|
1228
|
+
|
|
1229
|
+
const metrics = createMetrics();
|
|
1230
|
+
|
|
1231
|
+
live.metrics({
|
|
1232
|
+
counter: ({ name, help, labelNames }) => metrics.counter(name, help, labelNames),
|
|
1233
|
+
histogram: ({ name, help, labelNames }) => metrics.histogram(name, help, labelNames),
|
|
1234
|
+
gauge: ({ name, help, labelNames }) => metrics.gauge(name, help, labelNames)
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
export { message, close, unsubscribe } from 'svelte-realtime/server';
|
|
1238
|
+
```
|
|
1225
1239
|
|
|
1226
|
-
|
|
1227
|
-
|
|
1240
|
+
Mount the metrics endpoint on your uWS app (typically in `svelte.config.js` or wherever you build the app):
|
|
1241
|
+
|
|
1242
|
+
```js
|
|
1243
|
+
app.get('/metrics', metrics.handler);
|
|
1228
1244
|
```
|
|
1229
1245
|
|
|
1230
|
-
|
|
1246
|
+
The six-line shim adapts realtime's options-object call shape to the extensions registry's positional create methods. Once a metric is registered, every increment, observation, and gauge update flows directly to the extensions registry, so the emitted Prometheus output is exactly what `metrics.serialize()` produces.
|
|
1247
|
+
|
|
1248
|
+
### Registered metrics
|
|
1249
|
+
|
|
1231
1250
|
- `svelte_realtime_rpc_total` -- RPC call count by path and status
|
|
1232
1251
|
- `svelte_realtime_rpc_duration_seconds` -- RPC latency by path
|
|
1233
1252
|
- `svelte_realtime_rpc_errors_total` -- RPC errors by path and code
|
|
1234
|
-
- `svelte_realtime_stream_subscriptions` -- active stream subscription gauge
|
|
1253
|
+
- `svelte_realtime_stream_subscriptions` -- active stream subscription gauge
|
|
1235
1254
|
- `svelte_realtime_cron_total` -- cron execution count by path and status
|
|
1236
1255
|
- `svelte_realtime_cron_errors_total` -- cron errors by path
|
|
1237
1256
|
|
|
1257
|
+
### Registry shape
|
|
1258
|
+
|
|
1259
|
+
`live.metrics()` accepts any object exposing:
|
|
1260
|
+
|
|
1261
|
+
- `counter({ name, help, labelNames }) -> { inc(labels?) }`
|
|
1262
|
+
- `histogram({ name, help, labelNames }) -> { observe(labels, valueSeconds) }`
|
|
1263
|
+
- `gauge({ name, help }) -> { inc(), dec() }`
|
|
1264
|
+
|
|
1265
|
+
If you prefer `prom-client`, wire it the same way: `counter: ({ name, help, labelNames }) => new client.Counter({ name, help, labelNames, registers: [register] })`, and likewise for `Histogram` and `Gauge`.
|
|
1266
|
+
|
|
1238
1267
|
---
|
|
1239
1268
|
|
|
1240
1269
|
## Circuit breaker
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-realtime",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.23",
|
|
4
4
|
"description": "Realtime RPC and reactive subscriptions for SvelteKit, built on svelte-adapter-uws",
|
|
5
5
|
"author": "Kevin Radziszewski",
|
|
6
6
|
"license": "MIT",
|
|
@@ -72,6 +72,7 @@
|
|
|
72
72
|
"svelte-adapter-uws": ">=0.4.12"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
+
"svelte-adapter-uws-extensions": "^0.4.2",
|
|
75
76
|
"vitest": "^4.0.18"
|
|
76
77
|
},
|
|
77
78
|
"keywords": [
|
package/server.d.ts
CHANGED
|
@@ -182,6 +182,25 @@ export interface CreateMessageOptions {
|
|
|
182
182
|
onUnhandled?(ws: WebSocket<any>, data: ArrayBuffer, platform: Platform): void;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Shape accepted by `live.metrics()`. Any object matching this contract works
|
|
187
|
+
* (hand-rolled, prom-client adapter, or the `createMetrics()` registry from
|
|
188
|
+
* `svelte-adapter-uws-extensions/prometheus` wrapped to forward options as
|
|
189
|
+
* positional args -- see the README "Prometheus metrics" section).
|
|
190
|
+
*/
|
|
191
|
+
export interface MetricsRegistry {
|
|
192
|
+
counter(opts: { name: string; help: string; labelNames?: string[] }): {
|
|
193
|
+
inc(labels?: Record<string, string | number>): void;
|
|
194
|
+
};
|
|
195
|
+
histogram(opts: { name: string; help: string; labelNames?: string[]; buckets?: number[] }): {
|
|
196
|
+
observe(labels: Record<string, string | number>, valueSeconds: number): void;
|
|
197
|
+
};
|
|
198
|
+
gauge(opts: { name: string; help: string; labelNames?: string[] }): {
|
|
199
|
+
inc(): void;
|
|
200
|
+
dec(): void;
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
185
204
|
/**
|
|
186
205
|
* Mark a function as RPC-callable over WebSocket.
|
|
187
206
|
*
|
|
@@ -561,21 +580,29 @@ export namespace live {
|
|
|
561
580
|
function webhook(topic: string, config: WebhookConfig): WebhookHandler;
|
|
562
581
|
|
|
563
582
|
/**
|
|
564
|
-
* Opt-in Prometheus metrics integration.
|
|
565
|
-
*
|
|
566
|
-
* and instruments RPC calls, stream subscriptions, and cron executions.
|
|
583
|
+
* Opt-in Prometheus metrics integration. Instruments RPC calls, stream
|
|
584
|
+
* subscriptions, and cron executions. Zero overhead if never called.
|
|
567
585
|
*
|
|
568
|
-
*
|
|
586
|
+
* Call once at server start (e.g. the top of `src/hooks.ws.{js,ts}`).
|
|
587
|
+
* See the README "Prometheus metrics" section for a working example
|
|
588
|
+
* that pairs this with `createMetrics()` from
|
|
589
|
+
* `svelte-adapter-uws-extensions/prometheus`.
|
|
569
590
|
*
|
|
570
|
-
* @param registry -
|
|
591
|
+
* @param registry - Object matching the {@link MetricsRegistry} shape
|
|
571
592
|
*
|
|
572
593
|
* @example
|
|
573
594
|
* ```js
|
|
574
|
-
* import {
|
|
575
|
-
*
|
|
595
|
+
* import { createMetrics } from 'svelte-adapter-uws-extensions/prometheus';
|
|
596
|
+
*
|
|
597
|
+
* const metrics = createMetrics();
|
|
598
|
+
* live.metrics({
|
|
599
|
+
* counter: ({ name, help, labelNames }) => metrics.counter(name, help, labelNames),
|
|
600
|
+
* histogram: ({ name, help, labelNames }) => metrics.histogram(name, help, labelNames),
|
|
601
|
+
* gauge: ({ name, help, labelNames }) => metrics.gauge(name, help, labelNames)
|
|
602
|
+
* });
|
|
576
603
|
* ```
|
|
577
604
|
*/
|
|
578
|
-
function metrics(registry:
|
|
605
|
+
function metrics(registry: MetricsRegistry): void;
|
|
579
606
|
|
|
580
607
|
/**
|
|
581
608
|
* Wrap a stream initFn call with a circuit breaker.
|
package/server.js
CHANGED
|
@@ -1338,13 +1338,20 @@ live.room = function room(config) {
|
|
|
1338
1338
|
let _metricsInstruments = null;
|
|
1339
1339
|
|
|
1340
1340
|
/**
|
|
1341
|
-
* Opt-in Prometheus metrics integration.
|
|
1342
|
-
*
|
|
1343
|
-
* and instruments RPC calls, stream subscriptions, and cron executions.
|
|
1341
|
+
* Opt-in Prometheus metrics integration. Instruments RPC calls, stream
|
|
1342
|
+
* subscriptions, and cron executions. Zero overhead if never called.
|
|
1344
1343
|
*
|
|
1345
|
-
*
|
|
1344
|
+
* Call once at server start (e.g. the top of `src/hooks.ws.{js,ts}`).
|
|
1346
1345
|
*
|
|
1347
|
-
*
|
|
1346
|
+
* The registry is any object exposing:
|
|
1347
|
+
* counter({ name, help, labelNames }) -> { inc(labels?) }
|
|
1348
|
+
* histogram({ name, help, labelNames }) -> { observe(labels, valueSeconds) }
|
|
1349
|
+
* gauge({ name, help }) -> { inc(), dec() }
|
|
1350
|
+
*
|
|
1351
|
+
* See the README "Prometheus metrics" section for a working example that
|
|
1352
|
+
* pairs this with `createMetrics()` from `svelte-adapter-uws-extensions/prometheus`.
|
|
1353
|
+
*
|
|
1354
|
+
* @param {any} registry - Object with counter, histogram, and gauge factories
|
|
1348
1355
|
*/
|
|
1349
1356
|
live.metrics = function metrics(registry) {
|
|
1350
1357
|
_metricsInstruments = {
|