mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
perf(core): Introduce decorator to report slow method calls (#17729)
This commit is contained in:
@@ -9,3 +9,4 @@ export * from './pubsub';
|
||||
export { Redactable } from './redactable';
|
||||
export * from './shutdown';
|
||||
export * from './module/module-metadata';
|
||||
export { Timed, TimedOptions } from './timed';
|
||||
|
||||
42
packages/@n8n/decorators/src/timed.ts
Normal file
42
packages/@n8n/decorators/src/timed.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
export interface TimedOptions {
|
||||
/** Duration (in ms) above which to log a warning. Defaults to `100`. */
|
||||
threshold?: number;
|
||||
/** Whether to include method parameters in the log. Defaults to `false`. */
|
||||
logArgs?: boolean;
|
||||
}
|
||||
|
||||
interface Logger {
|
||||
warn(message: string, meta?: object): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory to create decorators to warn when method calls exceed a duration threshold.
|
||||
*/
|
||||
export const Timed =
|
||||
(logger: Logger, msg = 'Slow method call') =>
|
||||
(options: TimedOptions = {}): MethodDecorator =>
|
||||
(_target, propertyKey, descriptor: PropertyDescriptor) => {
|
||||
const originalMethod = descriptor.value as (...args: unknown[]) => unknown;
|
||||
const thresholdMs = options.threshold ?? 100;
|
||||
const logArgs = options.logArgs ?? false;
|
||||
|
||||
descriptor.value = async function (...args: unknown[]) {
|
||||
const methodName = `${this.constructor.name}.${String(propertyKey)}`;
|
||||
const start = performance.now();
|
||||
const result = await originalMethod.apply(this, args);
|
||||
const durationMs = performance.now() - start;
|
||||
|
||||
if (durationMs > thresholdMs) {
|
||||
logger.warn(msg, {
|
||||
method: methodName,
|
||||
durationMs: Math.round(durationMs),
|
||||
thresholdMs,
|
||||
params: logArgs ? args : '[hidden]',
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
Reference in New Issue
Block a user