1
- import type { Integration , Options } from '@sentry/core' ;
1
+ import type { Integration } from '@sentry/core' ;
2
2
import {
3
3
consoleIntegration ,
4
- consoleSandbox ,
5
4
functionToStringIntegration ,
6
5
getCurrentScope ,
7
6
getIntegrationsToSetup ,
8
7
hasSpansEnabled ,
9
8
inboundFiltersIntegration ,
9
+ initAndBind ,
10
10
linkedErrorsIntegration ,
11
11
logger ,
12
12
propagationContextFromHeaders ,
@@ -30,14 +30,14 @@ import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
30
30
import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception' ;
31
31
import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection' ;
32
32
import { processSessionIntegration } from '../integrations/processSession' ;
33
- import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME , spotlightIntegration } from '../integrations/spotlight' ;
33
+ import { spotlightIntegration } from '../integrations/spotlight' ;
34
34
import { getAutoPerformanceIntegrations } from '../integrations/tracing' ;
35
35
import { makeNodeTransport } from '../transports' ;
36
36
import type { NodeClientOptions , NodeOptions } from '../types' ;
37
37
import { isCjs } from '../utils/commonjs' ;
38
38
import { envToBool } from '../utils/envToBool' ;
39
- import { defaultStackParser , getSentryRelease } from './api' ;
40
- import { NodeClient } from './client' ;
39
+ import { defaultStackParser } from './api' ;
40
+ import { getTracesSampleRate , NodeClient } from './client' ;
41
41
import { initOpenTelemetry , maybeInitializeEsmLoader } from './initOtel' ;
42
42
43
43
function getCjsOnlyIntegrations ( ) : Integration [ ] {
@@ -74,9 +74,12 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
74
74
}
75
75
76
76
/** Get the default integrations for the Node SDK. */
77
- export function getDefaultIntegrations ( options : Options ) : Integration [ ] {
77
+ export function getDefaultIntegrations ( options : NodeOptions ) : Integration [ ] {
78
78
return [
79
79
...getDefaultIntegrationsWithoutPerformance ( ) ,
80
+ ...( options . spotlight
81
+ ? [ spotlightIntegration ( { sidecarUrl : typeof options . spotlight === 'string' ? options . spotlight : undefined } ) ]
82
+ : [ ] ) ,
80
83
// We only add performance integrations if tracing is enabled
81
84
// Note that this means that without tracing enabled, e.g. `expressIntegration()` will not be added
82
85
// This means that generally request isolation will work (because that is done by httpIntegration)
@@ -88,64 +91,37 @@ export function getDefaultIntegrations(options: Options): Integration[] {
88
91
/**
89
92
* Initialize Sentry for Node.
90
93
*/
91
- export function init ( options : NodeOptions | undefined = { } ) : NodeClient | undefined {
94
+ export function init ( options : NodeOptions = { } ) : NodeClient | undefined {
92
95
return _init ( options , getDefaultIntegrations ) ;
93
96
}
94
97
95
98
/**
96
99
* Initialize Sentry for Node, without any integrations added by default.
97
100
*/
98
- export function initWithoutDefaultIntegrations ( options : NodeOptions | undefined = { } ) : NodeClient {
101
+ export function initWithoutDefaultIntegrations ( options : NodeOptions = { } ) : NodeClient {
99
102
return _init ( options , ( ) => [ ] ) ;
100
103
}
101
104
102
105
/**
103
- * Initialize Sentry for Node, without performance instrumentation.
106
+ * Initialize a Node client with the provided options and default integrations getter function.
107
+ * This is an internal method the SDK uses under the hood to set up things - you should not use this as a user!
108
+ * Instead, use `init()` to initialize the SDK.
109
+ *
110
+ * @hidden
111
+ * @internal
104
112
*/
105
113
function _init (
106
- _options : NodeOptions | undefined = { } ,
107
- getDefaultIntegrationsImpl : ( options : Options ) => Integration [ ] ,
114
+ options : NodeOptions = { } ,
115
+ getDefaultIntegrationsImpl : ( options : NodeOptions ) => Integration [ ] ,
108
116
) : NodeClient {
109
- const options = getClientOptions ( _options , getDefaultIntegrationsImpl ) ;
110
-
111
- if ( options . debug === true ) {
112
- if ( DEBUG_BUILD ) {
113
- logger . enable ( ) ;
114
- } else {
115
- // use `console.warn` rather than `logger.warn` since by non-debug bundles have all `logger.x` statements stripped
116
- consoleSandbox ( ( ) => {
117
- // eslint-disable-next-line no-console
118
- console . warn ( '[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle.' ) ;
119
- } ) ;
120
- }
121
- }
122
-
123
117
if ( ! isCjs ( ) && options . registerEsmLoaderHooks !== false ) {
124
118
maybeInitializeEsmLoader ( ) ;
125
119
}
126
120
127
121
setOpenTelemetryContextAsyncContextStrategy ( ) ;
128
122
129
- const scope = getCurrentScope ( ) ;
130
- scope . update ( options . initialScope ) ;
131
-
132
- if ( options . spotlight && ! options . integrations . some ( ( { name } ) => name === SPOTLIGHT_INTEGRATION_NAME ) ) {
133
- options . integrations . push (
134
- spotlightIntegration ( {
135
- sidecarUrl : typeof options . spotlight === 'string' ? options . spotlight : undefined ,
136
- } ) ,
137
- ) ;
138
- }
139
-
140
- const client = new NodeClient ( options ) ;
141
- // The client is on the current scope, from where it generally is inherited
142
- getCurrentScope ( ) . setClient ( client ) ;
143
-
144
- client . init ( ) ;
145
-
146
- logger . log ( `Running in ${ isCjs ( ) ? 'CommonJS' : 'ESM' } mode.` ) ;
147
-
148
- client . startClientReportTracking ( ) ;
123
+ const clientOptions = getClientOptions ( options , getDefaultIntegrationsImpl ) ;
124
+ const client = initAndBind ( NodeClient , clientOptions ) ;
149
125
150
126
updateScopeFromEnvVariables ( ) ;
151
127
@@ -197,65 +173,29 @@ export function validateOpenTelemetrySetup(): void {
197
173
198
174
function getClientOptions (
199
175
options : NodeOptions ,
200
- getDefaultIntegrationsImpl : ( options : Options ) => Integration [ ] ,
176
+ getDefaultIntegrationsImpl : ( options : NodeOptions ) => Integration [ ] ,
201
177
) : NodeClientOptions {
202
- const release = getRelease ( options . release ) ;
203
- const spotlight =
204
- options . spotlight ?? envToBool ( process . env . SENTRY_SPOTLIGHT , { strict : true } ) ?? process . env . SENTRY_SPOTLIGHT ;
205
- const tracesSampleRate = getTracesSampleRate ( options . tracesSampleRate ) ;
206
-
207
- const mergedOptions = {
178
+ // We need to make sure to extract the tracesSampleRate already here, before we pass it to `getDefaultIntegrationsImpl`
179
+ // As otherwise, the check for `hasSpansEnabled` may not work in all scenarios
180
+ const optionsWithTracesSampleRate = {
208
181
...options ,
209
- dsn : options . dsn ?? process . env . SENTRY_DSN ,
210
- environment : options . environment ?? process . env . SENTRY_ENVIRONMENT ,
211
- sendClientReports : options . sendClientReports ?? true ,
212
- transport : options . transport ?? makeNodeTransport ,
213
- stackParser : stackParserFromStackParserOptions ( options . stackParser || defaultStackParser ) ,
214
- release,
215
- tracesSampleRate,
216
- spotlight,
217
- debug : envToBool ( options . debug ?? process . env . SENTRY_DEBUG ) ,
182
+ tracesSampleRate : getTracesSampleRate ( options . tracesSampleRate ) ,
218
183
} ;
219
184
220
185
const integrations = options . integrations ;
221
- const defaultIntegrations = options . defaultIntegrations ?? getDefaultIntegrationsImpl ( mergedOptions ) ;
186
+ const defaultIntegrations = options . defaultIntegrations ?? getDefaultIntegrationsImpl ( optionsWithTracesSampleRate ) ;
222
187
223
188
return {
224
- ...mergedOptions ,
189
+ ...optionsWithTracesSampleRate ,
190
+ transport : options . transport ?? makeNodeTransport ,
191
+ stackParser : stackParserFromStackParserOptions ( options . stackParser || defaultStackParser ) ,
225
192
integrations : getIntegrationsToSetup ( {
226
193
defaultIntegrations,
227
194
integrations,
228
195
} ) ,
229
196
} ;
230
197
}
231
198
232
- function getRelease ( release : NodeOptions [ 'release' ] ) : string | undefined {
233
- if ( release !== undefined ) {
234
- return release ;
235
- }
236
-
237
- const detectedRelease = getSentryRelease ( ) ;
238
- if ( detectedRelease !== undefined ) {
239
- return detectedRelease ;
240
- }
241
-
242
- return undefined ;
243
- }
244
-
245
- function getTracesSampleRate ( tracesSampleRate : NodeOptions [ 'tracesSampleRate' ] ) : number | undefined {
246
- if ( tracesSampleRate !== undefined ) {
247
- return tracesSampleRate ;
248
- }
249
-
250
- const sampleRateFromEnv = process . env . SENTRY_TRACES_SAMPLE_RATE ;
251
- if ( ! sampleRateFromEnv ) {
252
- return undefined ;
253
- }
254
-
255
- const parsed = parseFloat ( sampleRateFromEnv ) ;
256
- return isFinite ( parsed ) ? parsed : undefined ;
257
- }
258
-
259
199
/**
260
200
* Update scope and propagation context based on environmental variables.
261
201
*
0 commit comments