-
Notifications
You must be signed in to change notification settings - Fork 41
feat(debug): add capture trace on error admin option #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { AsyncLocalStorage } from 'node:async_hooks'; | |||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export interface RequestContext { | ||||||||||||||||||||||
| keyName?: string; | ||||||||||||||||||||||
| requestId?: string; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const storage = new AsyncLocalStorage<RequestContext>(); | ||||||||||||||||||||||
|
|
@@ -36,6 +37,26 @@ export function getCurrentKeyName(): string | undefined { | |||||||||||||||||||||
| return storage.getStore()?.keyName; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Read just the request id from the active request context. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export function getCurrentRequestId(): string | undefined { | ||||||||||||||||||||||
| return storage.getStore()?.requestId; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Attach the request id to the active request context so downstream code | ||||||||||||||||||||||
| * (notably DebugManager, reached via the cooldown path) can resolve the | ||||||||||||||||||||||
| * request id without explicit plumbing. Replaces the store via `enterWith` | ||||||||||||||||||||||
| * rather than mutating the existing object in place, so any other holder of | ||||||||||||||||||||||
| * a reference to the prior store is unaffected. No-op when called outside a | ||||||||||||||||||||||
| * request context. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export function setCurrentRequestId(requestId: string): void { | ||||||||||||||||||||||
| const store = storage.getStore(); | ||||||||||||||||||||||
| if (store) storage.enterWith({ ...store, requestId }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+55
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Mutating the shared
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Run a callback with a given request context. Prefer this wrapper in tests | ||||||||||||||||||||||
| * or short-lived scopes; long-lived Fastify handlers should use | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: When
captureOnErroris enabled,isCaptureEnabled()returns true for every request, causing all requests to be buffered inpendingLogsuntil flush. For high-throughput deployments this can lead to unbounded memory growth if many requests succeed and are discarded. Consider adding a cap on the number of pending logs or a TTL-based eviction for buffered traces in capture-on-error mode. [general, importance: 7]