mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-22 12:19:09 +00:00
perf(core): Introduce decorator to report slow method calls (#17729)
This commit is contained in:
@@ -20,6 +20,7 @@ import type {
|
|||||||
ListQuery,
|
ListQuery,
|
||||||
} from '../entities/types-db';
|
} from '../entities/types-db';
|
||||||
import { isStringArray } from '../utils/is-string-array';
|
import { isStringArray } from '../utils/is-string-array';
|
||||||
|
import { TimedQuery } from '../utils/timed-query';
|
||||||
|
|
||||||
type ResourceType = 'folder' | 'workflow';
|
type ResourceType = 'folder' | 'workflow';
|
||||||
|
|
||||||
@@ -371,6 +372,7 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TimedQuery()
|
||||||
async getMany(workflowIds: string[], options: ListQuery.Options = {}) {
|
async getMany(workflowIds: string[], options: ListQuery.Options = {}) {
|
||||||
if (workflowIds.length === 0) {
|
if (workflowIds.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
26
packages/@n8n/db/src/utils/timed-query.ts
Normal file
26
packages/@n8n/db/src/utils/timed-query.ts
Normal 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');
|
||||||
@@ -9,3 +9,4 @@ export * from './pubsub';
|
|||||||
export { Redactable } from './redactable';
|
export { Redactable } from './redactable';
|
||||||
export * from './shutdown';
|
export * from './shutdown';
|
||||||
export * from './module/module-metadata';
|
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;
|
||||||
|
};
|
||||||
@@ -4,10 +4,8 @@ import { getCommunityNodeTypes } from '../../utils/community-node-types-utils';
|
|||||||
import { CommunityNodeTypesService } from '../community-node-types.service';
|
import { CommunityNodeTypesService } from '../community-node-types.service';
|
||||||
|
|
||||||
jest.mock('@n8n/backend-common', () => ({
|
jest.mock('@n8n/backend-common', () => ({
|
||||||
|
...jest.requireActual('@n8n/backend-common'),
|
||||||
inProduction: jest.fn().mockReturnValue(false),
|
inProduction: jest.fn().mockReturnValue(false),
|
||||||
logger: jest.fn().mockImplementation(() => ({
|
|
||||||
error: jest.fn(),
|
|
||||||
})),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('../../utils/community-node-types-utils', () => ({
|
jest.mock('../../utils/community-node-types-utils', () => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user