Skip to content
Merged
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
35 changes: 32 additions & 3 deletions packages/nestjs/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { applySdkMetadata } from '@sentry/core';
import type { NodeClient, NodeOptions } from '@sentry/node';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
applySdkMetadata,
spanToJSON,
} from '@sentry/core';
import type { NodeClient, NodeOptions, Span } from '@sentry/node';
import { init as nodeInit } from '@sentry/node';

/**
Expand All @@ -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`,
});
}
}
Loading