-
Notifications
You must be signed in to change notification settings - Fork 43
Add heap stats logging to Service #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elrayle
merged 11 commits into
clearlydefined:master
from
harrider:users/harrider/add-heap-stats-logging
Dec 12, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cef82de
Add v8 package
harrider 084eb50
Add heapstats logging
harrider 87b065c
Add max heap size argument
harrider baff202
Add env vars to control heap stats logging
harrider 2c7f824
Control heap stats logging with env vars
harrider 237e1ce
Merge branch 'master' into users/harrider/add-heap-stats-logging
harrider f024bce
Refactor heap logging into dedicated module
harrider c100b62
Remove duplicate heap stat value
harrider d6dbbcb
Remove v8 npm package from package.json
harrider 4ff6126
Merge branch 'master' into users/harrider/add-heap-stats-logging
qtomlinson 574ed37
Update lib/heapLogger.js
elrayle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // (c) Copyright 2024, Microsoft and ClearlyDefined contributors. Licensed under the MIT license. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // =================================================== | ||
| // Log the heap statistics at regular intervals | ||
| // =================================================== | ||
| // 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 | ||
| // NOTE: To better understand heap stats being logged, check: | ||
| // - https://nodejs.org/docs/v22.12.0/api/v8.html#v8getheapspacestatistics | ||
| // - https://nodejs.org/docs/v22.12.0/api/v8.html#v8getheapstatistics | ||
| function trySetHeapLoggingAtInterval(config, logger) { | ||
| logger.debug('heapLogger.js :: Entered "trySetHeapLoggingAtInterval"...') | ||
|
|
||
| const shouldLogHeapstats = config.heapstats.logHeapstats | ||
| ? config.heapstats.logHeapstats.toLowerCase() === 'true' | ||
| : false | ||
|
|
||
| logger.debug(`heapLogger.js :: "shouldLogHeapstats" set to "${shouldLogHeapstats}"`) | ||
|
|
||
| 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 | ||
|
|
||
| logger.debug(`heapLogger.js :: heap stats logging interval will be "${heapStatsInverval}" ms`) | ||
|
|
||
| // 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 Size Executable: '${addCommas(heapStats.total_heap_size_executable)}' 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'))) { | ||
| logger.debug(`heapLogger.js :: setting heap stats logging at "${heapStatsInverval}" ms interval...`) | ||
|
|
||
| // Set the interval to log the heap space statistics | ||
| setInterval(logHeapSpaceStats, heapStatsInverval) | ||
|
|
||
| logger.debug(`heapLogger.js :: set heap stats logging at "${heapStatsInverval}" ms interval.`) | ||
| } | ||
| } else { | ||
| logger.debug('heapLogger.js :: heap stats logging not enabled.') | ||
| } | ||
|
|
||
| logger.debug('heapLogger.js :: Exiting "trySetHeapLoggingAtInterval".') | ||
| } | ||
|
|
||
| module.exports = trySetHeapLoggingAtInterval | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.