1+ /* eslint-disable max-lines */
12import { execFile } from 'node:child_process' ;
23import { readFile , readdir } from 'node:fs' ;
34import * as os from 'node:os' ;
@@ -18,6 +19,12 @@ import type {
1819export const readFileAsync = promisify ( readFile ) ;
1920export const readDirAsync = promisify ( readdir ) ;
2021
22+ // Process enhanced with methods from Node 18, 20, 22 as @types/node
23+ // is on `14.18.0` to match minimum version requirements of the SDK
24+ interface ProcessWithCurrentValues extends NodeJS . Process {
25+ availableMemory ?( ) : number ;
26+ }
27+
2128const INTEGRATION_NAME = 'Context' ;
2229
2330interface DeviceContextOptions {
@@ -114,10 +121,18 @@ export const nodeContextIntegration = defineIntegration(_nodeContextIntegration)
114121 */
115122function _updateContext ( contexts : Contexts ) : Contexts {
116123 // Only update properties if they exist
124+
117125 if ( contexts ?. app ?. app_memory ) {
118126 contexts . app . app_memory = process . memoryUsage ( ) . rss ;
119127 }
120128
129+ if ( contexts ?. app ?. free_memory && typeof ( process as ProcessWithCurrentValues ) . availableMemory === 'function' ) {
130+ const freeMemory = ( process as ProcessWithCurrentValues ) . availableMemory ?.( ) ;
131+ if ( freeMemory != null ) {
132+ contexts . app . free_memory = freeMemory ;
133+ }
134+ }
135+
121136 if ( contexts ?. device ?. free_memory ) {
122137 contexts . device . free_memory = os . freemem ( ) ;
123138 }
@@ -183,11 +198,23 @@ function getCultureContext(): CultureContext | undefined {
183198 return ;
184199}
185200
186- function getAppContext ( ) : AppContext {
201+ /**
202+ * Get app context information from process
203+ */
204+ export function getAppContext ( ) : AppContext {
187205 const app_memory = process . memoryUsage ( ) . rss ;
188206 const app_start_time = new Date ( Date . now ( ) - process . uptime ( ) * 1000 ) . toISOString ( ) ;
207+ // https://nodejs.org/api/process.html#processavailablememory
208+ const appContext : AppContext = { app_start_time, app_memory } ;
209+
210+ if ( typeof ( process as ProcessWithCurrentValues ) . availableMemory === 'function' ) {
211+ const freeMemory = ( process as ProcessWithCurrentValues ) . availableMemory ?.( ) ;
212+ if ( freeMemory != null ) {
213+ appContext . free_memory = freeMemory ;
214+ }
215+ }
189216
190- return { app_start_time , app_memory } ;
217+ return appContext ;
191218}
192219
193220/**
0 commit comments