Skip to content

Commit 7e59832

Browse files
authored
feat(node): Ensure Nest.js spans have better data (#12139)
Ensure op and origin is correctly set.
1 parent 99351c3 commit 7e59832

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

dev-packages/e2e-tests/test-applications/node-nestjs/tests/transactions.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ test('Sends an API route transaction', async ({ baseURL }) => {
107107
span_id: expect.any(String),
108108
trace_id: expect.any(String),
109109
data: {
110-
'sentry.origin': 'manual',
110+
'sentry.origin': 'auto.http.otel.nestjs',
111+
'sentry.op': 'handler.nestjs',
111112
component: '@nestjs/core',
112113
'nestjs.version': expect.any(String),
113114
'nestjs.type': 'handler',
@@ -119,7 +120,8 @@ test('Sends an API route transaction', async ({ baseURL }) => {
119120
start_timestamp: expect.any(Number),
120121
timestamp: expect.any(Number),
121122
status: 'ok',
122-
origin: 'manual',
123+
origin: 'auto.http.otel.nestjs',
124+
op: 'handler.nestjs',
123125
},
124126
]),
125127
transaction: 'GET /test-transaction',

packages/node/src/integrations/tracing/connect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {
4040

4141
// Sadly, ConnectInstrumentation has no requestHook, so we need to add the attributes here
4242
// We register this hook in this method, because if we register it in the integration `setup`,
43-
// it would always run even for users that are not even using fastify
43+
// it would always run even for users that are not even using connect
4444
const client = getClient();
4545
if (client) {
4646
client.on('spanStart', span => {

packages/node/src/integrations/tracing/nest.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
2-
import { captureException, defineIntegration, getDefaultIsolationScope, getIsolationScope } from '@sentry/core';
2+
import {
3+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
4+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
5+
captureException,
6+
defineIntegration,
7+
getClient,
8+
getDefaultIsolationScope,
9+
getIsolationScope,
10+
spanToJSON,
11+
} from '@sentry/core';
312
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
4-
import type { IntegrationFn } from '@sentry/types';
13+
import type { IntegrationFn, Span } from '@sentry/types';
514
import { logger } from '@sentry/utils';
615

716
interface MinimalNestJsExecutionContext {
@@ -52,6 +61,16 @@ export const nestIntegration = defineIntegration(_nestIntegration);
5261
* Setup an error handler for Nest.
5362
*/
5463
export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): void {
64+
// Sadly, NestInstrumentation has no requestHook, so we need to add the attributes here
65+
// We register this hook in this method, because if we register it in the integration `setup`,
66+
// it would always run even for users that are not even using Nest.js
67+
const client = getClient();
68+
if (client) {
69+
client.on('spanStart', span => {
70+
addNestSpanAttributes(span);
71+
});
72+
}
73+
5574
app.useGlobalInterceptors({
5675
intercept(context, next) {
5776
if (getIsolationScope() === getDefaultIsolationScope()) {
@@ -86,3 +105,20 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE
86105

87106
app.useGlobalFilters(wrappedFilter);
88107
}
108+
109+
function addNestSpanAttributes(span: Span): void {
110+
const attributes = spanToJSON(span).data || {};
111+
112+
// this is one of: app_creation, request_context, handler
113+
const type = attributes['nestjs.type'];
114+
115+
// If this is already set, or we have no nest.js span, no need to process again...
116+
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) {
117+
return;
118+
}
119+
120+
span.setAttributes({
121+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs',
122+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`,
123+
});
124+
}

0 commit comments

Comments
 (0)