11import { getCurrentHub , initAndBind , Integrations as CoreIntegrations } from '@sentry/core' ;
22import { getMainCarrier , setHubOnCarrier } from '@sentry/hub' ;
3+ import { SessionStatus } from '@sentry/types' ;
34import { getGlobalObject } from '@sentry/utils' ;
45import * as domain from 'domain' ;
56
@@ -96,9 +97,16 @@ export function init(options: NodeOptions = {}): void {
9697 const detectedRelease = getSentryRelease ( ) ;
9798 if ( detectedRelease !== undefined ) {
9899 options . release = detectedRelease ;
100+ } else {
101+ // If release is not provided, then we should disable autoSessionTracking
102+ options . autoSessionTracking = false ;
99103 }
100104 }
101105
106+ if ( options . autoSessionTracking === undefined ) {
107+ options . autoSessionTracking = true ;
108+ }
109+
102110 if ( options . environment === undefined && process . env . SENTRY_ENVIRONMENT ) {
103111 options . environment = process . env . SENTRY_ENVIRONMENT ;
104112 }
@@ -109,6 +117,10 @@ export function init(options: NodeOptions = {}): void {
109117 }
110118
111119 initAndBind ( NodeClient , options ) ;
120+
121+ if ( options . autoSessionTracking ) {
122+ startSessionTracking ( ) ;
123+ }
112124}
113125
114126/**
@@ -148,6 +160,20 @@ export async function close(timeout?: number): Promise<boolean> {
148160 return Promise . reject ( false ) ;
149161}
150162
163+ /**
164+ * Function that takes an instance of NodeClient and checks if autoSessionTracking option is enabled for that client
165+ */
166+ export function isAutoSessionTrackingEnabled ( client ?: NodeClient ) : boolean {
167+ if ( client === undefined ) {
168+ return false ;
169+ }
170+ const clientOptions : NodeOptions = client && client . getOptions ( ) ;
171+ if ( clientOptions && clientOptions . autoSessionTracking !== undefined ) {
172+ return clientOptions . autoSessionTracking ;
173+ }
174+ return false ;
175+ }
176+
151177/**
152178 * Returns a release dynamically from environment variables.
153179 */
@@ -180,3 +206,22 @@ export function getSentryRelease(fallback?: string): string | undefined {
180206 fallback
181207 ) ;
182208}
209+
210+ /**
211+ * Enable automatic Session Tracking for the node process.
212+ */
213+ function startSessionTracking ( ) : void {
214+ const hub = getCurrentHub ( ) ;
215+ hub . startSession ( ) ;
216+ // Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
217+ // The 'beforeExit' event is not emitted for conditions causing explicit termination,
218+ // such as calling process.exit() or uncaught exceptions.
219+ // Ref: https://nodejs.org/api/process.html#process_event_beforeexit
220+ process . on ( 'beforeExit' , ( ) => {
221+ const session = hub . getScope ( ) ?. getSession ( ) ;
222+ const terminalStates = [ SessionStatus . Exited , SessionStatus . Crashed ] ;
223+ // Only call endSession, if the Session exists on Scope and SessionStatus is not a
224+ // Terminal Status i.e. Exited or Crashed
225+ if ( session && ! terminalStates . includes ( session . status ) ) hub . endSession ( ) ;
226+ } ) ;
227+ }
0 commit comments