perf(core): Introduce decorator to report slow method calls (#17729)

This commit is contained in:
Iván Ovejero
2025-07-29 12:37:44 +02:00
committed by GitHub
parent ec6f243039
commit 056302ebe5
5 changed files with 72 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import type {
ListQuery,
} from '../entities/types-db';
import { isStringArray } from '../utils/is-string-array';
import { TimedQuery } from '../utils/timed-query';
type ResourceType = 'folder' | 'workflow';
@@ -371,6 +372,7 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
});
}
@TimedQuery()
async getMany(workflowIds: string[], options: ListQuery.Options = {}) {
if (workflowIds.length === 0) {
return [];

View File

@@ -0,0 +1,26 @@
import { Logger } from '@n8n/backend-common';
import { Timed } from '@n8n/decorators';
import { Container } from '@n8n/di';
/**
* Decorator that warns when database queries exceed a duration threshold.
*
* For options, see `@n8n/decorators/src/timed.ts`.
*
* @example
* ```ts
* @Service()
* class UserRepository {
* @TimedQuery()
* async findUsers() {
* // will log warning if execution takes > 100ms
* }
*
* @TimedQuery({ threshold: 50, logArgs: true })
* async findUserById(id: string) {
* // will log warning if execution takes >50ms, including args
* }
* }
* ```
*/
export const TimedQuery = Timed(Container.get(Logger), 'Slow database query');

View File

@@ -9,3 +9,4 @@ export * from './pubsub';
export { Redactable } from './redactable';
export * from './shutdown';
export * from './module/module-metadata';
export { Timed, TimedOptions } from './timed';

View 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;
};