Skip to content

Commit

Permalink
fix: enable bun.js by catching NotImplemented error
Browse files Browse the repository at this point in the history
  • Loading branch information
owlcode committed Apr 2, 2024
1 parent cf5e074 commit acba9d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Breaking

### Changed
- Enable `bun.js` by catching `NotImplemented` error thrown in `perf_hooks.monitorEventLoopDelay`

### Added

Expand Down
49 changes: 26 additions & 23 deletions lib/metrics/eventLoopLag.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,35 @@ module.exports = (registry, config = {}) => {
const labelNames = Object.keys(labels);
const registers = registry ? [registry] : undefined;

let collect;
if (!perf_hooks || !perf_hooks.monitorEventLoopDelay) {
collect = () => {
const start = process.hrtime();
setImmediate(reportEventloopLag, start, lag, labels);
};
} else {
const histogram = perf_hooks.monitorEventLoopDelay({
resolution: config.eventLoopMonitoringPrecision,
});
histogram.enable();
let collect = () => {
const start = process.hrtime();
setImmediate(reportEventloopLag, start, lag, labels);
};

collect = () => {
const start = process.hrtime();
setImmediate(reportEventloopLag, start, lag, labels);
if (perf_hooks && perf_hooks.monitorEventLoopDelay) {
try {
const histogram = perf_hooks.monitorEventLoopDelay({
resolution: config.eventLoopMonitoringPrecision,
});
histogram.enable();

lagMin.set(labels, histogram.min / 1e9);
lagMax.set(labels, histogram.max / 1e9);
lagMean.set(labels, histogram.mean / 1e9);
lagStddev.set(labels, histogram.stddev / 1e9);
lagP50.set(labels, histogram.percentile(50) / 1e9);
lagP90.set(labels, histogram.percentile(90) / 1e9);
lagP99.set(labels, histogram.percentile(99) / 1e9);
collect = () => {
const start = process.hrtime();
setImmediate(reportEventloopLag, start, lag, labels);

histogram.reset();
};
lagMin.set(labels, histogram.min / 1e9);
lagMax.set(labels, histogram.max / 1e9);
lagMean.set(labels, histogram.mean / 1e9);
lagStddev.set(labels, histogram.stddev / 1e9);
lagP50.set(labels, histogram.percentile(50) / 1e9);
lagP90.set(labels, histogram.percentile(90) / 1e9);
lagP99.set(labels, histogram.percentile(99) / 1e9);

histogram.reset();
};
} catch {
// bun has `perf_hooks.monitorEventLoopDelay` defined but throws "NotImplemented" when called
}
}

const lag = new Gauge({
Expand Down

0 comments on commit acba9d6

Please sign in to comment.