Skip to content

Commit

Permalink
Control heap stats logging with env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
harrider committed Nov 20, 2024
1 parent baff202 commit 2c7f824
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,53 +101,66 @@ function createApp(config) {
)

// ===================================================
// Log the heap space statistics at regular intervals
// Log the heap statistics at regular intervals
// ===================================================
const v8 = require('v8')
const heapStatsInverval = 10000; // 10 seconds

// Function to add commas to a number
const addCommas = (num) => Number(num).toLocaleString();

// Function to log the heap space statistics
const logHeapSpaceStats = () => {
// Get the current timestamp
const currentTimestamp = new Date().toISOString();

// Get the heap space statistics
const heapSpaceStats = v8.getHeapSpaceStatistics();

heapSpaceStats.forEach((space) => {
const heapStatsMessage = `[${currentTimestamp}] Heap Space Statistics: `
+ `Space Name: '${space.space_name}', `
+ `Space Size: '${addCommas(space.space_size)}' bytes, `
+ `Space Used Size: '${addCommas(space.space_used_size)}' bytes, `
+ `Space Available Size: '${addCommas(space.space_available_size)}' bytes, `
+ `Physical Space Size: '${addCommas(space.physical_space_size)}' bytes`
+ '\n--------------------------';

logger.info(heapStatsMessage);
});

// Get the heap statistics
const heapStats = v8.getHeapStatistics();

const heapStatsMessage = `[${currentTimestamp}] Heap Statistics: `
+ `Total Heap Size: '${addCommas(heapStats.total_heap_size)}' bytes, `
+ `Total Heap Used Size: '${addCommas(heapStats.total_heap_size)}' bytes, `
+ `Total Physical Size: '${addCommas(heapStats.total_physical_size)}' bytes, `
+ `Total Available Size: '${addCommas(heapStats.total_available_size)}' bytes, `
+ `Used Heap Size: '${addCommas(heapStats.used_heap_size)}' bytes, `
+ `Heap Size Limit: '${addCommas(heapStats.heap_size_limit)}' bytes`
+ '\n--------------------------';

logger.info(heapStatsMessage);
};

// Only run if not in a test environment
if (process.argv.every(arg => !arg.includes('mocha'))) {
// Set the interval to log the heap space statistics
setInterval(logHeapSpaceStats, heapStatsInverval);
// NOTE: set 'LOG_NODE_HEAPSTATS' env var to 'true' to log heap stats
// NOTE: set 'LOG_NODE_HEAPSTATS_INTERVAL_MS' env var to '<time_in_milliseconds>' for logging interval
const shouldLogHeapstats = config.heapstats.logHeapstats
? config.heapstats.logHeapstats.toLowerCase() === 'true'
: false

if (shouldLogHeapstats) {
const v8 = require('v8')

const addCommas = num => Number(num).toLocaleString()
const isNumeric = num => !isNaN(Number(num))

// Set the heapstats logging interval
const maybeInterval = config.heapstats.logInverval
const heapStatsInverval = maybeInterval && isNumeric(maybeInterval) ? maybeInterval : 30000

// Function to log the heap space statistics
const logHeapSpaceStats = () => {
// Get the current timestamp
const currentTimestamp = new Date().toISOString()

// Get the heap space statistics
const heapSpaceStats = v8.getHeapSpaceStatistics()

heapSpaceStats.forEach(space => {
const heapStatsMessage =
`[${currentTimestamp}] Heap Space Statistics: ` +
`Space Name: '${space.space_name}', ` +
`Space Size: '${addCommas(space.space_size)}' bytes, ` +
`Space Used Size: '${addCommas(space.space_used_size)}' bytes, ` +
`Space Available Size: '${addCommas(space.space_available_size)}' bytes, ` +
`Physical Space Size: '${addCommas(space.physical_space_size)}' bytes` +
'\n--------------------------'

logger.info(heapStatsMessage)
})

// Get the heap statistics
const heapStats = v8.getHeapStatistics()

const heapStatsMessage =
`[${currentTimestamp}] Heap Statistics: ` +
`Total Heap Size: '${addCommas(heapStats.total_heap_size)}' bytes, ` +
`Total Heap Used Size: '${addCommas(heapStats.total_heap_size)}' bytes, ` +
`Total Physical Size: '${addCommas(heapStats.total_physical_size)}' bytes, ` +
`Total Available Size: '${addCommas(heapStats.total_available_size)}' bytes, ` +
`Used Heap Size: '${addCommas(heapStats.used_heap_size)}' bytes, ` +
`Heap Size Limit: '${addCommas(heapStats.heap_size_limit)}' bytes` +
'\n--------------------------'

logger.info(heapStatsMessage)
}

// Only run if not in a test environment
if (process.argv.every(arg => !arg.includes('mocha'))) {
// Set the interval to log the heap space statistics
setInterval(logHeapSpaceStats, heapStatsInverval)
}
}
// ===================================================

Expand Down

0 comments on commit 2c7f824

Please sign in to comment.