-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsdk.ts
More file actions
221 lines (192 loc) · 7.39 KB
/
sdk.ts
File metadata and controls
221 lines (192 loc) · 7.39 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import type { Integration, Options } from '@sentry/core';
import {
applySdkMetadata,
consoleSandbox,
debug,
envToBool,
eventFiltersIntegration,
functionToStringIntegration,
getCurrentScope,
getIntegrationsToSetup,
linkedErrorsIntegration,
propagationContextFromHeaders,
requestDataIntegration,
spanStreamingIntegration,
stackParserFromStackParserOptions,
} from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';
import { childProcessIntegration } from '../integrations/childProcess';
import { nodeContextIntegration } from '../integrations/context';
import { contextLinesIntegration } from '../integrations/contextlines';
import { localVariablesIntegration } from '../integrations/local-variables';
import { modulesIntegration } from '../integrations/modules';
import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception';
import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection';
import { processSessionIntegration } from '../integrations/processSession';
import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME, spotlightIntegration } from '../integrations/spotlight';
import { consoleIntegration } from '../integrations/console';
import { systemErrorIntegration } from '../integrations/systemError';
import { defaultStackParser, getSentryRelease } from '../sdk/api';
import { makeNodeTransport } from '../transports';
import type { NodeClientOptions, NodeOptions } from '../types';
import { isCjs } from '../utils/detection';
import { getSpotlightConfig } from '../utils/spotlight';
import { setAsyncLocalStorageAsyncContextStrategy } from './asyncLocalStorageStrategy';
import { LightNodeClient } from './client';
import { httpIntegration } from './integrations/httpIntegration';
import { nativeNodeFetchIntegration } from './integrations/nativeNodeFetchIntegration';
/**
* Get default integrations for the Light Node-Core SDK.
*/
export function getDefaultIntegrations(): Integration[] {
return [
// Common
eventFiltersIntegration(),
functionToStringIntegration(),
linkedErrorsIntegration(),
requestDataIntegration(),
systemErrorIntegration(),
// Native Wrappers
consoleIntegration(),
httpIntegration(),
nativeNodeFetchIntegration(),
// Global Handlers
onUncaughtExceptionIntegration(),
onUnhandledRejectionIntegration(),
// Event Info
contextLinesIntegration(),
localVariablesIntegration(),
nodeContextIntegration(),
childProcessIntegration(),
processSessionIntegration(),
modulesIntegration(),
];
}
/**
* Initialize Sentry for Node in light mode (without OpenTelemetry).
*/
export function init(options: NodeOptions | undefined = {}): LightNodeClient | undefined {
return _init(options, getDefaultIntegrations);
}
/**
* Initialize Sentry for Node in light mode, without any integrations added by default.
*/
export function initWithoutDefaultIntegrations(options: NodeOptions | undefined = {}): LightNodeClient {
return _init(options, () => []);
}
/**
* Initialize Sentry for Node in light mode.
*/
function _init(
_options: NodeOptions | undefined = {},
getDefaultIntegrationsImpl: (options: Options) => Integration[],
): LightNodeClient {
const options = getClientOptions(_options, getDefaultIntegrationsImpl);
if (options.debug === true) {
if (DEBUG_BUILD) {
debug.enable();
} else {
// use `console.warn` rather than `debug.warn` since by non-debug bundles have all `debug.x` statements stripped
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn('[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle.');
});
}
}
// Use AsyncLocalStorage-based context strategy instead of OpenTelemetry
setAsyncLocalStorageAsyncContextStrategy();
const scope = getCurrentScope();
scope.update(options.initialScope);
if (options.spotlight && !options.integrations.some(({ name }) => name === SPOTLIGHT_INTEGRATION_NAME)) {
options.integrations.push(
spotlightIntegration({
sidecarUrl: typeof options.spotlight === 'string' ? options.spotlight : undefined,
}),
);
}
applySdkMetadata(options, 'node-light', ['node-core']);
const client = new LightNodeClient(options);
// The client is on the current scope, from where it generally is inherited
getCurrentScope().setClient(client);
client.init();
debug.log(`SDK initialized from ${isCjs() ? 'CommonJS' : 'ESM'} (light mode)`);
client.startClientReportTracking();
updateScopeFromEnvVariables();
// Ensure we flush events when vercel functions are ended
// See: https://vercel.com/docs/functions/functions-api-reference#sigterm-signal
if (process.env.VERCEL) {
process.on('SIGTERM', async () => {
// We have 500ms for processing here, so we try to make sure to have enough time to send the events
await client.flush(200);
});
}
return client;
}
function getClientOptions(
options: NodeOptions,
getDefaultIntegrationsImpl: (options: Options) => Integration[],
): NodeClientOptions {
const release = getRelease(options.release);
const spotlight = getSpotlightConfig(options.spotlight);
const tracesSampleRate = getTracesSampleRate(options.tracesSampleRate);
const mergedOptions = {
...options,
dsn: options.dsn ?? process.env.SENTRY_DSN,
environment: options.environment ?? process.env.SENTRY_ENVIRONMENT,
sendClientReports: options.sendClientReports ?? true,
transport: options.transport ?? makeNodeTransport,
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
release,
tracesSampleRate,
spotlight,
debug: envToBool(options.debug ?? process.env.SENTRY_DEBUG),
};
const integrations = options.integrations;
const defaultIntegrations = options.defaultIntegrations ?? getDefaultIntegrationsImpl(mergedOptions);
const resolvedIntegrations = getIntegrationsToSetup({
defaultIntegrations,
integrations,
});
if (mergedOptions.traceLifecycle === 'stream' && !resolvedIntegrations.some(i => i.name === 'SpanStreaming')) {
resolvedIntegrations.push(spanStreamingIntegration());
}
return {
...mergedOptions,
integrations: resolvedIntegrations,
};
}
function getRelease(release: NodeOptions['release']): string | undefined {
if (release !== undefined) {
return release;
}
const detectedRelease = getSentryRelease();
if (detectedRelease !== undefined) {
return detectedRelease;
}
return undefined;
}
function getTracesSampleRate(tracesSampleRate: NodeOptions['tracesSampleRate']): number | undefined {
if (tracesSampleRate !== undefined) {
return tracesSampleRate;
}
const sampleRateFromEnv = process.env.SENTRY_TRACES_SAMPLE_RATE;
if (!sampleRateFromEnv) {
return undefined;
}
const parsed = parseFloat(sampleRateFromEnv);
return isFinite(parsed) ? parsed : undefined;
}
/**
* Update scope and propagation context based on environmental variables.
*
* See https://github.com/getsentry/rfcs/blob/main/text/0071-continue-trace-over-process-boundaries.md
* for more details.
*/
function updateScopeFromEnvVariables(): void {
if (envToBool(process.env.SENTRY_USE_ENVIRONMENT) !== false) {
const sentryTraceEnv = process.env.SENTRY_TRACE;
const baggageEnv = process.env.SENTRY_BAGGAGE;
const propagationContext = propagationContextFromHeaders(sentryTraceEnv, baggageEnv);
getCurrentScope().setPropagationContext(propagationContext);
}
}