Skip to content

feat(node): Ensure Nest.js spans have better data #12139

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

Merged
merged 1 commit into from
May 21, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ test('Sends an API route transaction', async ({ baseURL }) => {
span_id: expect.any(String),
trace_id: expect.any(String),
data: {
'sentry.origin': 'manual',
'sentry.origin': 'auto.http.otel.nestjs',
'sentry.op': 'handler.nestjs',
component: '@nestjs/core',
'nestjs.version': expect.any(String),
'nestjs.type': 'handler',
Expand All @@ -119,7 +120,8 @@ test('Sends an API route transaction', async ({ baseURL }) => {
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
status: 'ok',
origin: 'manual',
origin: 'auto.http.otel.nestjs',
op: 'handler.nestjs',
},
]),
transaction: 'GET /test-transaction',
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/integrations/tracing/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {

// Sadly, ConnectInstrumentation 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 fastify
// it would always run even for users that are not even using connect
const client = getClient();
if (client) {
client.on('spanStart', span => {
Expand Down
40 changes: 38 additions & 2 deletions packages/node/src/integrations/tracing/nest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
import { captureException, defineIntegration, getDefaultIsolationScope, getIsolationScope } from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
captureException,
defineIntegration,
getClient,
getDefaultIsolationScope,
getIsolationScope,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import type { IntegrationFn, Span } from '@sentry/types';
import { logger } from '@sentry/utils';

interface MinimalNestJsExecutionContext {
Expand Down Expand Up @@ -52,6 +61,16 @@ export const nestIntegration = defineIntegration(_nestIntegration);
* Setup an error handler for Nest.
*/
export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): 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);
});
}

app.useGlobalInterceptors({
intercept(context, next) {
if (getIsolationScope() === getDefaultIsolationScope()) {
Expand Down Expand Up @@ -86,3 +105,20 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE

app.useGlobalFilters(wrappedFilter);
}

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