The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/packages/tracing/src/traceInstanceMethods.ts

44 lines
1.2 KiB

import { tracerActiveSpan } from '.';
const getArguments = (args: any[]): any[] => {
return args.map((arg) => {
if (typeof arg === 'object' && arg != null && 'session' in arg) {
return '[mongo options with session]';
}
return arg;
});
};
export function traceInstanceMethods<T extends object>(instance: T, ignoreMethods: string[] = []): T {
const className = instance.constructor.name;
return new Proxy(instance, {
get(target: Record<string, any>, prop: string): any {
if (typeof target[prop] === 'function' && !ignoreMethods.includes(prop)) {
return new Proxy(target[prop], {
apply: (target, thisArg, argumentsList): any => {
if (['doNotMixInclusionAndExclusionFields', 'ensureDefaultFields'].includes(prop)) {
return Reflect.apply(target, thisArg, argumentsList);
}
return tracerActiveSpan(
`model ${className}.${prop}`,
{
attributes: {
model: className,
method: prop,
parameters: getArguments(argumentsList),
},
},
() => {
return Reflect.apply(target, thisArg, argumentsList);
},
);
},
});
}
return Reflect.get(target, prop);
},
}) as T;
}