1- import type { Integration , Options } from '@sentry/core' ;
1+ import type { Integration } from '@sentry/core' ;
22import {
33 consoleIntegration ,
4- consoleSandbox ,
54 functionToStringIntegration ,
65 getCurrentScope ,
76 getIntegrationsToSetup ,
87 hasSpansEnabled ,
98 inboundFiltersIntegration ,
9+ initAndBind ,
1010 linkedErrorsIntegration ,
1111 logger ,
1212 propagationContextFromHeaders ,
@@ -30,14 +30,14 @@ import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
3030import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception' ;
3131import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection' ;
3232import { processSessionIntegration } from '../integrations/processSession' ;
33- import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME , spotlightIntegration } from '../integrations/spotlight' ;
33+ import { spotlightIntegration } from '../integrations/spotlight' ;
3434import { getAutoPerformanceIntegrations } from '../integrations/tracing' ;
3535import { makeNodeTransport } from '../transports' ;
3636import type { NodeClientOptions , NodeOptions } from '../types' ;
3737import { isCjs } from '../utils/commonjs' ;
3838import { 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' ;
4141import { initOpenTelemetry , maybeInitializeEsmLoader } from './initOtel' ;
4242
4343function getCjsOnlyIntegrations ( ) : Integration [ ] {
@@ -74,9 +74,12 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
7474}
7575
7676/** Get the default integrations for the Node SDK. */
77- export function getDefaultIntegrations ( options : Options ) : Integration [ ] {
77+ export function getDefaultIntegrations ( options : NodeOptions ) : Integration [ ] {
7878 return [
7979 ...getDefaultIntegrationsWithoutPerformance ( ) ,
80+ ...( options . spotlight
81+ ? [ spotlightIntegration ( { sidecarUrl : typeof options . spotlight === 'string' ? options . spotlight : undefined } ) ]
82+ : [ ] ) ,
8083 // We only add performance integrations if tracing is enabled
8184 // Note that this means that without tracing enabled, e.g. `expressIntegration()` will not be added
8285 // This means that generally request isolation will work (because that is done by httpIntegration)
@@ -88,64 +91,37 @@ export function getDefaultIntegrations(options: Options): Integration[] {
8891/**
8992 * Initialize Sentry for Node.
9093 */
91- export function init ( options : NodeOptions | undefined = { } ) : NodeClient | undefined {
94+ export function init ( options : NodeOptions = { } ) : NodeClient | undefined {
9295 return _init ( options , getDefaultIntegrations ) ;
9396}
9497
9598/**
9699 * Initialize Sentry for Node, without any integrations added by default.
97100 */
98- export function initWithoutDefaultIntegrations ( options : NodeOptions | undefined = { } ) : NodeClient {
101+ export function initWithoutDefaultIntegrations ( options : NodeOptions = { } ) : NodeClient {
99102 return _init ( options , ( ) => [ ] ) ;
100103}
101104
102105/**
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
104112 */
105113function _init (
106- _options : NodeOptions | undefined = { } ,
107- getDefaultIntegrationsImpl : ( options : Options ) => Integration [ ] ,
114+ options : NodeOptions = { } ,
115+ getDefaultIntegrationsImpl : ( options : NodeOptions ) => Integration [ ] ,
108116) : 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-
123117 if ( ! isCjs ( ) && options . registerEsmLoaderHooks !== false ) {
124118 maybeInitializeEsmLoader ( ) ;
125119 }
126120
127121 setOpenTelemetryContextAsyncContextStrategy ( ) ;
128122
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 ) ;
149125
150126 updateScopeFromEnvVariables ( ) ;
151127
@@ -197,65 +173,29 @@ export function validateOpenTelemetrySetup(): void {
197173
198174function getClientOptions (
199175 options : NodeOptions ,
200- getDefaultIntegrationsImpl : ( options : Options ) => Integration [ ] ,
176+ getDefaultIntegrationsImpl : ( options : NodeOptions ) => Integration [ ] ,
201177) : 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 = {
208181 ...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 ) ,
218183 } ;
219184
220185 const integrations = options . integrations ;
221- const defaultIntegrations = options . defaultIntegrations ?? getDefaultIntegrationsImpl ( mergedOptions ) ;
186+ const defaultIntegrations = options . defaultIntegrations ?? getDefaultIntegrationsImpl ( optionsWithTracesSampleRate ) ;
222187
223188 return {
224- ...mergedOptions ,
189+ ...optionsWithTracesSampleRate ,
190+ transport : options . transport ?? makeNodeTransport ,
191+ stackParser : stackParserFromStackParserOptions ( options . stackParser || defaultStackParser ) ,
225192 integrations : getIntegrationsToSetup ( {
226193 defaultIntegrations,
227194 integrations,
228195 } ) ,
229196 } ;
230197}
231198
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-
259199/**
260200 * Update scope and propagation context based on environmental variables.
261201 *
0 commit comments