@dudousxd/nestjs-diagnostics-telescope 0.2.0
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/CHANGELOG.md +26 -0
- package/README.md +63 -0
- package/dist/diagnostic.watcher.d.ts +58 -0
- package/dist/diagnostic.watcher.d.ts.map +1 -0
- package/dist/diagnostic.watcher.js +101 -0
- package/dist/diagnostic.watcher.js.map +1 -0
- package/dist/diagnostics-telescope.extension.d.ts +28 -0
- package/dist/diagnostics-telescope.extension.d.ts.map +1 -0
- package/dist/diagnostics-telescope.extension.js +113 -0
- package/dist/diagnostics-telescope.extension.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @dudousxd/nestjs-diagnostics-telescope
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`fce932d`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/fce932df74ba25d47d9b41b3dc03b61674bd976f) - Initial release.
|
|
8
|
+
|
|
9
|
+
`@dudousxd/nestjs-diagnostics` — a standard convention for `@dudousxd/nestjs-*`
|
|
10
|
+
libraries to emit observability events over `node:diagnostics_channel`. Channels
|
|
11
|
+
are named `aviary:<lib>:<event>`; `emit(lib, event, payload, opts?)` builds a
|
|
12
|
+
`DiagnosticEvent` envelope (`ts`, `lib`, `event`, optional `traceId`, `payload`)
|
|
13
|
+
and publishes only when the channel has subscribers. A process-wide registry
|
|
14
|
+
(`registeredChannels()` + `onChannelRegistered()`) makes every channel
|
|
15
|
+
discoverable, since `diagnostics_channel` has no wildcard subscription. An
|
|
16
|
+
optional context accessor (`setContextAccessor`) auto-fills `traceId`.
|
|
17
|
+
|
|
18
|
+
`@dudousxd/nestjs-diagnostics-telescope` — a `nestjs-telescope` extension whose
|
|
19
|
+
single generic `DiagnosticWatcher` subscribes to every registered channel
|
|
20
|
+
(current + future) and records each event as a `diagnostic` entry, with a
|
|
21
|
+
"Diagnostics" dashboard page.
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [[`fce932d`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/fce932df74ba25d47d9b41b3dc03b61674bd976f)]:
|
|
26
|
+
- @dudousxd/nestjs-diagnostics@0.2.0
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @dudousxd/nestjs-diagnostics-telescope
|
|
2
|
+
|
|
3
|
+
A [nestjs-telescope](https://www.npmjs.com/package/@dudousxd/nestjs-telescope)
|
|
4
|
+
extension for [`@dudousxd/nestjs-diagnostics`](https://www.npmjs.com/package/@dudousxd/nestjs-diagnostics).
|
|
5
|
+
ONE generic watcher records **every** `aviary:<lib>:<event>` diagnostics event —
|
|
6
|
+
from any `@dudousxd/nestjs-*` library — as a `diagnostic` entry in the Telescope
|
|
7
|
+
dashboard. No per-library watcher needed.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @dudousxd/nestjs-diagnostics-telescope
|
|
13
|
+
# peers: @dudousxd/nestjs-telescope, plus @dudousxd/nestjs-diagnostics on the producers
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { TelescopeModule } from '@dudousxd/nestjs-telescope';
|
|
20
|
+
import { nestjsDiagnosticsTelescope } from '@dudousxd/nestjs-diagnostics-telescope';
|
|
21
|
+
|
|
22
|
+
TelescopeModule.forRoot({
|
|
23
|
+
extensions: [nestjsDiagnosticsTelescope()],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then anywhere a library emits:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { emit } from '@dudousxd/nestjs-diagnostics';
|
|
31
|
+
|
|
32
|
+
emit('billing', 'invoice-paid', { invoiceId: 'inv_123', amount: 4200 });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
…a `diagnostic` entry lands in Telescope with `content` = the payload, the
|
|
36
|
+
envelope's `traceId`, and tags `lib:billing` / `event:invoice-paid`.
|
|
37
|
+
|
|
38
|
+
## How the generic watcher auto-subscribes
|
|
39
|
+
|
|
40
|
+
`node:diagnostics_channel` has no wildcard — you can only subscribe by exact
|
|
41
|
+
channel name. So on `register` the `DiagnosticWatcher`:
|
|
42
|
+
|
|
43
|
+
1. subscribes to every channel already in the diagnostics registry
|
|
44
|
+
(`registeredChannels()`), and
|
|
45
|
+
2. registers an `onChannelRegistered` callback, so any channel that appears later
|
|
46
|
+
(a library's first `emit('newlib', …)`) is subscribed to automatically.
|
|
47
|
+
|
|
48
|
+
Subscribing also flips each producer's `channel.hasSubscribers` to `true`, which
|
|
49
|
+
is exactly what makes `emit()` start building and publishing envelopes (zero
|
|
50
|
+
overhead when nobody is listening).
|
|
51
|
+
|
|
52
|
+
## What it contributes
|
|
53
|
+
|
|
54
|
+
| Hook | Contribution |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
| `watchers` | one `DiagnosticWatcher` covering all channels (current + future) |
|
|
57
|
+
| `entryTypes` | the navigable `diagnostic` type (sky dot) |
|
|
58
|
+
| `dashboards` | a "Diagnostics" page — busiest `lib:event` pairs + a recent-events table |
|
|
59
|
+
| `dataProviders` | `diagnostics.topEvents`, `diagnostics.recentEvents` |
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type DiagnosticEvent } from '@dudousxd/nestjs-diagnostics';
|
|
2
|
+
import type { RecordInput, Watcher, WatcherContext } from '@dudousxd/nestjs-telescope';
|
|
3
|
+
/** Telescope entry `type` produced by this watcher. */
|
|
4
|
+
export declare const DIAGNOSTIC_ENTRY_TYPE = "diagnostic";
|
|
5
|
+
/**
|
|
6
|
+
* What a single recorded diagnostic entry looks like in the Telescope dashboard.
|
|
7
|
+
* Mirrors the {@link DiagnosticEvent} envelope, with the library-defined data
|
|
8
|
+
* preserved verbatim under `payload`.
|
|
9
|
+
*/
|
|
10
|
+
export interface DiagnosticEntryContent {
|
|
11
|
+
/** The emitting library, e.g. `'billing'`. */
|
|
12
|
+
lib: string;
|
|
13
|
+
/** The event within that library, e.g. `'invoice-paid'`. */
|
|
14
|
+
event: string;
|
|
15
|
+
/** Epoch millis the producer stamped the event with. */
|
|
16
|
+
ts: number;
|
|
17
|
+
/** The trace id the producer resolved, or `null` when none. */
|
|
18
|
+
traceId: string | null;
|
|
19
|
+
/** The library-defined payload, recorded as-is. */
|
|
20
|
+
payload: unknown;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The ONE generic nestjs-telescope watcher behind
|
|
24
|
+
* `@dudousxd/nestjs-diagnostics-telescope`. It records every event any
|
|
25
|
+
* `@dudousxd/nestjs-*` library emits through `@dudousxd/nestjs-diagnostics` —
|
|
26
|
+
* one `diagnostic` entry per `aviary:<lib>:<event>` publish — without needing a
|
|
27
|
+
* bespoke watcher per library.
|
|
28
|
+
*
|
|
29
|
+
* ## How it auto-subscribes to current + future channels
|
|
30
|
+
* `node:diagnostics_channel` has no wildcard, so on `register` the watcher:
|
|
31
|
+
* 1. subscribes to every channel already in the diagnostics
|
|
32
|
+
* {@link registeredChannels registry}, and
|
|
33
|
+
* 2. registers an {@link onChannelRegistered} callback so any channel that
|
|
34
|
+
* appears later (a library's first `emit('newlib', …)`) is subscribed too.
|
|
35
|
+
*
|
|
36
|
+
* Subscribing also flips each producer's `channel.hasSubscribers` to `true`,
|
|
37
|
+
* which is what makes `emit()` start building + publishing envelopes at all
|
|
38
|
+
* (zero-overhead when nobody listens).
|
|
39
|
+
*/
|
|
40
|
+
export declare class DiagnosticWatcher implements Watcher {
|
|
41
|
+
readonly type = "diagnostic";
|
|
42
|
+
private registered;
|
|
43
|
+
private offChannelRegistered;
|
|
44
|
+
/** name → the subscribe listener we attached, so cleanup can detach exactly. */
|
|
45
|
+
private readonly subscriptions;
|
|
46
|
+
register(ctx: WatcherContext): void;
|
|
47
|
+
/** Unsubscribe from every channel and stop watching for new ones. */
|
|
48
|
+
cleanup(): void;
|
|
49
|
+
/** Subscribe once to `name`, recording each publish as a `diagnostic` entry. */
|
|
50
|
+
private subscribe;
|
|
51
|
+
/** Validate + record, swallowing any failure so a producer can never break. */
|
|
52
|
+
private safeRecord;
|
|
53
|
+
}
|
|
54
|
+
/** Map a {@link DiagnosticEvent} envelope to a Telescope `RecordInput`. */
|
|
55
|
+
export declare function buildDiagnosticEntry(msg: DiagnosticEvent): RecordInput<DiagnosticEntryContent>;
|
|
56
|
+
/** Strict structural validation of a diagnostics envelope. */
|
|
57
|
+
export declare function isDiagnosticEvent(msg: unknown): msg is DiagnosticEvent;
|
|
58
|
+
//# sourceMappingURL=diagnostic.watcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostic.watcher.d.ts","sourceRoot":"","sources":["../src/diagnostic.watcher.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,eAAe,EAGrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAEvF,uDAAuD;AACvD,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,sBAAsB;IACrC,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,4DAA4D;IAC5D,KAAK,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,EAAE,EAAE,MAAM,CAAC;IACX,+DAA+D;IAC/D,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,mDAAmD;IACnD,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,iBAAkB,YAAW,OAAO;IAC/C,QAAQ,CAAC,IAAI,gBAAyB;IACtC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,oBAAoB,CAA6B;IACzD,gFAAgF;IAChF,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA6C;IAE3E,QAAQ,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI;IAQnC,qEAAqE;IACrE,OAAO,IAAI,IAAI;IAUf,gFAAgF;IAChF,OAAO,CAAC,SAAS;IAQjB,+EAA+E;IAC/E,OAAO,CAAC,UAAU;CASnB;AAED,2EAA2E;AAC3E,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,eAAe,GAAG,WAAW,CAAC,sBAAsB,CAAC,CAmB9F;AAED,8DAA8D;AAC9D,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,eAAe,CAUtE"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import diagnostics_channel from 'node:diagnostics_channel';
|
|
2
|
+
import { onChannelRegistered, registeredChannels, } from '@dudousxd/nestjs-diagnostics';
|
|
3
|
+
/** Telescope entry `type` produced by this watcher. */
|
|
4
|
+
export const DIAGNOSTIC_ENTRY_TYPE = 'diagnostic';
|
|
5
|
+
/**
|
|
6
|
+
* The ONE generic nestjs-telescope watcher behind
|
|
7
|
+
* `@dudousxd/nestjs-diagnostics-telescope`. It records every event any
|
|
8
|
+
* `@dudousxd/nestjs-*` library emits through `@dudousxd/nestjs-diagnostics` —
|
|
9
|
+
* one `diagnostic` entry per `aviary:<lib>:<event>` publish — without needing a
|
|
10
|
+
* bespoke watcher per library.
|
|
11
|
+
*
|
|
12
|
+
* ## How it auto-subscribes to current + future channels
|
|
13
|
+
* `node:diagnostics_channel` has no wildcard, so on `register` the watcher:
|
|
14
|
+
* 1. subscribes to every channel already in the diagnostics
|
|
15
|
+
* {@link registeredChannels registry}, and
|
|
16
|
+
* 2. registers an {@link onChannelRegistered} callback so any channel that
|
|
17
|
+
* appears later (a library's first `emit('newlib', …)`) is subscribed too.
|
|
18
|
+
*
|
|
19
|
+
* Subscribing also flips each producer's `channel.hasSubscribers` to `true`,
|
|
20
|
+
* which is what makes `emit()` start building + publishing envelopes at all
|
|
21
|
+
* (zero-overhead when nobody listens).
|
|
22
|
+
*/
|
|
23
|
+
export class DiagnosticWatcher {
|
|
24
|
+
type = DIAGNOSTIC_ENTRY_TYPE;
|
|
25
|
+
registered = false;
|
|
26
|
+
offChannelRegistered = null;
|
|
27
|
+
/** name → the subscribe listener we attached, so cleanup can detach exactly. */
|
|
28
|
+
subscriptions = new Map();
|
|
29
|
+
register(ctx) {
|
|
30
|
+
if (this.registered)
|
|
31
|
+
return;
|
|
32
|
+
this.registered = true;
|
|
33
|
+
for (const name of registeredChannels())
|
|
34
|
+
this.subscribe(ctx, name);
|
|
35
|
+
this.offChannelRegistered = onChannelRegistered((name) => this.subscribe(ctx, name));
|
|
36
|
+
}
|
|
37
|
+
/** Unsubscribe from every channel and stop watching for new ones. */
|
|
38
|
+
cleanup() {
|
|
39
|
+
this.offChannelRegistered?.();
|
|
40
|
+
this.offChannelRegistered = null;
|
|
41
|
+
for (const [name, listener] of this.subscriptions) {
|
|
42
|
+
diagnostics_channel.channel(name).unsubscribe(listener);
|
|
43
|
+
}
|
|
44
|
+
this.subscriptions.clear();
|
|
45
|
+
this.registered = false;
|
|
46
|
+
}
|
|
47
|
+
/** Subscribe once to `name`, recording each publish as a `diagnostic` entry. */
|
|
48
|
+
subscribe(ctx, name) {
|
|
49
|
+
if (this.subscriptions.has(name))
|
|
50
|
+
return;
|
|
51
|
+
const listener = (msg) => this.safeRecord(ctx, msg);
|
|
52
|
+
this.subscriptions.set(name, listener);
|
|
53
|
+
const channel = diagnostics_channel.channel(name);
|
|
54
|
+
channel.subscribe(listener);
|
|
55
|
+
}
|
|
56
|
+
/** Validate + record, swallowing any failure so a producer can never break. */
|
|
57
|
+
safeRecord(ctx, msg) {
|
|
58
|
+
try {
|
|
59
|
+
if (!isDiagnosticEvent(msg))
|
|
60
|
+
return;
|
|
61
|
+
ctx.record(buildDiagnosticEntry(msg));
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
// NOT rethrown — telescope must never break an emitting code path.
|
|
65
|
+
console.error('DiagnosticWatcher: failed to record diagnostic event:', err);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Map a {@link DiagnosticEvent} envelope to a Telescope `RecordInput`. */
|
|
70
|
+
export function buildDiagnosticEntry(msg) {
|
|
71
|
+
const content = {
|
|
72
|
+
lib: msg.lib,
|
|
73
|
+
event: msg.event,
|
|
74
|
+
ts: msg.ts,
|
|
75
|
+
traceId: msg.traceId ?? null,
|
|
76
|
+
payload: msg.payload,
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
type: DIAGNOSTIC_ENTRY_TYPE,
|
|
80
|
+
// Group by lib + event so the dashboard can roll up "billing:invoice-paid".
|
|
81
|
+
familyHash: `${msg.lib}:${msg.event}`,
|
|
82
|
+
tags: [
|
|
83
|
+
`lib:${msg.lib}`,
|
|
84
|
+
`event:${msg.event}`,
|
|
85
|
+
...(msg.traceId ? [`trace:${msg.traceId}`] : []),
|
|
86
|
+
],
|
|
87
|
+
content,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** Strict structural validation of a diagnostics envelope. */
|
|
91
|
+
export function isDiagnosticEvent(msg) {
|
|
92
|
+
if (typeof msg !== 'object' || msg === null)
|
|
93
|
+
return false;
|
|
94
|
+
const m = msg;
|
|
95
|
+
return (typeof m.ts === 'number' &&
|
|
96
|
+
typeof m.lib === 'string' &&
|
|
97
|
+
typeof m.event === 'string' &&
|
|
98
|
+
'payload' in m &&
|
|
99
|
+
(m.traceId === undefined || typeof m.traceId === 'string'));
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=diagnostic.watcher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E;QACrC,IAAI,EAAE;YACJ,OAAO,GAAG,CAAC,GAAG,EAAE;YAChB,SAAS,GAAG,CAAC,KAAK,EAAE;YACpB,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SACjD;QACD,OAAO;KACR,CAAC;AACJ,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,iBAAiB,CAAC,GAAY;IAC5C,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC1D,MAAM,CAAC,GAAG,GAA8B,CAAC;IACzC,OAAO,CACL,OAAO,CAAC,CAAC,EAAE,KAAK,QAAQ;QACxB,OAAO,CAAC,CAAC,GAAG,KAAK,QAAQ;QACzB,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ;QAC3B,SAAS,IAAI,CAAC;QACd,CAAC,CAAC,CAAC,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ,CAAC,CAC3D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type TelescopeExtension } from '@dudousxd/nestjs-telescope';
|
|
2
|
+
/** Options for {@link nestjsDiagnosticsTelescope}. */
|
|
3
|
+
export interface DiagnosticsTelescopeOptions {
|
|
4
|
+
/** How many event kinds to surface in the top-N panel. Default 10. */
|
|
5
|
+
topEventsLimit?: number;
|
|
6
|
+
/** How many recent events to list in the table panel. Default 50. */
|
|
7
|
+
recentLimit?: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A `@dudousxd/nestjs-telescope` extension that records every diagnostics event
|
|
11
|
+
* any `@dudousxd/nestjs-*` library emits through `@dudousxd/nestjs-diagnostics`
|
|
12
|
+
* — over the `aviary:<lib>:<event>` channels — as one `diagnostic` entry each.
|
|
13
|
+
*
|
|
14
|
+
* It wires four hooks:
|
|
15
|
+
* - `watchers` — ONE generic {@link DiagnosticWatcher} that subscribes to
|
|
16
|
+
* every registered diagnostics channel (current + future).
|
|
17
|
+
* - `entryTypes` — registers the navigable `diagnostic` type (sky dot).
|
|
18
|
+
* - `dashboards` — a "Diagnostics" page: a top-N of the busiest `lib:event`
|
|
19
|
+
* pairs and a table of the most recent events.
|
|
20
|
+
* - `dataProviders`— the server-side queries the two panels bind to.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* TelescopeModule.forRoot({ extensions: [nestjsDiagnosticsTelescope()] });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function nestjsDiagnosticsTelescope(options?: DiagnosticsTelescopeOptions): TelescopeExtension;
|
|
27
|
+
export default nestjsDiagnosticsTelescope;
|
|
28
|
+
//# sourceMappingURL=diagnostics-telescope.extension.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics-telescope.extension.d.ts","sourceRoot":"","sources":["../src/diagnostics-telescope.extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,kBAAkB,EAExB,MAAM,4BAA4B,CAAC;AAWpC,sDAAsD;AACtD,MAAM,WAAW,2BAA2B;IAC1C,sEAAsE;IACtE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qEAAqE;IACrE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,GAAE,2BAAgC,GACxC,kBAAkB,CAmFpB;AAaD,eAAe,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { TELESCOPE_STORAGE, } from '@dudousxd/nestjs-telescope';
|
|
2
|
+
import { DIAGNOSTIC_ENTRY_TYPE, DiagnosticWatcher, } from './diagnostic.watcher.js';
|
|
3
|
+
/** Provider names the dashboard panels bind to (namespaced to avoid collisions). */
|
|
4
|
+
const TOP_EVENTS_PROVIDER = 'diagnostics.topEvents';
|
|
5
|
+
const RECENT_EVENTS_PROVIDER = 'diagnostics.recentEvents';
|
|
6
|
+
/**
|
|
7
|
+
* A `@dudousxd/nestjs-telescope` extension that records every diagnostics event
|
|
8
|
+
* any `@dudousxd/nestjs-*` library emits through `@dudousxd/nestjs-diagnostics`
|
|
9
|
+
* — over the `aviary:<lib>:<event>` channels — as one `diagnostic` entry each.
|
|
10
|
+
*
|
|
11
|
+
* It wires four hooks:
|
|
12
|
+
* - `watchers` — ONE generic {@link DiagnosticWatcher} that subscribes to
|
|
13
|
+
* every registered diagnostics channel (current + future).
|
|
14
|
+
* - `entryTypes` — registers the navigable `diagnostic` type (sky dot).
|
|
15
|
+
* - `dashboards` — a "Diagnostics" page: a top-N of the busiest `lib:event`
|
|
16
|
+
* pairs and a table of the most recent events.
|
|
17
|
+
* - `dataProviders`— the server-side queries the two panels bind to.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* TelescopeModule.forRoot({ extensions: [nestjsDiagnosticsTelescope()] });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function nestjsDiagnosticsTelescope(options = {}) {
|
|
24
|
+
const topEventsLimit = options.topEventsLimit ?? 10;
|
|
25
|
+
const recentLimit = options.recentLimit ?? 50;
|
|
26
|
+
return {
|
|
27
|
+
name: 'nestjs-diagnostics',
|
|
28
|
+
watchers() {
|
|
29
|
+
return [new DiagnosticWatcher()];
|
|
30
|
+
},
|
|
31
|
+
entryTypes() {
|
|
32
|
+
return [{ id: DIAGNOSTIC_ENTRY_TYPE, label: 'Diagnostics', dot: 'bg-sky-400' }];
|
|
33
|
+
},
|
|
34
|
+
dashboards() {
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
id: 'diagnostics.diagnostics',
|
|
38
|
+
label: 'Diagnostics',
|
|
39
|
+
navGroup: 'Observability',
|
|
40
|
+
panels: [
|
|
41
|
+
{
|
|
42
|
+
kind: 'topN',
|
|
43
|
+
title: 'Busiest events',
|
|
44
|
+
data: { provider: TOP_EVENTS_PROVIDER, query: { limit: topEventsLimit } },
|
|
45
|
+
limit: topEventsLimit,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
kind: 'table',
|
|
49
|
+
title: 'Recent events',
|
|
50
|
+
data: { provider: RECENT_EVENTS_PROVIDER, query: { limit: recentLimit } },
|
|
51
|
+
columns: [
|
|
52
|
+
{ key: 'lib', label: 'Library' },
|
|
53
|
+
{ key: 'event', label: 'Event' },
|
|
54
|
+
{ key: 'traceId', label: 'Trace' },
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
},
|
|
61
|
+
dataProviders() {
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
name: TOP_EVENTS_PROVIDER,
|
|
65
|
+
async resolve(query, ctx) {
|
|
66
|
+
const limit = numberOr(query?.limit, topEventsLimit);
|
|
67
|
+
const entries = await loadDiagnostics(ctx);
|
|
68
|
+
const counts = new Map();
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const content = entry.content;
|
|
71
|
+
if (!content)
|
|
72
|
+
continue;
|
|
73
|
+
const label = `${content.lib}:${content.event}`;
|
|
74
|
+
counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
75
|
+
}
|
|
76
|
+
const items = [...counts.entries()]
|
|
77
|
+
.map(([label, value]) => ({ label, value }))
|
|
78
|
+
.sort((a, b) => b.value - a.value)
|
|
79
|
+
.slice(0, limit);
|
|
80
|
+
return { items };
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: RECENT_EVENTS_PROVIDER,
|
|
85
|
+
async resolve(query, ctx) {
|
|
86
|
+
const limit = numberOr(query?.limit, recentLimit);
|
|
87
|
+
const entries = await loadDiagnostics(ctx, limit);
|
|
88
|
+
const rows = entries.map((entry) => {
|
|
89
|
+
const content = entry.content;
|
|
90
|
+
return {
|
|
91
|
+
lib: content?.lib ?? null,
|
|
92
|
+
event: content?.event ?? null,
|
|
93
|
+
traceId: content?.traceId ?? null,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
return { rows };
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Resolve the Telescope store and fetch `diagnostic` entries. */
|
|
104
|
+
async function loadDiagnostics(ctx, limit = 200) {
|
|
105
|
+
const storage = ctx.moduleRef.get(TELESCOPE_STORAGE, { strict: false });
|
|
106
|
+
const page = await storage.get({ type: DIAGNOSTIC_ENTRY_TYPE, limit });
|
|
107
|
+
return page.data;
|
|
108
|
+
}
|
|
109
|
+
function numberOr(value, fallback) {
|
|
110
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
111
|
+
}
|
|
112
|
+
export default nestjsDiagnosticsTelescope;
|
|
113
|
+
//# sourceMappingURL=diagnostics-telescope.extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics-telescope.extension.js","sourceRoot":"","sources":["../src/diagnostics-telescope.extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,iBAAiB,GAGlB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,qBAAqB,EAErB,iBAAiB,GAClB,MAAM,yBAAyB,CAAC;AAEjC,oFAAoF;AACpF,MAAM,mBAAmB,GAAG,uBAAuB,CAAC;AACpD,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AAU1D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,0BAA0B,CACxC,UAAuC,EAAE;IAEzC,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,EAAE,CAAC;IACpD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC;IAE9C,OAAO;QACL,IAAI,EAAE,oBAAoB;QAE1B,QAAQ;YACN,OAAO,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AE,OAAO,EAAE,OAAO,IAAI,IAAI;6BAClC,CAAC;wBACJ,CAAC,CAAC,CAAC;wBACH,OAAO,EAAE,IAAI,EAAE,CAAC;oBAClB,CAAC;iBACF;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,kEAAkE;AAClE,KAAK,UAAU,eAAe,CAAC,GAAqB,EAAE,KAAK,GAAG,GAAG;IAC/D,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,GAAG,CAAkB,iBAAiB,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IACzF,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,qBAAqB,EAAE,KAAK,EAAE,CAAC,CAAC;IACvE,OAAO,IAAI,CAAC,IAAI,CAAC;AACnB,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc,EAAE,QAAgB;IAChD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;AAChF,CAAC;AAED,eAAe,0BAA0B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default } from './diagnostics-telescope.extension.js';
|
|
2
|
+
export { type DiagnosticsTelescopeOptions, nestjsDiagnosticsTelescope, } from './diagnostics-telescope.extension.js';
|
|
3
|
+
export { buildDiagnosticEntry, DIAGNOSTIC_ENTRY_TYPE, type DiagnosticEntryContent, DiagnosticWatcher, isDiagnosticEvent, } from './diagnostic.watcher.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAC/D,OAAO,EACL,KAAK,2BAA2B,EAChC,0BAA0B,GAC3B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,KAAK,sBAAsB,EAC3B,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default } from './diagnostics-telescope.extension.js';
|
|
2
|
+
export { nestjsDiagnosticsTelescope, } from './diagnostics-telescope.extension.js';
|
|
3
|
+
export { buildDiagnosticEntry, DIAGNOSTIC_ENTRY_TYPE, DiagnosticWatcher, isDiagnosticEvent, } from './diagnostic.watcher.js';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAC/D,OAAO,EAEL,0BAA0B,GAC3B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EAErB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,yBAAyB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dudousxd/nestjs-diagnostics-telescope",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "nestjs-telescope extension for @dudousxd/nestjs-diagnostics — one generic watcher records every aviary:<lib>:<event> diagnostic event in the Telescope dashboard.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/DavideCarvalho/nestjs-diagnostics.git",
|
|
9
|
+
"directory": "packages/telescope"
|
|
10
|
+
},
|
|
11
|
+
"author": "Davi Carvalho <davi@goflip.ai>",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist/",
|
|
24
|
+
"README.md",
|
|
25
|
+
"CHANGELOG.md"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@dudousxd/nestjs-diagnostics": "^0.2.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@dudousxd/nestjs-telescope": "^1.9.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@dudousxd/nestjs-telescope": "1.9.0",
|
|
35
|
+
"@dudousxd/nestjs-telescope-testing": "1.9.0",
|
|
36
|
+
"@types/node": "^20.0.0",
|
|
37
|
+
"typescript": "^5.4.0",
|
|
38
|
+
"vitest": "^3.0.0"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"nestjs",
|
|
45
|
+
"diagnostics",
|
|
46
|
+
"diagnostics-channel",
|
|
47
|
+
"telescope",
|
|
48
|
+
"observability",
|
|
49
|
+
"watcher",
|
|
50
|
+
"extension"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsc -p tsconfig.json",
|
|
54
|
+
"test": "vitest run --passWithNoTests",
|
|
55
|
+
"test:watch": "vitest",
|
|
56
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
57
|
+
}
|
|
58
|
+
}
|