From 41a70577b96a7f8fafd9eea70cb9570d2bd41b8c Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 15 Nov 2024 12:43:02 +0000 Subject: [PATCH 1/2] feat(nestjs): Duplicate `SentryService` behaviour into `@sentry/nestjs` SDK `init()` --- packages/nestjs/src/sdk.ts | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/nestjs/src/sdk.ts b/packages/nestjs/src/sdk.ts index 8d5ca21b1706..c1b19e5e395d 100644 --- a/packages/nestjs/src/sdk.ts +++ b/packages/nestjs/src/sdk.ts @@ -1,5 +1,10 @@ -import { applySdkMetadata } from '@sentry/core'; -import type { NodeClient, NodeOptions } from '@sentry/node'; +import { + applySdkMetadata, + SEMANTIC_ATTRIBUTE_SENTRY_OP, + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, + spanToJSON, +} from '@sentry/core'; +import type { NodeClient, NodeOptions, Span } from '@sentry/node'; import { init as nodeInit } from '@sentry/node'; /** @@ -12,5 +17,29 @@ export function init(options: NodeOptions | undefined = {}): NodeClient | undefi applySdkMetadata(opts, 'nestjs'); - return nodeInit(opts); + const client = nodeInit(opts); + + if (client) { + client.on('spanStart', span => { + // The NestInstrumentation has no requestHook, so we add NestJS-specific attributes here + addNestSpanAttributes(span); + }); + } + + return client; +} + +function addNestSpanAttributes(span: Span): void { + const attributes = spanToJSON(span).data || {}; + + // this is one of: app_creation, request_context, handler + const type = attributes['nestjs.type']; + + // Only set the NestJS attributes for spans that are created by the NestJS instrumentation and for spans that do not have an op already. + if (type && !attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]) { + span.setAttributes({ + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs', + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`, + }); + } } From befa5f80ffeeea1005f444ddc7d25e80a26dfa81 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 15 Nov 2024 13:55:17 +0000 Subject: [PATCH 2/2] lint --- packages/nestjs/src/sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nestjs/src/sdk.ts b/packages/nestjs/src/sdk.ts index c1b19e5e395d..b4789e2d01c2 100644 --- a/packages/nestjs/src/sdk.ts +++ b/packages/nestjs/src/sdk.ts @@ -1,7 +1,7 @@ import { - applySdkMetadata, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, + applySdkMetadata, spanToJSON, } from '@sentry/core'; import type { NodeClient, NodeOptions, Span } from '@sentry/node';