@dudousxd/nestjs-diagnostics 0.2.1 → 0.2.2
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 +17 -0
- package/dist/channel.d.ts +4 -0
- package/dist/channel.d.ts.map +1 -1
- package/dist/channel.js +33 -1
- package/dist/channel.js.map +1 -1
- package/dist/registry.d.ts +5 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +24 -0
- package/dist/registry.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @dudousxd/nestjs-diagnostics
|
|
2
2
|
|
|
3
|
+
## 0.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4](https://github.com/DavideCarvalho/nestjs-diagnostics/pull/4) [`2d9b817`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/2d9b817158b058e0c6413acfa8c2b3acd3728a6a) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - Make `emit()`/`getChannel()` ~11x cheaper on the hot path by memoizing the
|
|
8
|
+
resolved channel per `(lib, event)` pair. Previously every call re-built the
|
|
9
|
+
`aviary:<lib>:<event>` string, re-looked-up the node channel, and re-checked the
|
|
10
|
+
registry — ~174 ns/op even when nobody was subscribed. Now the first call for a
|
|
11
|
+
pair pays that cost and every subsequent call is two `Map.get`s returning the same
|
|
12
|
+
channel object (~16 ns/op; the no-subscriber path allocates nothing). The
|
|
13
|
+
consumer-side pattern of caching the channel and gating on `hasSubscribers` before
|
|
14
|
+
calling `emit` (~4 ns/op) stays the cheapest and remains recommended.
|
|
15
|
+
|
|
16
|
+
No API or behavior change: channel identity, registry discovery
|
|
17
|
+
(`registeredChannels`/`onChannelRegistered`), `hasSubscribers` gating,
|
|
18
|
+
`opts.traceId` precedence, and never-throw are all unchanged.
|
|
19
|
+
|
|
3
20
|
## 0.2.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/channel.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export declare function channelName(lib: string, event: string): string;
|
|
|
13
13
|
* `.hasSubscribers` is the cheap gate before building an envelope. Touching the
|
|
14
14
|
* channel also records its name in the {@link registerChannel registry} so a
|
|
15
15
|
* generic observer can discover it.
|
|
16
|
+
*
|
|
17
|
+
* Memoized via {@link channelCache}: only the first call for a `(lib, event)`
|
|
18
|
+
* pair builds the name, resolves the channel, and registers it; subsequent calls
|
|
19
|
+
* return the cached object after two map lookups.
|
|
16
20
|
*/
|
|
17
21
|
export declare function getChannel(lib: string, event: string): Channel;
|
|
18
22
|
/**
|
package/dist/channel.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAA4B,EAAE,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAG7E,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D,gEAAgE;AAChE,eAAO,MAAM,cAAc,WAAW,CAAC;AAEvC;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE9D;
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAA4B,EAAE,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAG7E,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D,gEAAgE;AAChE,eAAO,MAAM,cAAc,WAAW,CAAC;AAEvC;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE9D;AAqBD;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAc9D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,IAAI,CAgB3F"}
|
package/dist/channel.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import diagnostics_channel from 'node:diagnostics_channel';
|
|
2
2
|
import { resolveTraceId } from './context-accessor.js';
|
|
3
|
-
import { registerChannel } from './registry.js';
|
|
3
|
+
import { onRegistryReset, registerChannel } from './registry.js';
|
|
4
4
|
/** The prefix all channels created by this convention share. */
|
|
5
5
|
export const CHANNEL_PREFIX = 'aviary';
|
|
6
6
|
/**
|
|
@@ -10,17 +10,49 @@ export const CHANNEL_PREFIX = 'aviary';
|
|
|
10
10
|
export function channelName(lib, event) {
|
|
11
11
|
return `${CHANNEL_PREFIX}:${lib}:${event}`;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Per-`(lib, event)` cache of the resolved {@link Channel}, keyed by `lib` then
|
|
15
|
+
* `event`. The first {@link getChannel}/{@link emit} for a pair pays the string
|
|
16
|
+
* concat + `diagnostics_channel.channel()` lookup + registry insert; every call
|
|
17
|
+
* after that is a pair of `Map.get`s and returns the SAME object — no string is
|
|
18
|
+
* allocated on the steady state. A nested map (rather than one keyed by the full
|
|
19
|
+
* `aviary:<lib>:<event>` name) is what lets us skip the concat entirely.
|
|
20
|
+
*
|
|
21
|
+
* Node already returns a process-stable {@link Channel} per name, so caching it
|
|
22
|
+
* here changes nothing observable: `getChannel(a,b)` still returns
|
|
23
|
+
* `diagnostics_channel.channel('aviary:a:b')`. The registry is still fed exactly
|
|
24
|
+
* once per pair (on the cache miss), so discovery is unchanged.
|
|
25
|
+
*/
|
|
26
|
+
const channelCache = new Map();
|
|
27
|
+
// Drop the memo cache whenever the registry is reset (test-only), so the next
|
|
28
|
+
// getChannel re-registers every channel. See onRegistryReset in registry.ts.
|
|
29
|
+
onRegistryReset(() => channelCache.clear());
|
|
13
30
|
/**
|
|
14
31
|
* The memoized `node:diagnostics_channel` for a `<lib>`/`<event>` pair. Node
|
|
15
32
|
* returns the same {@link Channel} object for the same name, so reading
|
|
16
33
|
* `.hasSubscribers` is the cheap gate before building an envelope. Touching the
|
|
17
34
|
* channel also records its name in the {@link registerChannel registry} so a
|
|
18
35
|
* generic observer can discover it.
|
|
36
|
+
*
|
|
37
|
+
* Memoized via {@link channelCache}: only the first call for a `(lib, event)`
|
|
38
|
+
* pair builds the name, resolves the channel, and registers it; subsequent calls
|
|
39
|
+
* return the cached object after two map lookups.
|
|
19
40
|
*/
|
|
20
41
|
export function getChannel(lib, event) {
|
|
42
|
+
let byEvent = channelCache.get(lib);
|
|
43
|
+
if (byEvent !== undefined) {
|
|
44
|
+
const cached = byEvent.get(event);
|
|
45
|
+
if (cached !== undefined)
|
|
46
|
+
return cached;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
byEvent = new Map();
|
|
50
|
+
channelCache.set(lib, byEvent);
|
|
51
|
+
}
|
|
21
52
|
const name = channelName(lib, event);
|
|
22
53
|
const channel = diagnostics_channel.channel(name);
|
|
23
54
|
registerChannel(name);
|
|
55
|
+
byEvent.set(event, channel);
|
|
24
56
|
return channel;
|
|
25
57
|
}
|
|
26
58
|
/**
|
package/dist/channel.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,mBAAqC,MAAM,0BAA0B,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,mBAAqC,MAAM,0BAA0B,CAAC;A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,UAAU,IAAI,CAAC,GAAW,EAAE,KAAa,EAAE,OAAgB,EAAE,IAAkB;IACnF,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACvC,IAAI,CAAC,OAAO,CAAC,cAAc;QAAE,OAAO;IACpC,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,cAAc,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAoB;YAChC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE;YACd,GAAG;YACH,KAAK;YACL,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,OAAO;SACR,CAAC;QACF,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;IAC3D,CAAC;AACH,CAAC"}
|
package/dist/registry.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ export declare function registeredChannels(): string[];
|
|
|
31
31
|
* ```
|
|
32
32
|
*/
|
|
33
33
|
export declare function onChannelRegistered(cb: (name: string) => void): () => void;
|
|
34
|
+
/**
|
|
35
|
+
* Register a callback run by {@link resetRegistry}. Used internally by
|
|
36
|
+
* {@link getChannel}'s memo cache; not part of the public contract.
|
|
37
|
+
*/
|
|
38
|
+
export declare function onRegistryReset(cb: () => void): void;
|
|
34
39
|
/**
|
|
35
40
|
* Test-only: forget every registered channel and listener. Not part of the
|
|
36
41
|
* public contract — exposed so suites can assert registration in isolation.
|
package/dist/registry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AA6BH;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAUlD;AAED,4EAA4E;AAC5E,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAE7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAK1E;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AA6BH;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAUlD;AAED,4EAA4E;AAC5E,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAE7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAK1E;AAYD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,IAAI,CAEpD;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAUpC"}
|
package/dist/registry.js
CHANGED
|
@@ -58,6 +58,22 @@ export function onChannelRegistered(cb) {
|
|
|
58
58
|
listeners.delete(cb);
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Hooks run by {@link resetRegistry} so other modules can drop registry-derived
|
|
63
|
+
* caches in lockstep. {@link getChannel} memoizes channels and only registers a
|
|
64
|
+
* name on a cache MISS; if `resetRegistry` cleared `channels` without clearing
|
|
65
|
+
* that cache, an already-cached channel would never re-register and would vanish
|
|
66
|
+
* from discovery. Registering its cache-clear here keeps the test-only reset
|
|
67
|
+
* exact: after a reset, the next `getChannel`/`emit` re-registers every channel.
|
|
68
|
+
*/
|
|
69
|
+
const resetHooks = new Set();
|
|
70
|
+
/**
|
|
71
|
+
* Register a callback run by {@link resetRegistry}. Used internally by
|
|
72
|
+
* {@link getChannel}'s memo cache; not part of the public contract.
|
|
73
|
+
*/
|
|
74
|
+
export function onRegistryReset(cb) {
|
|
75
|
+
resetHooks.add(cb);
|
|
76
|
+
}
|
|
61
77
|
/**
|
|
62
78
|
* Test-only: forget every registered channel and listener. Not part of the
|
|
63
79
|
* public contract — exposed so suites can assert registration in isolation.
|
|
@@ -65,5 +81,13 @@ export function onChannelRegistered(cb) {
|
|
|
65
81
|
export function resetRegistry() {
|
|
66
82
|
channels.clear();
|
|
67
83
|
listeners.clear();
|
|
84
|
+
for (const hook of resetHooks) {
|
|
85
|
+
try {
|
|
86
|
+
hook();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// A misbehaving reset hook must never break the reset itself.
|
|
90
|
+
}
|
|
91
|
+
}
|
|
68
92
|
}
|
|
69
93
|
//# sourceMappingURL=registry.js.map
|
package/dist/registry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAoBH,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;AACzE,MAAM,WAAW,GAAG,UAA+D,CAAC;AACpF,MAAM,QAAQ,GAAa,WAAW,CAAC,YAAY,CAAC,IAAI;IACtD,QAAQ,EAAE,IAAI,GAAG,EAAU;IAC3B,SAAS,EAAE,IAAI,GAAG,EAA0B;CAC7C,CAAC;AACF,WAAW,CAAC,YAAY,CAAC,GAAG,QAAQ,CAAC;AACrC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY;IAC1C,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO;IAC/B,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACnB,KAAK,MAAM,QAAQ,I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