Skip to content
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

feat(nestjs): Duplicate SentryService behaviour into @sentry/nestjs SDK init() #14321

Merged
merged 2 commits into from
Nov 18, 2024
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