Skip to content

Commit

Permalink
feat(nestjs): Duplicate SentryService behaviour into `@sentry/nestj…
Browse files Browse the repository at this point in the history
…s` SDK `init()`
  • Loading branch information
lforst committed Nov 15, 2024
1 parent a5a214c commit 41a7057
Showing 1 changed file with 32 additions and 3 deletions.
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 {
applySdkMetadata,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
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`,
});
}
}

0 comments on commit 41a7057

Please sign in to comment.