Skip to content

feat(nestjs): Caching instrumentation #13258

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/node/src/integrations/tracing/nest/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { addNonEnumerableProperty } from '@sentry/utils';
import type { InjectableTarget } from './types';
import type {InjectableTarget, InjectTarget} from './types';

const sentryPatched = 'sentryPatched';

Expand All @@ -10,7 +10,7 @@ const sentryPatched = 'sentryPatched';
* We already guard duplicate patching with isWrapped. However, isWrapped checks whether a file has been patched, whereas we use this check for concrete target classes.
* This check might not be necessary, but better to play it safe.
*/
export function isPatched(target: InjectableTarget): boolean {
export function isPatched(target: InjectableTarget | InjectTarget): boolean {
if (target.sentryPatched) {
return true;
}
Expand All @@ -23,7 +23,7 @@ export function isPatched(target: InjectableTarget): boolean {
* Returns span options for nest middleware spans.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function getMiddlewareSpanOptions(target: InjectableTarget) {
export function getMiddlewareSpanOptions(target: InjectableTarget | InjectTarget) {
return {
name: target.name,
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import {
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
} from '@opentelemetry/instrumentation';
import { getActiveSpan, startSpan, startSpanManual, withActiveSpan } from '@sentry/core';
import {
getActiveSpan,
startSpan,
startSpanManual,
withActiveSpan,
startInactiveSpan,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_CACHE_KEY,
SEMANTIC_ATTRIBUTE_CACHE_HIT
} from '@sentry/core';
import type { Span } from '@sentry/types';
import { SDK_VERSION } from '@sentry/utils';
import { getMiddlewareSpanOptions, isPatched } from './helpers';
import type { InjectableTarget } from './types';
import type { InjectableTarget, InjectTarget } from './types';

const supportedVersions = ['>=8.0.0 <11'];

const CACHE_MANAGER = 'CACHE_MANAGER';

/**
* Custom instrumentation for nestjs.
*
Expand All @@ -34,7 +46,7 @@ export class SentryNestInstrumentation extends InstrumentationBase {
public init(): InstrumentationNodeModuleDefinition {
const moduleDef = new InstrumentationNodeModuleDefinition(SentryNestInstrumentation.COMPONENT, supportedVersions);

moduleDef.files.push(this._getInjectableFileInstrumentation(supportedVersions));
moduleDef.files.push(this._getInjectableFileInstrumentation(supportedVersions), this._getInjectFileInstrumentation(supportedVersions));
return moduleDef;
}

Expand All @@ -58,6 +70,60 @@ export class SentryNestInstrumentation extends InstrumentationBase {
);
}

/**
*
*/
private _getInjectFileInstrumentation(versions: string[]): InstrumentationNodeModuleFile {
return new InstrumentationNodeModuleFile(
'@nestjs/common/decorators/core/inject.decorator.js',
versions,
(moduleExports: { Inject: InjectTarget }) => {
if (isWrapped(moduleExports.Inject)) {
this._unwrap(moduleExports, 'Inject');
}
this._wrap(moduleExports, 'Inject', this._createWrapInject());
return moduleExports;
},
(moduleExports: { Inject: InjectTarget }) => {
this._unwrap(moduleExports, 'Inject');
},
)
}

/**
*
*/
private _createWrapInject() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function wrapInject(original: any) {
return function wrappedInject(token?: unknown) {
return function (target: InjectTarget, key: string | symbol | undefined, index?: number) {
if (token !== CACHE_MANAGER || target.prototype === undefined) {
return original(token)(target, key, index);
}

if (index === undefined) {
return original(token)(target, key, index);
}

// target is the service that injects the cache manager
// TODO: retrieve cache manager so we can patch it

console.log('bla: ', target.prototype.cacheManager);

console.log('target: ', target);
console.log('target prototype', target.prototype);
console.log('property name: ', Object.getOwnPropertyNames(target.prototype));
console.log('token: ', token);
console.log('key: ', key);
console.log('index: ', index);

return original(token)(target, key, index);
}
}
}
}

/**
* Creates a wrapper function for the @Injectable decorator.
*
Expand Down
12 changes: 12 additions & 0 deletions packages/node/src/integrations/tracing/nest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ export interface InjectableTarget {
intercept?: (context: unknown, next: CallHandler, ...args: any[]) => Observable<any>;
};
}

export interface InjectTarget {
name: string;
sentryPatched?: boolean;
__SENTRY_INTERNAL__?: boolean;
prototype: {
set?: (key: string, ...args: any[]) => Promise<void> | void;
get?: (key: string, ...args: any[]) => Promise<unknown> | undefined;
del?: (key: string, ...args: any[]) => void | Promise<void>;
cacheManager?: any;
}
}