Skip to content
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

feat(nestjs): Remove SentryService #14759

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
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
72 changes: 1 addition & 71 deletions packages/nestjs/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ import type {
ExecutionContext,
HttpServer,
NestInterceptor,
OnModuleInit,
} from '@nestjs/common';
import { Catch, Global, HttpException, Injectable, Logger, Module } from '@nestjs/common';
import { APP_INTERCEPTOR, BaseExceptionFilter } from '@nestjs/core';
import type { Span } from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
captureException,
getClient,
getDefaultIsolationScope,
getIsolationScope,
logger,
spanToJSON,
} from '@sentry/core';
import { captureException, getDefaultIsolationScope, getIsolationScope, logger } from '@sentry/core';
import type { Observable } from 'rxjs';
import { isExpectedError } from './helpers';

Expand Down Expand Up @@ -177,40 +166,6 @@ export { SentryGlobalGraphQLFilter };
*/
export const SentryGlobalGenericFilter = SentryGlobalFilter;

/**
* Service to set up Sentry performance tracing for Nest.js applications.
*
* @deprecated `SentryService` is deprecated.
* If you are using `@sentry/nestjs` you can safely remove any references to the `SentryService`.
* If you are using another package migrate to `@sentry/nestjs` and remove the `SentryService` afterwards.
*/
class SentryService implements OnModuleInit {
public readonly __SENTRY_INTERNAL__: boolean;

public constructor() {
this.__SENTRY_INTERNAL__ = true;
}

/**
* Initializes the Sentry service and registers span attributes.
*/
public onModuleInit(): void {
// Sadly, NestInstrumentation has no requestHook, so we need to add the attributes here
// We register this hook in this method, because if we register it in the integration `setup`,
// it would always run even for users that are not even using Nest.js
const client = getClient();
if (client) {
client.on('spanStart', span => {
addNestSpanAttributes(span);
});
}
}
}
// eslint-disable-next-line deprecation/deprecation
Injectable()(SentryService);
// eslint-disable-next-line deprecation/deprecation
export { SentryService };

/**
* Set up a root module that can be injected in nest applications.
*/
Expand All @@ -222,48 +177,23 @@ class SentryModule {
return {
module: SentryModule,
providers: [
// eslint-disable-next-line deprecation/deprecation
SentryService,
{
provide: APP_INTERCEPTOR,
// eslint-disable-next-line deprecation/deprecation
useClass: SentryTracingInterceptor,
},
],
// eslint-disable-next-line deprecation/deprecation
exports: [SentryService],
};
}
}
Global()(SentryModule);
Module({
providers: [
// eslint-disable-next-line deprecation/deprecation
SentryService,
{
provide: APP_INTERCEPTOR,
// eslint-disable-next-line deprecation/deprecation
useClass: SentryTracingInterceptor,
},
],
// eslint-disable-next-line deprecation/deprecation
exports: [SentryService],
})(SentryModule);
export { SentryModule };

function addNestSpanAttributes(span: Span): void {
const attributes = spanToJSON(span).data;

// this is one of: app_creation, request_context, handler
const type = attributes['nestjs.type'];

// If this is already set, or we have no nest.js span, no need to process again...
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) {
return;
}

span.setAttributes({
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`,
});
}
Loading