-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsdk.ts
53 lines (44 loc) · 1.67 KB
/
sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
applySdkMetadata,
spanToJSON,
} from '@sentry/core';
import type { Integration } from '@sentry/core';
import type { NodeClient, NodeOptions, Span } from '@sentry/node';
import { getDefaultIntegrations as getDefaultNodeIntegrations, init as nodeInit } from '@sentry/node';
import { nestIntegration } from './integrations/nest';
/**
* Initializes the NestJS SDK
*/
export function init(options: NodeOptions | undefined = {}): NodeClient | undefined {
const opts: NodeOptions = {
defaultIntegrations: getDefaultIntegrations(options),
...options,
};
applySdkMetadata(opts, 'nestjs');
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;
}
/** Get the default integrations for the NestJS SDK. */
export function getDefaultIntegrations(options: NodeOptions): Integration[] | undefined {
return [nestIntegration(), ...getDefaultNodeIntegrations(options)];
}
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`,
});
}
}