Skip to content

Commit 43c7079

Browse files
authored
feat(nestjs): Duplicate SentryService behaviour into @sentry/nestjs SDK init() (#14321)
1 parent ef83bfc commit 43c7079

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

packages/nestjs/src/sdk.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { applySdkMetadata } from '@sentry/core';
2-
import type { NodeClient, NodeOptions } from '@sentry/node';
1+
import {
2+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
3+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
4+
applySdkMetadata,
5+
spanToJSON,
6+
} from '@sentry/core';
7+
import type { NodeClient, NodeOptions, Span } from '@sentry/node';
38
import { init as nodeInit } from '@sentry/node';
49

510
/**
@@ -12,5 +17,29 @@ export function init(options: NodeOptions | undefined = {}): NodeClient | undefi
1217

1318
applySdkMetadata(opts, 'nestjs');
1419

15-
return nodeInit(opts);
20+
const client = nodeInit(opts);
21+
22+
if (client) {
23+
client.on('spanStart', span => {
24+
// The NestInstrumentation has no requestHook, so we add NestJS-specific attributes here
25+
addNestSpanAttributes(span);
26+
});
27+
}
28+
29+
return client;
30+
}
31+
32+
function addNestSpanAttributes(span: Span): void {
33+
const attributes = spanToJSON(span).data || {};
34+
35+
// this is one of: app_creation, request_context, handler
36+
const type = attributes['nestjs.type'];
37+
38+
// Only set the NestJS attributes for spans that are created by the NestJS instrumentation and for spans that do not have an op already.
39+
if (type && !attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]) {
40+
span.setAttributes({
41+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs',
42+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`,
43+
});
44+
}
1645
}

0 commit comments

Comments
 (0)