From 2194d354a322987a8e9c3739fb4d9981bde8de37 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Tue, 2 Apr 2024 21:40:55 +0200 Subject: [PATCH 1/4] fix: enable bun.js by catching NotImplemented error --- CHANGELOG.md | 1 + lib/metrics/eventLoopLag.js | 49 +++++++++++++++------------- lib/metrics/heapSpacesSizeAndUsed.js | 5 +++ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83d0a70c..2e247886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/metrics/eventLoopLag.js b/lib/metrics/eventLoopLag.js index 45d5c33d..7acbbcfc 100644 --- a/lib/metrics/eventLoopLag.js +++ b/lib/metrics/eventLoopLag.js @@ -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({ diff --git a/lib/metrics/heapSpacesSizeAndUsed.js b/lib/metrics/heapSpacesSizeAndUsed.js index 472be21c..58aa6962 100644 --- a/lib/metrics/heapSpacesSizeAndUsed.js +++ b/lib/metrics/heapSpacesSizeAndUsed.js @@ -11,6 +11,11 @@ METRICS.forEach(metricType => { }); module.exports = (registry, config = {}) => { + try { + v8.getHeapSpaceStatistics(); + } catch { + return; // Bun + } const registers = registry ? [registry] : undefined; const namePrefix = config.prefix ? config.prefix : ''; From 8676c29fe4a25a3ca673a7604cdd33a649ea8092 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Wed, 3 Apr 2024 10:45:11 +0200 Subject: [PATCH 2/4] fix: rethrow error other than bun's "not_implemented" --- lib/metrics/eventLoopLag.js | 8 ++++++-- lib/metrics/heapSpacesSizeAndUsed.js | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/metrics/eventLoopLag.js b/lib/metrics/eventLoopLag.js index 7acbbcfc..86f73d71 100644 --- a/lib/metrics/eventLoopLag.js +++ b/lib/metrics/eventLoopLag.js @@ -63,8 +63,12 @@ module.exports = (registry, config = {}) => { histogram.reset(); }; - } catch { - // bun has `perf_hooks.monitorEventLoopDelay` defined but throws "NotImplemented" when called + } catch (e) { + if (e.code === 'ERR_NOT_IMPLEMENTED') { + return; // Bun + } + + throw e; } } diff --git a/lib/metrics/heapSpacesSizeAndUsed.js b/lib/metrics/heapSpacesSizeAndUsed.js index 58aa6962..9519a3b2 100644 --- a/lib/metrics/heapSpacesSizeAndUsed.js +++ b/lib/metrics/heapSpacesSizeAndUsed.js @@ -13,8 +13,11 @@ METRICS.forEach(metricType => { module.exports = (registry, config = {}) => { try { v8.getHeapSpaceStatistics(); - } catch { - return; // Bun + } catch (e) { + if (e.code === 'ERR_NOT_IMPLEMENTED') { + return; // Bun + } + throw e; } const registers = registry ? [registry] : undefined; const namePrefix = config.prefix ? config.prefix : ''; From f1af3af71ca9d45fe959db8130a59a9de94cc9f8 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Wed, 3 Apr 2024 10:51:34 +0200 Subject: [PATCH 3/4] fix: update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e247886..bbb8a624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Breaking ### Changed -- Enable `bun.js` by catching `NotImplemented` error thrown in `perf_hooks.monitorEventLoopDelay` + +- Enable `bun.js` by catching `NotImplemented` error (Fixes [#570](https://github.com/siimon/prom-client/issues/570)) ### Added From 2aa1d8f58ea36441182c53987cf4cdd77e9fad9c Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Wed, 3 Apr 2024 11:22:02 +0200 Subject: [PATCH 4/4] ci: add bun CI --- .editorconfig | 4 ++++ .github/workflows/{nodejs.yml => ci.yml} | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) rename .github/workflows/{nodejs.yml => ci.yml} (77%) diff --git a/.editorconfig b/.editorconfig index 648cc36e..1ef11c63 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/.github/workflows/nodejs.yml b/.github/workflows/ci.yml similarity index 77% rename from .github/workflows/nodejs.yml rename to .github/workflows/ci.yml index c1f8182b..4d7736c2 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: Node.js CI +name: CI on: push: @@ -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 @@ -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