Skip to content

Commit

Permalink
fix: enable bun.js by catching NotImplemented error (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
owlcode committed Apr 8, 2024
1 parent cf5e074 commit 99b25a4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
16 changes: 14 additions & 2 deletions .github/workflows/nodejs.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node.js CI
name: CI

on:
push:
Expand All @@ -11,7 +11,7 @@ on:
- '**'

jobs:
build:
nodejs:
name: Test on Node.js v${{ matrix.node-version }} and OS ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -40,3 +40,15 @@ jobs:
run: npm i
- name: Test
run: npm run test
bun:
name: Test on Bun
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install Dependencies
run: bun install
- name: Test
run: bun test-unit
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Enable `bun.js` by catching `NotImplemented` error (Fixes [#570](https://github.com/siimon/prom-client/issues/570))

### Added

[unreleased]: https://github.com/siimon/prom-client/compare/v15.1.0...HEAD
Expand Down
53 changes: 30 additions & 23 deletions lib/metrics/eventLoopLag.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,39 @@ 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 (e) {
if (e.code === 'ERR_NOT_IMPLEMENTED') {
return; // Bun
}

throw e;
}
}

const lag = new Gauge({
Expand Down
8 changes: 8 additions & 0 deletions lib/metrics/heapSpacesSizeAndUsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ METRICS.forEach(metricType => {
});

module.exports = (registry, config = {}) => {
try {
v8.getHeapSpaceStatistics();
} catch (e) {
if (e.code === 'ERR_NOT_IMPLEMENTED') {
return; // Bun
}
throw e;
}
const registers = registry ? [registry] : undefined;
const namePrefix = config.prefix ? config.prefix : '';

Expand Down

0 comments on commit 99b25a4

Please sign in to comment.