From cc0fad989fc75295e1086022d2267a3cf7bdf3dc Mon Sep 17 00:00:00 2001 From: William Killerud Date: Thu, 7 Mar 2024 14:01:08 +0100 Subject: [PATCH] chore: update devdependencies --- .eslintrc | 25 +- .github/workflows/release.yml | 64 ++--- .github/workflows/test.yml | 40 +-- .gitignore | 1 + .prettierrc | 15 + lib/collector-active-handles-total.js | 52 ++-- lib/collector-active-requests-total.js | 52 ++-- lib/collector-cpu-total.js | 106 ++++--- lib/collector-eventloop-lag.js | 56 ++-- lib/collector-gc.js | 101 +++---- lib/collector-heap-used-and-size.js | 112 ++++---- lib/collector-max-file-descriptors.js | 106 +++---- lib/collector-open-file-descriptors.js | 64 +++-- lib/collector-process-resident-memory.js | 219 +++++++-------- lib/collector-process-start-time.js | 66 ++--- lib/collector-v8-heap.js | 114 ++++---- lib/collector-version.js | 102 +++---- lib/process.js | 325 ++++++++++----------- package.json | 97 +++---- release.config.js | 50 ++-- test/collector-active-handles-total.js | 92 +++--- test/collector-active-requests-total.js | 92 +++--- test/collector-cpu-total.js | 198 +++++++------ test/collector-eventloop-lag.js | 89 +++--- test/collector-gc.js | 115 ++++---- test/collector-heap-used-and-size.js | 206 +++++++------- test/collector-max-file-descriptors.js | 115 ++++---- test/collector-open-file-descriptors.js | 101 ++++--- test/collector-process-resident-memory.js | 328 +++++++++++++--------- test/collector-process-start-time.js | 90 +++--- test/collector-v8-heap.js | 257 +++++++++-------- test/collector-version.js | 121 ++++---- test/process.js | 62 ++-- 33 files changed, 1915 insertions(+), 1718 deletions(-) create mode 100644 .prettierrc diff --git a/.eslintrc b/.eslintrc index a3c550c..f085dd3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,21 +1,10 @@ { - "env": { - "es6": true, - "node": true - }, - "extends": "airbnb-base", - "rules": { - "strict": [0, "global"], - "prefer-const": 1, - "indent": [1, 4], - "class-methods-use-this": [0], - "import/no-extraneous-dependencies": [0], - "arrow-body-style": [0, "always"], - "no-multiple-empty-lines": [0], - "no-underscore-dangle": [0], - "comma-dangle": [0], - "no-plusplus": [0], - "no-console": [0], - "new-cap": [0] + "extends": ["eslint:recommended", "plugin:prettier/recommended"], + "plugins": ["prettier"], + "env": { "es6": true, "node": true }, + "parserOptions": { + "requireConfigFile": false, + "ecmaVersion": 2020, + "sourceType": "module" } } diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 670c0b5..6c3e157 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,42 +1,42 @@ name: Release on main permissions: - contents: write # to be able to publish a GitHub release - issues: write # to be able to comment on released issues - pull-requests: write # to be able to comment on released pull requests - id-token: write # to enable use of OIDC for npm provenance + contents: write # to be able to publish a GitHub release + issues: write # to be able to comment on released issues + pull-requests: write # to be able to comment on released pull requests + id-token: write # to enable use of OIDC for npm provenance on: - push: - branches: - - main + push: + branches: + - main # Cancel previous workflows which might still be running concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} jobs: - release: - name: Release - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Install Node.js - uses: actions/setup-node@v3 - with: - node-version: 20 - - - name: Install dependencies - run: npm install - - - name: Run all tests - run: npm test - - - name: Release and publish - run: npx semantic-release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm install + + - name: Run all tests + run: npm test + + - name: Release and publish + run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b65fd35..ce06135 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,29 +1,29 @@ name: Run Lint and Tests on: - pull_request: - branches: [main] + pull_request: + branches: [main] jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - os: [ubuntu-latest, macOS-latest, windows-latest] - node-version: [10.x, 18.x, 20.x] - steps: - - uses: actions/checkout@v3 + build: + runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest, macOS-latest, windows-latest] + node-version: [18.x, 20.x] + steps: + - uses: actions/checkout@v4 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} - - name: Install dependencies - run: npm install + - name: Install dependencies + run: npm install - - name: Lint - run: npm run lint + - name: Lint + run: npm run lint - - name: Run tests - run: npm test + - name: Run tests + run: npm test diff --git a/.gitignore b/.gitignore index 6f4acfe..e9208cc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ tmp/**/* *.log package-lock.json .nyc_output +.tap diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..9f74261 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,15 @@ +{ + "singleQuote": false, + "trailingComma": "all", + "useTabs": true, + "printWidth": 120, + "overrides": [ + { + "files": ["*.yml"], + "options": { + "tabWidth": 2, + "useTabs": false + } + } + ] +} diff --git a/lib/collector-active-handles-total.js b/lib/collector-active-handles-total.js index f9f2a88..69ca41b 100644 --- a/lib/collector-active-handles-total.js +++ b/lib/collector-active-handles-total.js @@ -1,35 +1,37 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorActiveHandlesTotal = class CollectorActiveHandlesTotal { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); - Object.defineProperty(this, 'collectable', { - value: (typeof process._getActiveHandles === 'function'), - }); - } + Object.defineProperty(this, "collectable", { + value: typeof process._getActiveHandles === "function", + }); + } - get [Symbol.toStringTag]() { - return 'CollectorActiveHandlesTotal'; - } + get [Symbol.toStringTag]() { + return "CollectorActiveHandlesTotal"; + } - collect(timestamp) { - if (!this.collectable) { - return null; - } + collect(timestamp) { + if (!this.collectable) { + return null; + } - return [new Metric({ - name: `${this.prefix}nodejs_active_handles_total`, - description: 'Total number of handles', - type: 1, - value: process._getActiveHandles().length, - timestamp, - })]; - } + return [ + new Metric({ + name: `${this.prefix}nodejs_active_handles_total`, + description: "Total number of handles", + type: 1, + value: process._getActiveHandles().length, + timestamp, + }), + ]; + } }; module.exports = CollectorActiveHandlesTotal; diff --git a/lib/collector-active-requests-total.js b/lib/collector-active-requests-total.js index 5678a17..9a0344f 100644 --- a/lib/collector-active-requests-total.js +++ b/lib/collector-active-requests-total.js @@ -1,35 +1,37 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorActiveRequestsTotal = class CollectorActiveRequestsTotal { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); - Object.defineProperty(this, 'collectable', { - value: (typeof process._getActiveRequests === 'function'), - }); - } + Object.defineProperty(this, "collectable", { + value: typeof process._getActiveRequests === "function", + }); + } - get [Symbol.toStringTag]() { - return 'CollectorActiveRequestsTotal'; - } + get [Symbol.toStringTag]() { + return "CollectorActiveRequestsTotal"; + } - collect(timestamp) { - if (!this.collectable) { - return null; - } + collect(timestamp) { + if (!this.collectable) { + return null; + } - return [new Metric({ - name: `${this.prefix}nodejs_active_requests_total`, - description: 'Total number of active requests', - type: 1, - value: process._getActiveRequests().length, - timestamp, - })]; - } + return [ + new Metric({ + name: `${this.prefix}nodejs_active_requests_total`, + description: "Total number of active requests", + type: 1, + value: process._getActiveRequests().length, + timestamp, + }), + ]; + } }; module.exports = CollectorActiveRequestsTotal; diff --git a/lib/collector-cpu-total.js b/lib/collector-cpu-total.js index 45cef9f..9cc0678 100644 --- a/lib/collector-cpu-total.js +++ b/lib/collector-cpu-total.js @@ -1,61 +1,57 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorCpuTotal = class CollectorCpuTotal { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - - Object.defineProperty(this, 'previousSample', { - writable: true, - value: process.cpuUsage(), - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorCpuTotal'; - } - - collect(timestamp) { - const sample = process.cpuUsage(); - - const userUsageMicros = sample.user - this.previousSample.user; - const systemUsageMicros = sample.system - this.previousSample.system; - - this.previousSample = sample; - - const cpuUserUsageCounter = new Metric({ - name: `${this.prefix}process_cpu_user_seconds_total`, - description: 'Total user CPU time spent in seconds', - type: 2, - value: userUsageMicros / 1e6, - timestamp, - }); - - const cpuSystemUsageCounter = new Metric({ - name: `${this.prefix}process_cpu_system_seconds_total`, - description: 'Total system CPU time spent in seconds', - type: 2, - value: systemUsageMicros / 1e6, - timestamp, - }); - - const cpuUsageCounter = new Metric({ - name: `${this.prefix}process_cpu_seconds_total`, - description: 'Total user and system CPU time spent in seconds', - type: 2, - value: (userUsageMicros + systemUsageMicros) / 1e6, - timestamp, - }); - - return [ - cpuUserUsageCounter, - cpuSystemUsageCounter, - cpuUsageCounter, - ]; - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + + Object.defineProperty(this, "previousSample", { + writable: true, + value: process.cpuUsage(), + }); + } + + get [Symbol.toStringTag]() { + return "CollectorCpuTotal"; + } + + collect(timestamp) { + const sample = process.cpuUsage(); + + const userUsageMicros = sample.user - this.previousSample.user; + const systemUsageMicros = sample.system - this.previousSample.system; + + this.previousSample = sample; + + const cpuUserUsageCounter = new Metric({ + name: `${this.prefix}process_cpu_user_seconds_total`, + description: "Total user CPU time spent in seconds", + type: 2, + value: userUsageMicros / 1e6, + timestamp, + }); + + const cpuSystemUsageCounter = new Metric({ + name: `${this.prefix}process_cpu_system_seconds_total`, + description: "Total system CPU time spent in seconds", + type: 2, + value: systemUsageMicros / 1e6, + timestamp, + }); + + const cpuUsageCounter = new Metric({ + name: `${this.prefix}process_cpu_seconds_total`, + description: "Total user and system CPU time spent in seconds", + type: 2, + value: (userUsageMicros + systemUsageMicros) / 1e6, + timestamp, + }); + + return [cpuUserUsageCounter, cpuSystemUsageCounter, cpuUsageCounter]; + } }; module.exports = CollectorCpuTotal; diff --git a/lib/collector-eventloop-lag.js b/lib/collector-eventloop-lag.js index 9cf556f..fa3af3e 100644 --- a/lib/collector-eventloop-lag.js +++ b/lib/collector-eventloop-lag.js @@ -1,37 +1,39 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorEventloopLag = class CollectorEventloopLag { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + } - get [Symbol.toStringTag]() { - return 'CollectorEventloopLag'; - } + get [Symbol.toStringTag]() { + return "CollectorEventloopLag"; + } - collect(timestamp) { - const start = process.hrtime(); + collect(timestamp) { + const start = process.hrtime(); - return new Promise((resolve) => { - setImmediate(() => { - const delta = process.hrtime(start); - const nanosec = delta[0] * 1e9 + delta[1]; - const seconds = nanosec / 1e9; + return new Promise((resolve) => { + setImmediate(() => { + const delta = process.hrtime(start); + const nanosec = delta[0] * 1e9 + delta[1]; + const seconds = nanosec / 1e9; - resolve([new Metric({ - name: `${this.prefix}nodejs_eventloop_lag_seconds`, - description: 'Lag of eventloop in seconds', - type: 1, - value: seconds, - timestamp, - })]); - }); - }); - } + resolve([ + new Metric({ + name: `${this.prefix}nodejs_eventloop_lag_seconds`, + description: "Lag of eventloop in seconds", + type: 1, + value: seconds, + timestamp, + }), + ]); + }); + }); + } }; module.exports = CollectorEventloopLag; diff --git a/lib/collector-gc.js b/lib/collector-gc.js index e8150a0..8452a48 100644 --- a/lib/collector-gc.js +++ b/lib/collector-gc.js @@ -1,61 +1,52 @@ -'use strict'; +"use strict"; -const EventEmitter = require('events'); -const perfHooks = require('perf_hooks'); -const Metric = require('@metrics/metric'); +const EventEmitter = require("events"); +const perfHooks = require("perf_hooks"); +const Metric = require("@metrics/metric"); const CollectorGC = class CollectorGC extends EventEmitter { - constructor(prefix = '') { - super(); - - const dictionary = new Map([ - [perfHooks.constants.NODE_PERFORMANCE_GC_MAJOR, 'major'], - [perfHooks.constants.NODE_PERFORMANCE_GC_MINOR, 'minor'], - [perfHooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL, 'incremental'], - [perfHooks.constants.NODE_PERFORMANCE_GC_WEAKCB, 'weakcb'], - ]); - - const buckets = [ - 0.001, - 0.01, - 0.1, - 1, - 2, - 5 - ]; - - Object.defineProperty(this, 'observer', { - value: new perfHooks.PerformanceObserver((list) => { - const entry = list.getEntries()[0]; - const metric = new Metric({ - name: `${prefix}nodejs_gc_duration_seconds`, - description: 'Garbage collection duration by kind, one of "major", "minor", "incremental" or "weakcb"', - type: 5, - labels: [ - { name: 'kind', value: dictionary.get(entry.kind) }, - ], - timestamp: Date.now() / 1000, - value: entry.duration / 1000, - meta: { - buckets, - }, - }); - this.emit('metric', metric); - }), - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorGC'; - } - - start() { - this.observer.observe({ entryTypes: ['gc'], buffered: false }); - } - - stop() { - this.observer.disconnect(); - } + constructor(prefix = "") { + super(); + + const dictionary = new Map([ + [perfHooks.constants.NODE_PERFORMANCE_GC_MAJOR, "major"], + [perfHooks.constants.NODE_PERFORMANCE_GC_MINOR, "minor"], + [perfHooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL, "incremental"], + [perfHooks.constants.NODE_PERFORMANCE_GC_WEAKCB, "weakcb"], + ]); + + const buckets = [0.001, 0.01, 0.1, 1, 2, 5]; + + Object.defineProperty(this, "observer", { + value: new perfHooks.PerformanceObserver((list) => { + const entry = list.getEntries()[0]; + const metric = new Metric({ + name: `${prefix}nodejs_gc_duration_seconds`, + description: 'Garbage collection duration by kind, one of "major", "minor", "incremental" or "weakcb"', + type: 5, + labels: [{ name: "kind", value: dictionary.get(entry.kind) }], + timestamp: Date.now() / 1000, + value: entry.duration / 1000, + meta: { + buckets, + }, + }); + this.emit("metric", metric); + }), + }); + } + + get [Symbol.toStringTag]() { + return "CollectorGC"; + } + + start() { + this.observer.observe({ entryTypes: ["gc"], buffered: false }); + } + + stop() { + this.observer.disconnect(); + } }; module.exports = CollectorGC; diff --git a/lib/collector-heap-used-and-size.js b/lib/collector-heap-used-and-size.js index f6c9b38..a940dcb 100644 --- a/lib/collector-heap-used-and-size.js +++ b/lib/collector-heap-used-and-size.js @@ -1,64 +1,60 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorHeapUsedAndSize = class CollectorHeapUsedAndSize { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorHeapUsedAndSize'; - } - - collect(timestamp) { - const mem = this.constructor.getMemoryUsage(); - if (!mem) { - return null; - } - - const heapSizeTotal = new Metric({ - name: `${this.prefix}nodejs_heap_size_total_bytes`, - description: 'Process heap size from node.js in bytes', - type: 1, - value: mem.heapTotal, - timestamp, - }); - - const heapSizeUsed = new Metric({ - name: `${this.prefix}nodejs_heap_size_used_bytes`, - description: 'Process heap size used from node.js in bytes', - type: 1, - value: mem.heapUsed, - timestamp, - }); - - const externalMemUsed = new Metric({ - name: `${this.prefix}nodejs_external_memory_bytes`, - description: 'Nodejs external memory size in bytes', - type: 1, - value: mem.external, - timestamp, - }); - - return [ - heapSizeTotal, - heapSizeUsed, - externalMemUsed, - ]; - } - - static getMemoryUsage() { - let mem = null; - try { - mem = process.memoryUsage(); - } catch (ex) { // eslint-disable-line no-empty - - } - return mem; - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + } + + get [Symbol.toStringTag]() { + return "CollectorHeapUsedAndSize"; + } + + collect(timestamp) { + const mem = this.constructor.getMemoryUsage(); + if (!mem) { + return null; + } + + const heapSizeTotal = new Metric({ + name: `${this.prefix}nodejs_heap_size_total_bytes`, + description: "Process heap size from node.js in bytes", + type: 1, + value: mem.heapTotal, + timestamp, + }); + + const heapSizeUsed = new Metric({ + name: `${this.prefix}nodejs_heap_size_used_bytes`, + description: "Process heap size used from node.js in bytes", + type: 1, + value: mem.heapUsed, + timestamp, + }); + + const externalMemUsed = new Metric({ + name: `${this.prefix}nodejs_external_memory_bytes`, + description: "Nodejs external memory size in bytes", + type: 1, + value: mem.external, + timestamp, + }); + + return [heapSizeTotal, heapSizeUsed, externalMemUsed]; + } + + static getMemoryUsage() { + let mem = null; + try { + mem = process.memoryUsage(); + } catch (ex) { + // eslint-disable-line no-empty + } + return mem; + } }; module.exports = CollectorHeapUsedAndSize; diff --git a/lib/collector-max-file-descriptors.js b/lib/collector-max-file-descriptors.js index 5ddf92c..327d5fc 100644 --- a/lib/collector-max-file-descriptors.js +++ b/lib/collector-max-file-descriptors.js @@ -1,68 +1,70 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); -const fs = require('fs'); +const Metric = require("@metrics/metric"); +const fs = require("fs"); const CollectorMaxFileDescriptors = class CollectorMaxFileDescriptors { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); - Object.defineProperty(this, 'collected', { - writable: true, - value: false, - }); - } + Object.defineProperty(this, "collected", { + writable: true, + value: false, + }); + } - get [Symbol.toStringTag]() { - return 'CollectorMaxFileDescriptors'; - } + get [Symbol.toStringTag]() { + return "CollectorMaxFileDescriptors"; + } - collect(timestamp) { - if (process.platform !== 'linux') { - return null; - } + collect(timestamp) { + if (process.platform !== "linux") { + return null; + } - if (this.collected) { - return null; - } + if (this.collected) { + return null; + } - return new Promise((resolve) => { - fs.readFile('/proc/self/limits', 'utf8', (error, limits) => { - if (error) { - resolve(null); - return; - } + return new Promise((resolve) => { + fs.readFile("/proc/self/limits", "utf8", (error, limits) => { + if (error) { + resolve(null); + return; + } - const lines = limits.split('\n'); + const lines = limits.split("\n"); - let result = ''; - lines.find((line) => { - if (line.startsWith('Max open files')) { - [, result] = line.split(/ +/); - return true; - } - return false; - }); + let result = ""; + lines.find((line) => { + if (line.startsWith("Max open files")) { + [, result] = line.split(/ +/); + return true; + } + return false; + }); - if (result === '') { - resolve(null); - return; - } + if (result === "") { + resolve(null); + return; + } - resolve([new Metric({ - name: `${this.prefix}process_max_fds`, - description: 'Maximum number of open file descriptors', - type: 1, - value: parseInt(result, 10), - timestamp, - })]); + resolve([ + new Metric({ + name: `${this.prefix}process_max_fds`, + description: "Maximum number of open file descriptors", + type: 1, + value: parseInt(result, 10), + timestamp, + }), + ]); - this.collected = true; - }); - }); - } + this.collected = true; + }); + }); + } }; module.exports = CollectorMaxFileDescriptors; diff --git a/lib/collector-open-file-descriptors.js b/lib/collector-open-file-descriptors.js index c0d9ae2..be9ee13 100644 --- a/lib/collector-open-file-descriptors.js +++ b/lib/collector-open-file-descriptors.js @@ -1,41 +1,43 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); -const fs = require('fs'); +const Metric = require("@metrics/metric"); +const fs = require("fs"); const CollectorOpenFileDescriptors = class CollectorOpenFileDescriptors { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + } - get [Symbol.toStringTag]() { - return 'CollectorOpenFileDescriptors'; - } + get [Symbol.toStringTag]() { + return "CollectorOpenFileDescriptors"; + } - collect(timestamp) { - if (process.platform !== 'linux') { - return null; - } + collect(timestamp) { + if (process.platform !== "linux") { + return null; + } - return new Promise((resolve) => { - fs.readdir('/proc/self/fd', (error, result) => { - if (error) { - resolve(null); - return; - } + return new Promise((resolve) => { + fs.readdir("/proc/self/fd", (error, result) => { + if (error) { + resolve(null); + return; + } - resolve([new Metric({ - name: `${this.prefix}process_open_fds`, - description: 'Number of open file descriptors', - type: 1, - value: result.length - 1, - timestamp, - })]); - }); - }); - } + resolve([ + new Metric({ + name: `${this.prefix}process_open_fds`, + description: "Number of open file descriptors", + type: 1, + value: result.length - 1, + timestamp, + }), + ]); + }); + }); + } }; module.exports = CollectorOpenFileDescriptors; diff --git a/lib/collector-process-resident-memory.js b/lib/collector-process-resident-memory.js index 4dc288f..c7f3d08 100644 --- a/lib/collector-process-resident-memory.js +++ b/lib/collector-process-resident-memory.js @@ -1,117 +1,116 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); -const fs = require('fs'); +const Metric = require("@metrics/metric"); +const fs = require("fs"); -const generic = Symbol('generic'); -const linux = Symbol('linux'); +const generic = Symbol("generic"); +const linux = Symbol("linux"); const CollectorProcessResidentMemory = class CollectorProcessResidentMemory { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorProcessResidentMemory'; - } - - [generic](timestamp) { - const mem = this.constructor.getMemoryUsage(); - if (mem) { - return [new Metric({ - name: `${this.prefix}process_resident_memory_bytes`, - description: 'Resident memory size in bytes', - type: 1, - value: mem.rss, - timestamp, - })]; - } - return null; - } - - [linux](timestamp) { - return new Promise((resolve) => { - fs.readFile('/proc/self/status', 'utf8', (error, file) => { - if (error) { - resolve(null); - return; - } - - const mem = this.constructor.parseFile(file); - - const residentMemory = new Metric({ - name: `${this.prefix}process_resident_memory_bytes`, - description: 'Resident memory size in bytes', - type: 1, - value: mem.VmRSS, - timestamp, - }); - - const virtualMemory = new Metric({ - name: `${this.prefix}process_virtual_memory_bytes`, - description: 'Virtual memory size in bytes', - type: 1, - value: mem.VmSize, - timestamp, - }); - - const heapSizeMemory = new Metric({ - name: `${this.prefix}process_heap_bytes`, - description: 'Process heap size in bytes', - type: 1, - value: mem.VmData, - timestamp, - }); - - resolve([ - residentMemory, - virtualMemory, - heapSizeMemory, - ]); - }); - }); - } - - collect(timestamp) { - if (process.platform === 'linux') { - return this[linux](timestamp); - } - return this[generic](timestamp); - } - - static getMemoryUsage() { - let mem; - try { - mem = process.memoryUsage(); - } catch (ex) { // eslint-disable-line no-empty - - } - return mem; - } - - static parseFile(file) { - const metrics = {}; - const values = ['VmSize', 'VmRSS', 'VmData']; - - file.split('\n') - .filter((s) => values.some((value) => s.indexOf(value) === 0)) - .forEach((string) => { - const split = string.split(':'); - - // Get the value - let value = split[1].trim(); - // Remove trailing ` kb` - value = value.substr(0, value.length - 3); - // Make it into a number in bytes bytes - value = Number(value) * 1024; - - metrics[split[0]] = value; - }); - - return metrics; - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + } + + get [Symbol.toStringTag]() { + return "CollectorProcessResidentMemory"; + } + + [generic](timestamp) { + const mem = this.constructor.getMemoryUsage(); + if (mem) { + return [ + new Metric({ + name: `${this.prefix}process_resident_memory_bytes`, + description: "Resident memory size in bytes", + type: 1, + value: mem.rss, + timestamp, + }), + ]; + } + return null; + } + + [linux](timestamp) { + return new Promise((resolve) => { + fs.readFile("/proc/self/status", "utf8", (error, file) => { + if (error) { + resolve(null); + return; + } + + const mem = this.constructor.parseFile(file); + + const residentMemory = new Metric({ + name: `${this.prefix}process_resident_memory_bytes`, + description: "Resident memory size in bytes", + type: 1, + value: mem.VmRSS, + timestamp, + }); + + const virtualMemory = new Metric({ + name: `${this.prefix}process_virtual_memory_bytes`, + description: "Virtual memory size in bytes", + type: 1, + value: mem.VmSize, + timestamp, + }); + + const heapSizeMemory = new Metric({ + name: `${this.prefix}process_heap_bytes`, + description: "Process heap size in bytes", + type: 1, + value: mem.VmData, + timestamp, + }); + + resolve([residentMemory, virtualMemory, heapSizeMemory]); + }); + }); + } + + collect(timestamp) { + if (process.platform === "linux") { + return this[linux](timestamp); + } + return this[generic](timestamp); + } + + static getMemoryUsage() { + let mem; + try { + mem = process.memoryUsage(); + } catch (ex) { + // eslint-disable-line no-empty + } + return mem; + } + + static parseFile(file) { + const metrics = {}; + const values = ["VmSize", "VmRSS", "VmData"]; + + file + .split("\n") + .filter((s) => values.some((value) => s.indexOf(value) === 0)) + .forEach((string) => { + const split = string.split(":"); + + // Get the value + let value = split[1].trim(); + // Remove trailing ` kb` + value = value.substr(0, value.length - 3); + // Make it into a number in bytes bytes + value = Number(value) * 1024; + + metrics[split[0]] = value; + }); + + return metrics; + } }; module.exports = CollectorProcessResidentMemory; diff --git a/lib/collector-process-start-time.js b/lib/collector-process-start-time.js index 921dff1..f701035 100644 --- a/lib/collector-process-start-time.js +++ b/lib/collector-process-start-time.js @@ -1,40 +1,42 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const START_TIME = Math.round(Date.now() / 1000 - process.uptime()); const CollectorProcessStartTime = class CollectorProcessStartTime { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - - Object.defineProperty(this, 'collected', { - writable: true, - value: false, - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorProcessStartTime'; - } - - collect(timestamp) { - if (this.collected) { - return null; - } - - this.collected = true; - - return [new Metric({ - name: `${this.prefix}process_start_time_seconds`, - description: 'Start time of the process since unix epoch in seconds.', - type: 1, - value: START_TIME, - timestamp, - })]; - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + + Object.defineProperty(this, "collected", { + writable: true, + value: false, + }); + } + + get [Symbol.toStringTag]() { + return "CollectorProcessStartTime"; + } + + collect(timestamp) { + if (this.collected) { + return null; + } + + this.collected = true; + + return [ + new Metric({ + name: `${this.prefix}process_start_time_seconds`, + description: "Start time of the process since unix epoch in seconds.", + type: 1, + value: START_TIME, + timestamp, + }), + ]; + } }; module.exports = CollectorProcessStartTime; diff --git a/lib/collector-v8-heap.js b/lib/collector-v8-heap.js index 2e502b0..780f991 100644 --- a/lib/collector-v8-heap.js +++ b/lib/collector-v8-heap.js @@ -1,64 +1,76 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); -const v8 = require('v8'); +const Metric = require("@metrics/metric"); +const v8 = require("v8"); const CollectorV8Heap = class CollectorV8Heap { - constructor(prefix = '') { - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - } + constructor(prefix = "") { + Object.defineProperty(this, "prefix", { + value: prefix, + }); + } - get [Symbol.toStringTag]() { - return 'CollectorV8Heap'; - } + get [Symbol.toStringTag]() { + return "CollectorV8Heap"; + } - collect(timestamp) { - const metrics = []; + collect(timestamp) { + const metrics = []; - v8.getHeapSpaceStatistics().forEach((space) => { - const name = space.space_name.substr(0, space.space_name.indexOf('_space')); + v8.getHeapSpaceStatistics().forEach((space) => { + const name = space.space_name.substr(0, space.space_name.indexOf("_space")); - metrics.push(new Metric({ - name: `${this.prefix}nodejs_heap_space_size_total_bytes`, - description: 'Process heap space size total from node.js in bytes', - type: 1, - labels: [{ - name: 'space', - value: name, - }], - timestamp, - value: space.space_size, - })); + metrics.push( + new Metric({ + name: `${this.prefix}nodejs_heap_space_size_total_bytes`, + description: "Process heap space size total from node.js in bytes", + type: 1, + labels: [ + { + name: "space", + value: name, + }, + ], + timestamp, + value: space.space_size, + }), + ); - metrics.push(new Metric({ - name: `${this.prefix}nodejs_heap_space_size_used_bytes`, - description: 'Process heap space size used from node.js in bytes', - type: 1, - labels: [{ - name: 'space', - value: name, - }], - timestamp, - value: space.space_used_size, - })); + metrics.push( + new Metric({ + name: `${this.prefix}nodejs_heap_space_size_used_bytes`, + description: "Process heap space size used from node.js in bytes", + type: 1, + labels: [ + { + name: "space", + value: name, + }, + ], + timestamp, + value: space.space_used_size, + }), + ); - metrics.push(new Metric({ - name: `${this.prefix}nodejs_heap_space_size_available_bytes`, - description: 'Process heap space size available from node.js in bytes', - type: 1, - labels: [{ - name: 'space', - value: name, - }], - timestamp, - value: space.space_available_size, - })); - }); + metrics.push( + new Metric({ + name: `${this.prefix}nodejs_heap_space_size_available_bytes`, + description: "Process heap space size available from node.js in bytes", + type: 1, + labels: [ + { + name: "space", + value: name, + }, + ], + timestamp, + value: space.space_available_size, + }), + ); + }); - return metrics; - } + return metrics; + } }; module.exports = CollectorV8Heap; diff --git a/lib/collector-version.js b/lib/collector-version.js index 7223eda..9e00588 100644 --- a/lib/collector-version.js +++ b/lib/collector-version.js @@ -1,56 +1,58 @@ -'use strict'; +"use strict"; -const Metric = require('@metrics/metric'); +const Metric = require("@metrics/metric"); const CollectorVersion = class CollectorVersion { - constructor(prefix = '') { - const { version } = process; - const segments = version - .slice(1) - .split('.') - .map((value) => { - return parseInt(value, 10); - }); - - Object.defineProperty(this, 'segments', { - value: [version].concat(segments), - }); - - Object.defineProperty(this, 'prefix', { - value: prefix, - }); - - Object.defineProperty(this, 'collected', { - writable: true, - value: false, - }); - } - - get [Symbol.toStringTag]() { - return 'CollectorVersion'; - } - - collect(timestamp) { - if (this.collected) { - return null; - } - - this.collected = true; - - return [new Metric({ - name: `${this.prefix}nodejs_version_info`, - description: 'Node.js version info', - type: 1, - labels: [ - { name: 'version', value: this.segments[0] }, - { name: 'major', value: this.segments[1] }, - { name: 'minor', value: this.segments[2] }, - { name: 'patch', value: this.segments[3] }, - ], - value: 1, - timestamp, - })]; - } + constructor(prefix = "") { + const { version } = process; + const segments = version + .slice(1) + .split(".") + .map((value) => { + return parseInt(value, 10); + }); + + Object.defineProperty(this, "segments", { + value: [version].concat(segments), + }); + + Object.defineProperty(this, "prefix", { + value: prefix, + }); + + Object.defineProperty(this, "collected", { + writable: true, + value: false, + }); + } + + get [Symbol.toStringTag]() { + return "CollectorVersion"; + } + + collect(timestamp) { + if (this.collected) { + return null; + } + + this.collected = true; + + return [ + new Metric({ + name: `${this.prefix}nodejs_version_info`, + description: "Node.js version info", + type: 1, + labels: [ + { name: "version", value: this.segments[0] }, + { name: "major", value: this.segments[1] }, + { name: "minor", value: this.segments[2] }, + { name: "patch", value: this.segments[3] }, + ], + value: 1, + timestamp, + }), + ]; + } }; module.exports = CollectorVersion; diff --git a/lib/process.js b/lib/process.js index 4013b8e..f310ee3 100644 --- a/lib/process.js +++ b/lib/process.js @@ -1,166 +1,169 @@ -'use strict'; - -const Interval = require('deferred-interval'); -const stream = require('readable-stream'); - -const CollectorVER = require('./collector-version'); -const CollectorMFD = require('./collector-max-file-descriptors'); -const CollectorOFD = require('./collector-open-file-descriptors'); -const CollectorELL = require('./collector-eventloop-lag'); -const CollectorPST = require('./collector-process-start-time'); -const CollectorART = require('./collector-active-requests-total'); -const CollectorAHT = require('./collector-active-handles-total'); -const CollectorCPU = require('./collector-cpu-total'); -const CollectorHUS = require('./collector-heap-used-and-size'); -const CollectorPRM = require('./collector-process-resident-memory'); -const CollectorV8H = require('./collector-v8-heap'); -const CollectorGC = require('./collector-gc'); - -const collect = Symbol('collect'); +"use strict"; + +const Interval = require("deferred-interval"); +const stream = require("readable-stream"); + +const CollectorVER = require("./collector-version"); +const CollectorMFD = require("./collector-max-file-descriptors"); +const CollectorOFD = require("./collector-open-file-descriptors"); +const CollectorELL = require("./collector-eventloop-lag"); +const CollectorPST = require("./collector-process-start-time"); +const CollectorART = require("./collector-active-requests-total"); +const CollectorAHT = require("./collector-active-handles-total"); +const CollectorCPU = require("./collector-cpu-total"); +const CollectorHUS = require("./collector-heap-used-and-size"); +const CollectorPRM = require("./collector-process-resident-memory"); +const CollectorV8H = require("./collector-v8-heap"); +const CollectorGC = require("./collector-gc"); + +const collect = Symbol("collect"); const MetricsProcess = class MetricsProcess extends stream.Readable { - constructor({ - prefix = '', - interval = 10000, - } = {}) { - super({ objectMode: true, }); - - Object.defineProperty(this, 'scheduler', { - value: new Interval(), - }); - - Object.defineProperty(this, 'interval', { - value: interval, - }); - - Object.defineProperty(this, 'collectorVER', { - value: new CollectorVER(prefix), - }); - - Object.defineProperty(this, 'collectorMFD', { - value: new CollectorMFD(prefix), - }); - - Object.defineProperty(this, 'collectorOFD', { - value: new CollectorOFD(prefix), - }); - - Object.defineProperty(this, 'collectorELL', { - value: new CollectorELL(prefix), - }); - - Object.defineProperty(this, 'collectorPST', { - value: new CollectorPST(prefix), - }); - - Object.defineProperty(this, 'collectorART', { - value: new CollectorART(prefix), - }); - - Object.defineProperty(this, 'collectorAHT', { - value: new CollectorAHT(prefix), - }); - - Object.defineProperty(this, 'collectorCPU', { - value: new CollectorCPU(prefix), - }); - - Object.defineProperty(this, 'collectorHUS', { - value: new CollectorHUS(prefix), - }); - - Object.defineProperty(this, 'collectorPRM', { - value: new CollectorPRM(prefix), - }); - - Object.defineProperty(this, 'collectorV8H', { - value: new CollectorV8H(prefix), - }); - - Object.defineProperty(this, 'collectorGC', { - value: new CollectorGC(prefix), - }); - - this.collectorGC.on('metric', (metric) => { - if (this._readableState.flowing) { - this.push(metric); - return; - } - this.emit('drop', metric); - }); - } - - get [Symbol.toStringTag]() { - return 'MetricsProcess'; - } - - [collect]() { - const now = Date.now() / 1000; - return Promise.all([ - this.collectorVER.collect(now), - this.collectorMFD.collect(now), - this.collectorOFD.collect(now), - this.collectorELL.collect(now), - this.collectorPST.collect(now), - this.collectorART.collect(now), - this.collectorAHT.collect(now), - this.collectorCPU.collect(now), - this.collectorHUS.collect(now), - this.collectorPRM.collect(now), - this.collectorV8H.collect(now), - ]); - } - - start(options = {}) { - const o = { - ver: true, - mfd: true, - ofd: true, - ell: true, - pst: true, - art: true, - aht: true, - cpu: true, - hus: true, - prm: true, - v8h: true, - gc: false, - ...options - }; - - this.scheduler.start(async (done) => { - this.emit('collect:start'); - - const results = await this[collect](o); - results.filter((result) => { - return Array.isArray(result); - }).forEach((result) => { - result.forEach((metric) => { - if (this._readableState.flowing) { - this.push(metric); - return; - } - this.emit('drop', metric); - }); - }); - - this.emit('collect:end'); - done(); - }, this.interval, true); - - if (o.gc) { - this.collectorGC.start(); - } - } - - stop() { - this.scheduler.stop(); - this.collectorGC.stop(); - } - - _read() { - // nothiong to do, push happens in scheduler - } + constructor({ prefix = "", interval = 10000 } = {}) { + super({ objectMode: true }); + + Object.defineProperty(this, "scheduler", { + value: new Interval(), + }); + + Object.defineProperty(this, "interval", { + value: interval, + }); + + Object.defineProperty(this, "collectorVER", { + value: new CollectorVER(prefix), + }); + + Object.defineProperty(this, "collectorMFD", { + value: new CollectorMFD(prefix), + }); + + Object.defineProperty(this, "collectorOFD", { + value: new CollectorOFD(prefix), + }); + + Object.defineProperty(this, "collectorELL", { + value: new CollectorELL(prefix), + }); + + Object.defineProperty(this, "collectorPST", { + value: new CollectorPST(prefix), + }); + + Object.defineProperty(this, "collectorART", { + value: new CollectorART(prefix), + }); + + Object.defineProperty(this, "collectorAHT", { + value: new CollectorAHT(prefix), + }); + + Object.defineProperty(this, "collectorCPU", { + value: new CollectorCPU(prefix), + }); + + Object.defineProperty(this, "collectorHUS", { + value: new CollectorHUS(prefix), + }); + + Object.defineProperty(this, "collectorPRM", { + value: new CollectorPRM(prefix), + }); + + Object.defineProperty(this, "collectorV8H", { + value: new CollectorV8H(prefix), + }); + + Object.defineProperty(this, "collectorGC", { + value: new CollectorGC(prefix), + }); + + this.collectorGC.on("metric", (metric) => { + if (this._readableState.flowing) { + this.push(metric); + return; + } + this.emit("drop", metric); + }); + } + + get [Symbol.toStringTag]() { + return "MetricsProcess"; + } + + [collect]() { + const now = Date.now() / 1000; + return Promise.all([ + this.collectorVER.collect(now), + this.collectorMFD.collect(now), + this.collectorOFD.collect(now), + this.collectorELL.collect(now), + this.collectorPST.collect(now), + this.collectorART.collect(now), + this.collectorAHT.collect(now), + this.collectorCPU.collect(now), + this.collectorHUS.collect(now), + this.collectorPRM.collect(now), + this.collectorV8H.collect(now), + ]); + } + + start(options = {}) { + const o = { + ver: true, + mfd: true, + ofd: true, + ell: true, + pst: true, + art: true, + aht: true, + cpu: true, + hus: true, + prm: true, + v8h: true, + gc: false, + ...options, + }; + + this.scheduler.start( + async (done) => { + this.emit("collect:start"); + + const results = await this[collect](o); + results + .filter((result) => { + return Array.isArray(result); + }) + .forEach((result) => { + result.forEach((metric) => { + if (this._readableState.flowing) { + this.push(metric); + return; + } + this.emit("drop", metric); + }); + }); + + this.emit("collect:end"); + done(); + }, + this.interval, + true, + ); + + if (o.gc) { + this.collectorGC.start(); + } + } + + stop() { + this.scheduler.stop(); + this.collectorGC.stop(); + } + + _read() { + // nothiong to do, push happens in scheduler + } }; module.exports = MetricsProcess; diff --git a/package.json b/package.json index fc6ca1f..2453437 100644 --- a/package.json +++ b/package.json @@ -1,50 +1,51 @@ { - "name": "@metrics/process", - "version": "2.2.1", - "description": "Module for collecting different process and system metrics and providing them as a metric stream", - "main": "lib/process.js", - "files": [ - "lib" - ], - "scripts": { - "test": "tap test/*.js --expose-gc", - "lint": "eslint .", - "lint:fix": "eslint . --fix" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/metrics-js/process.git" - }, - "author": "Trygve Lie", - "license": "MIT", - "bugs": { - "url": "https://github.com/metrics-js/process/issues" - }, - "homepage": "https://github.com/metrics-js/process#readme", - "keywords": [ - "server-statistics", - "server-stats", - "statistics", - "metrics", - "monitoring", - "process" - ], - "dependencies": { - "@metrics/metric": "2.3.3", - "deferred-interval": "2.0.0", - "readable-stream": "3.6.2" - }, - "devDependencies": { - "@semantic-release/changelog": "6.0.3", - "@semantic-release/commit-analyzer": "11.1.0", - "@semantic-release/git": "10.0.1", - "@semantic-release/github": "9.2.6", - "@semantic-release/npm": "11.0.3", - "@semantic-release/release-notes-generator": "12.1.0", - "eslint": "6.8.0", - "eslint-config-airbnb-base": "14.0.0", - "eslint-plugin-import": "2.20.1", - "semantic-release": "22.0.12", - "tap": "14.11.0" - } + "name": "@metrics/process", + "version": "2.2.1", + "description": "Module for collecting different process and system metrics and providing them as a metric stream", + "main": "lib/process.js", + "files": [ + "lib" + ], + "scripts": { + "test": "tap test/*.js --expose-gc --reporter tap", + "lint": "eslint .", + "lint:fix": "eslint . --fix" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/metrics-js/process.git" + }, + "author": "Trygve Lie", + "license": "MIT", + "bugs": { + "url": "https://github.com/metrics-js/process/issues" + }, + "homepage": "https://github.com/metrics-js/process#readme", + "keywords": [ + "server-statistics", + "server-stats", + "statistics", + "metrics", + "monitoring", + "process" + ], + "dependencies": { + "@metrics/metric": "2.3.3", + "deferred-interval": "2.0.0", + "readable-stream": "3.6.2" + }, + "devDependencies": { + "@semantic-release/changelog": "6.0.3", + "@semantic-release/commit-analyzer": "11.1.0", + "@semantic-release/git": "10.0.1", + "@semantic-release/github": "9.2.6", + "@semantic-release/npm": "11.0.3", + "@semantic-release/release-notes-generator": "12.1.0", + "eslint": "8.57.0", + "eslint-config-prettier": "9.1.0", + "eslint-plugin-prettier": "5.1.3", + "prettier": "3.2.5", + "semantic-release": "23.0.2", + "tap": "14.11.0" + } } diff --git a/release.config.js b/release.config.js index c6a260d..5e2b5b4 100644 --- a/release.config.js +++ b/release.config.js @@ -1,27 +1,27 @@ module.exports = { - plugins: [ - '@semantic-release/commit-analyzer', - '@semantic-release/release-notes-generator', - '@semantic-release/changelog', - [ - '@semantic-release/npm', - { - tarballDir: 'release', - }, - ], - [ - '@semantic-release/github', - { - assets: 'release/*.tgz', - }, - ], - '@semantic-release/git', - ], - preset: 'angular', - branches: [ - { name: 'main' }, - { name: 'alpha', prerelease: true }, - { name: 'beta', prerelease: true }, - { name: 'next', prerelease: true }, - ], + plugins: [ + "@semantic-release/commit-analyzer", + "@semantic-release/release-notes-generator", + "@semantic-release/changelog", + [ + "@semantic-release/npm", + { + tarballDir: "release", + }, + ], + [ + "@semantic-release/github", + { + assets: "release/*.tgz", + }, + ], + "@semantic-release/git", + ], + preset: "angular", + branches: [ + { name: "main" }, + { name: "alpha", prerelease: true }, + { name: "beta", prerelease: true }, + { name: "next", prerelease: true }, + ], }; diff --git a/test/collector-active-handles-total.js b/test/collector-active-handles-total.js index 8da35ab..6982362 100644 --- a/test/collector-active-handles-total.js +++ b/test/collector-active-handles-total.js @@ -1,62 +1,68 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-active-handles-total'); +const Collector = require("../lib/collector-active-handles-total"); -tap.test('Constructor() - object type - should be CollectorActiveHandlesTotal', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorActiveHandlesTotal]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorActiveHandlesTotal", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorActiveHandlesTotal]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 1", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_active_requests_total"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'nodejs_active_handles_total'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_active_requests_total"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "nodejs_active_handles_total"); + t.end(); + }, +); -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_nodejs_active_handles_total'); - t.end(); -}); +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_nodejs_active_handles_total"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); tap.test('.collect() - "process._getActiveHandles" is not a function - should return "null"', (t) => { - const original = process._getActiveHandles; - process._getActiveHandles = null; + const original = process._getActiveHandles; + process._getActiveHandles = null; - const collector = new Collector(); - const result = collector.collect(); - t.true(result === null); + const collector = new Collector(); + const result = collector.collect(); + t.ok(result === null); - process._getActiveHandles = original; - t.end(); + process._getActiveHandles = original; + t.end(); }); diff --git a/test/collector-active-requests-total.js b/test/collector-active-requests-total.js index 049c6a8..747e007 100644 --- a/test/collector-active-requests-total.js +++ b/test/collector-active-requests-total.js @@ -1,62 +1,68 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-active-requests-total'); +const Collector = require("../lib/collector-active-requests-total"); -tap.test('Constructor() - object type - should be CollectorActiveRequestsTotal', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorActiveRequestsTotal]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorActiveRequestsTotal", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorActiveRequestsTotal]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 1", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_active_requests_total"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'nodejs_active_requests_total'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_active_requests_total"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "nodejs_active_requests_total"); + t.end(); + }, +); -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_nodejs_active_requests_total'); - t.end(); -}); +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_nodejs_active_requests_total"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); tap.test('.collect() - "process._getActiveRequests" is not a function - should return "null"', (t) => { - const original = process._getActiveRequests; - process._getActiveRequests = null; + const original = process._getActiveRequests; + process._getActiveRequests = null; - const collector = new Collector(); - const result = collector.collect(); - t.true(result === null); + const collector = new Collector(); + const result = collector.collect(); + t.ok(result === null); - process._getActiveRequests = original; - t.end(); + process._getActiveRequests = original; + t.end(); }); diff --git a/test/collector-cpu-total.js b/test/collector-cpu-total.js index c17e55e..f17afe0 100644 --- a/test/collector-cpu-total.js +++ b/test/collector-cpu-total.js @@ -1,119 +1,137 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-cpu-total'); +const Collector = require("../lib/collector-cpu-total"); -tap.test('Constructor() - object type - should be CollectorCpuTotal', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorCpuTotal]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorCpuTotal", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorCpuTotal]"); + t.end(); }); -tap.test('.collect() - call method - should return an object', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result, 'object'); - t.end(); +tap.test(".collect() - call method - should return an object", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result, "object"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 3', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 3); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 3", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 3); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should be an object where "name" is "process_cpu_user_seconds_total"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'process_cpu_user_seconds_total'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_process_cpu_user_seconds_total'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should be an object where "name" is "process_cpu_user_seconds_total"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "process_cpu_user_seconds_total"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_process_cpu_user_seconds_total"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 2', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 2); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 2); + t.end(); }); -tap.test('.collect() - call method - 2nd item in Array - should be an object where "name" is "process_cpu_system_seconds_total"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].name, 'process_cpu_system_seconds_total'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[1].name, 'foo_process_cpu_system_seconds_total'); - t.end(); -}); +tap.test( + '.collect() - call method - 2nd item in Array - should be an object where "name" is "process_cpu_system_seconds_total"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].name, "process_cpu_system_seconds_total"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[1].name, "foo_process_cpu_system_seconds_total"); + t.end(); + }, +); tap.test('.collect() - call method - 2nd item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.true(Number.isFinite(result[1].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.ok(Number.isFinite(result[1].value)); + t.end(); }); tap.test('.collect() - call method - 2nd item in Array - should return an object where "type" is 2', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].type, 2); - t.end(); -}); - -tap.test('.collect() - call method - 3rd item in Array - should be an object where "name" is "process_cpu_seconds_total"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].name, 'process_cpu_seconds_total'); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].type, 2); + t.end(); }); -tap.test('.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[2].name, 'foo_process_cpu_seconds_total'); - t.end(); -}); +tap.test( + '.collect() - call method - 3rd item in Array - should be an object where "name" is "process_cpu_seconds_total"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].name, "process_cpu_seconds_total"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[2].name, "foo_process_cpu_seconds_total"); + t.end(); + }, +); tap.test('.collect() - call method - 3rd item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.true(Number.isFinite(result[2].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.ok(Number.isFinite(result[2].value)); + t.end(); }); tap.test('.collect() - call method - 3rd item in Array - should return an object where "type" is 2', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].type, 2); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].type, 2); + t.end(); }); diff --git a/test/collector-eventloop-lag.js b/test/collector-eventloop-lag.js index 8196424..0d28588 100644 --- a/test/collector-eventloop-lag.js +++ b/test/collector-eventloop-lag.js @@ -1,50 +1,59 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-eventloop-lag'); +const Collector = require("../lib/collector-eventloop-lag"); -tap.test('Constructor() - object type - should be CollectorEventloopLag', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorEventloopLag]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorEventloopLag", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorEventloopLag]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 1", async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_eventloop_lag_seconds"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'nodejs_eventloop_lag_seconds'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[0].name, 'foo_nodejs_eventloop_lag_seconds'); - t.end(); -}); - -tap.test('.collect() - call method - 1st item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_eventloop_lag_seconds"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "nodejs_eventloop_lag_seconds"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[0].name, "foo_nodejs_eventloop_lag_seconds"); + t.end(); + }, +); + +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); diff --git a/test/collector-gc.js b/test/collector-gc.js index 9ef2606..4b30aeb 100644 --- a/test/collector-gc.js +++ b/test/collector-gc.js @@ -1,76 +1,83 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-gc'); +const Collector = require("../lib/collector-gc"); // NOTE: This test must be run with the "--expose-gc" argument // node --expose-gc collector-gc.js // Helper to trigger GC const gc = () => { - return new Promise((resolve, reject) => { - try { - if (global.gc) { - global.gc(); - } else { - reject(new Error('Not able to trigger a GC. Please run the test with the "--expose-gc" argument')); - } - } catch (error) { - reject(error); - } + return new Promise((resolve, reject) => { + try { + if (global.gc) { + global.gc(); + } else { + reject(new Error('Not able to trigger a GC. Please run the test with the "--expose-gc" argument')); + } + } catch (error) { + reject(error); + } - setTimeout(() => { - resolve(); - }, 40); - }); + setTimeout(() => { + resolve(); + }, 40); + }); }; -const BUCKETS = [ - 0.001, - 0.01, - 0.1, - 1, - 2, - 5 -]; -const KINDS = new Set(['major', 'minor', 'incremental', 'weakcb']); +const BUCKETS = [0.001, 0.01, 0.1, 1, 2, 5]; +const KINDS = new Set(["major", "minor", "incremental", "weakcb"]); -tap.test('Constructor()', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorGC]', 'Should be of object type [object CollectorGC]'); - t.type(collector.start, 'function', 'Should have a .start() function'); - t.type(collector.stop, 'function', 'Should have a .stop() function'); - t.end(); +tap.test("Constructor()", (t) => { + const collector = new Collector(); + t.equal( + Object.prototype.toString.call(collector), + "[object CollectorGC]", + "Should be of object type [object CollectorGC]", + ); + t.type(collector.start, "function", "Should have a .start() function"); + t.type(collector.stop, "function", "Should have a .stop() function"); + t.end(); }); -tap.test('Emitted metric object by GC', async (t) => { - const collector = new Collector(); - collector.on('metric', (metric) => { - t.equal(Object.prototype.toString.call(metric), '[object Metric]', 'Metric should be of type [object Metric]'); - t.equal(metric.name, 'nodejs_gc_duration_seconds', 'Metric should have ".name" property set to "nodejs_gc_duration_seconds"'); - t.equal(metric.type, 5, 'Metric should have ".type" property set to "5"'); - t.type(metric.value, 'number', 'Metric should have ".value" with a numeric value'); - t.equal(metric.labels[0].name, 'kind', 'Metric should have one label with ".name" set to "kind"'); - t.true(KINDS.has(metric.labels[0].value), 'Metric should have one label with ".value" set to "major", "minor", "incremental" or "weakcb"'); - t.same(metric.meta.buckets, BUCKETS, 'Metric should have a ".meta.buckets" property set with defined buckets'); - }); - collector.start(); +tap.test("Emitted metric object by GC", async (t) => { + const collector = new Collector(); + collector.on("metric", (metric) => { + t.equal(Object.prototype.toString.call(metric), "[object Metric]", "Metric should be of type [object Metric]"); + t.equal( + metric.name, + "nodejs_gc_duration_seconds", + 'Metric should have ".name" property set to "nodejs_gc_duration_seconds"', + ); + t.equal(metric.type, 5, 'Metric should have ".type" property set to "5"'); + t.type(metric.value, "number", 'Metric should have ".value" with a numeric value'); + t.equal(metric.labels[0].name, "kind", 'Metric should have one label with ".name" set to "kind"'); + t.ok( + KINDS.has(metric.labels[0].value), + 'Metric should have one label with ".value" set to "major", "minor", "incremental" or "weakcb"', + ); + t.same(metric.meta.buckets, BUCKETS, 'Metric should have a ".meta.buckets" property set with defined buckets'); + }); + collector.start(); - await gc(); + await gc(); - collector.stop(); + collector.stop(); }); - tap.test('"prefix" on constructor is set', async (t) => { - const collector = new Collector('foo_'); - collector.on('metric', (metric) => { - t.equal(metric.name, 'foo_nodejs_gc_duration_seconds', 'Metric should have prepended the prefix to the ".name" property'); - }); - collector.start(); + const collector = new Collector("foo_"); + collector.on("metric", (metric) => { + t.equal( + metric.name, + "foo_nodejs_gc_duration_seconds", + 'Metric should have prepended the prefix to the ".name" property', + ); + }); + collector.start(); - await gc(); + await gc(); - collector.stop(); + collector.stop(); }); diff --git a/test/collector-heap-used-and-size.js b/test/collector-heap-used-and-size.js index fed2b7d..2cac6c8 100644 --- a/test/collector-heap-used-and-size.js +++ b/test/collector-heap-used-and-size.js @@ -1,126 +1,144 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-heap-used-and-size'); +const Collector = require("../lib/collector-heap-used-and-size"); -tap.test('Constructor() - object type - should be CollectorHeapUsedAndSize', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorHeapUsedAndSize]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorHeapUsedAndSize", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorHeapUsedAndSize]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 3', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 3); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 3", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 3); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should be an object where "name" is "nodejs_heap_size_total_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'nodejs_heap_size_total_bytes'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_nodejs_heap_size_total_bytes'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should be an object where "name" is "nodejs_heap_size_total_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "nodejs_heap_size_total_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_nodejs_heap_size_total_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); -tap.test('.collect() - call method - 2nd item in Array - should be an object where "name" is "nodejs_heap_size_used_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].name, 'nodejs_heap_size_used_bytes'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[1].name, 'foo_nodejs_heap_size_used_bytes'); - t.end(); -}); +tap.test( + '.collect() - call method - 2nd item in Array - should be an object where "name" is "nodejs_heap_size_used_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].name, "nodejs_heap_size_used_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[1].name, "foo_nodejs_heap_size_used_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 2nd item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.true(Number.isFinite(result[1].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.ok(Number.isFinite(result[1].value)); + t.end(); }); tap.test('.collect() - call method - 2nd item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].type, 1); - t.end(); -}); - -tap.test('.collect() - call method - 3rd item in Array - should be an object where "name" is "nodejs_external_memory_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].name, 'nodejs_external_memory_bytes'); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].type, 1); + t.end(); }); -tap.test('.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[2].name, 'foo_nodejs_external_memory_bytes'); - t.end(); -}); +tap.test( + '.collect() - call method - 3rd item in Array - should be an object where "name" is "nodejs_external_memory_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].name, "nodejs_external_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[2].name, "foo_nodejs_external_memory_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 3rd item in Array - should be an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.true(Number.isFinite(result[2].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.ok(Number.isFinite(result[2].value)); + t.end(); }); tap.test('.collect() - call method - 3rd item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].type, 1); + t.end(); }); tap.test('.collect() - "process.memoryUsage" throws - should return "null"', (t) => { - const original = process.memoryUsage; - process.memoryUsage = () => { - throw new Error('blah'); - }; + const original = process.memoryUsage; + process.memoryUsage = () => { + throw new Error("blah"); + }; - const collector = new Collector(); - const result = collector.collect(); - t.true(result === null); + const collector = new Collector(); + const result = collector.collect(); + t.ok(result === null); - process.memoryUsage = original; - t.end(); + process.memoryUsage = original; + t.end(); }); diff --git a/test/collector-max-file-descriptors.js b/test/collector-max-file-descriptors.js index 280b2d2..702ccfe 100644 --- a/test/collector-max-file-descriptors.js +++ b/test/collector-max-file-descriptors.js @@ -1,77 +1,90 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-max-file-descriptors'); +const Collector = require("../lib/collector-max-file-descriptors"); -tap.grep = process.platform === 'linux' ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; +tap.grep = + process.platform === "linux" ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; -tap.test('Constructor() - condition:all - object type - should be CollectorMaxFileDescriptors', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorMaxFileDescriptors]'); - t.end(); +tap.test("Constructor() - condition:all - object type - should be CollectorMaxFileDescriptors", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorMaxFileDescriptors]"); + t.end(); }); /** * When the operating system support reporting file descriptors */ -tap.test('.collect() - condition:supported - call method - should return an Array with the length of 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - condition:supported - call method - should return an Array with the length of 1", async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_max_fds"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'process_max_fds'); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_max_fds"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "process_max_fds"); + t.end(); + }, +); -tap.test('.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[0].name, 'foo_process_max_fds'); - t.end(); -}); +tap.test( + '.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[0].name, "foo_process_max_fds"); + t.end(); + }, +); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); + }, +); tap.test('.collect() - condition:supported - call method twice - should return "null" on second call', async (t) => { - const collector = new Collector(); - const resultA = await collector.collect(); - t.true(Array.isArray(resultA)); + const collector = new Collector(); + const resultA = await collector.collect(); + t.ok(Array.isArray(resultA)); - const resultB = await collector.collect(); - t.true(resultB === null); - t.end(); + const resultB = await collector.collect(); + t.ok(resultB === null); + t.end(); }); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); + }, +); /** * When the operating system DOES NOT support reporting file descriptors */ tap.test('.collect() - condition:unsupported - call method - should return "null"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(result === null); - t.end(); + const collector = new Collector(); + const result = await collector.collect(); + t.ok(result === null); + t.end(); }); diff --git a/test/collector-open-file-descriptors.js b/test/collector-open-file-descriptors.js index a2947a3..ab97bfc 100644 --- a/test/collector-open-file-descriptors.js +++ b/test/collector-open-file-descriptors.js @@ -1,66 +1,79 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-open-file-descriptors'); +const Collector = require("../lib/collector-open-file-descriptors"); -tap.grep = process.platform === 'linux' ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; +tap.grep = + process.platform === "linux" ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; -tap.test('Constructor() - condition:all - object type - should be CollectorOpenFileDescriptors', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorOpenFileDescriptors]'); - t.end(); +tap.test("Constructor() - condition:all - object type - should be CollectorOpenFileDescriptors", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorOpenFileDescriptors]"); + t.end(); }); /** * When the operating system support reporting file descriptors */ -tap.test('.collect() - condition:supported - call method - should return an Array with the length of 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - condition:supported - call method - should return an Array with the length of 1", async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_open_fds"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.equal(result[0].name, 'process_open_fds'); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_open_fds"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.equal(result[0].name, "process_open_fds"); + t.end(); + }, +); -tap.test('.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[0].name, 'foo_process_open_fds'); - t.end(); -}); +tap.test( + '.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[0].name, "foo_process_open_fds"); + t.end(); + }, +); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); + }, +); -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); -}); +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); + }, +); /** * When the operating system DOES NOT support reporting file descriptors */ tap.test('.collect() - condition:unsupported - call method - should return "null"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(result === null); - t.end(); + const collector = new Collector(); + const result = await collector.collect(); + t.ok(result === null); + t.end(); }); diff --git a/test/collector-process-resident-memory.js b/test/collector-process-resident-memory.js index 45eadb9..22b5a46 100644 --- a/test/collector-process-resident-memory.js +++ b/test/collector-process-resident-memory.js @@ -1,155 +1,203 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-process-resident-memory'); +const Collector = require("../lib/collector-process-resident-memory"); -tap.grep = process.platform === 'linux' ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; +tap.grep = + process.platform === "linux" ? [/condition:all|condition:supported/] : [/condition:all|condition:unsupported/]; -tap.test('Constructor() - condition:all - object type - should be CollectorProcessResidentMemory', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorProcessResidentMemory]'); - t.end(); +tap.test("Constructor() - condition:all - object type - should be CollectorProcessResidentMemory", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorProcessResidentMemory]"); + t.end(); }); /** * When the operating system support reporting more than the resident process memory */ -tap.test('.collect() - condition:supported - call method - should return an Array with the length of 3', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 3); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_resident_memory_bytes"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'process_resident_memory_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[0].name, 'foo_process_resident_memory_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 2nd item in Array - should return an object where "name" is "process_virtual_memory_bytes"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].name, 'process_virtual_memory_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[1].name, 'foo_process_virtual_memory_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 2nd item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[1], 'object'); - t.true(Number.isFinite(result[1].value)); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 2nd item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].type, 1); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 3rd item in Array - should return an object where "name" is "process_heap_bytes"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].name, 'process_heap_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', async (t) => { - const collector = new Collector('foo_'); - const result = await collector.collect(); - t.equal(result[2].name, 'foo_process_heap_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 3rd item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[2], 'object'); - t.true(Number.isFinite(result[2].value)); - t.end(); -}); - -tap.test('.collect() - condition:supported - call method - 3rd item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].type, 1); - t.end(); -}); - +tap.test(".collect() - condition:supported - call method - should return an Array with the length of 3", async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 3); + t.end(); +}); + +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "name" is "process_resident_memory_bytes"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "process_resident_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[0].name, "foo_process_resident_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 1st item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 2nd item in Array - should return an object where "name" is "process_virtual_memory_bytes"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].name, "process_virtual_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[1].name, "foo_process_virtual_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 2nd item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[1], "object"); + t.ok(Number.isFinite(result[1].value)); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 2nd item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].type, 1); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 3rd item in Array - should return an object where "name" is "process_heap_bytes"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].name, "process_heap_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', + async (t) => { + const collector = new Collector("foo_"); + const result = await collector.collect(); + t.equal(result[2].name, "foo_process_heap_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 3rd item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[2], "object"); + t.ok(Number.isFinite(result[2].value)); + t.end(); + }, +); + +tap.test( + '.collect() - condition:supported - call method - 3rd item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].type, 1); + t.end(); + }, +); /** * When the operating system DOES NOT support reporting more than the resident process memory */ -tap.test('.collect() - condition:unsupported - call method - should return an Array with the length of 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); -}); - -tap.test('.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "name" is "process_resident_memory_bytes"', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'process_resident_memory_bytes'); - t.end(); -}); - -tap.test('.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "value" is an Integer', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isFinite(result[0].value)); - t.end(); -}); - -tap.test('.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "type" is 1', async (t) => { - const collector = new Collector(); - const result = await collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); -}); +tap.test( + ".collect() - condition:unsupported - call method - should return an Array with the length of 1", + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); + }, +); + +tap.test( + '.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "name" is "process_resident_memory_bytes"', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "process_resident_memory_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "value" is an Integer', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isFinite(result[0].value)); + t.end(); + }, +); + +tap.test( + '.collect() - condition:unsupported - call method - 1st item in Array - should return an object where "type" is 1', + async (t) => { + const collector = new Collector(); + const result = await collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); + }, +); diff --git a/test/collector-process-start-time.js b/test/collector-process-start-time.js index 8918f99..1f5f6a2 100644 --- a/test/collector-process-start-time.js +++ b/test/collector-process-start-time.js @@ -1,60 +1,66 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-process-start-time'); +const Collector = require("../lib/collector-process-start-time"); -tap.test('Constructor() - object type - should be CollectorProcessStartTime', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorProcessStartTime]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorProcessStartTime", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorProcessStartTime]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 1", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "process_start_time_seconds"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].name, 'process_start_time_seconds'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "process_start_time_seconds"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].name, "process_start_time_seconds"); + t.end(); + }, +); -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_process_start_time_seconds'); - t.end(); -}); +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_process_start_time_seconds"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object where "value" is an Integer', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Number.isInteger(result[0].value)); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Number.isInteger(result[0].value)); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); tap.test('.collect() - call method twice - should return "null" on second call', (t) => { - const collector = new Collector(); - const resultA = collector.collect(); - t.true(Array.isArray(resultA)); + const collector = new Collector(); + const resultA = collector.collect(); + t.ok(Array.isArray(resultA)); - const resultB = collector.collect(); - t.true(resultB === null); - t.end(); + const resultB = collector.collect(); + t.ok(resultB === null); + t.end(); }); diff --git a/test/collector-v8-heap.js b/test/collector-v8-heap.js index 920083a..9039e80 100644 --- a/test/collector-v8-heap.js +++ b/test/collector-v8-heap.js @@ -1,138 +1,165 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-v8-heap'); +const Collector = require("../lib/collector-v8-heap"); -tap.test('Constructor() - object type - should be CollectorV8Heap', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorV8Heap]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorV8Heap", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorV8Heap]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.end(); +tap.test(".collect() - call method - should return an Array", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_heap_space_size_total_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.equal(result[0].name, 'nodejs_heap_space_size_total_bytes'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_heap_space_size_total_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.equal(result[0].name, "nodejs_heap_space_size_total_bytes"); + t.end(); + }, +); -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_nodejs_heap_space_size_total_bytes'); - t.end(); -}); +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_nodejs_heap_space_size_total_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object with a "value" key', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(result[0].value !== null); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(result[0].value !== null); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); -}); - -tap.test('.collect() - call method - 1st item in Array - should return an object where "meta" is and object with metadata', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.type(result[0].meta, 'object'); - // Labels can vary between node versions, just check its not just an empty array - t.true(result[0].labels.length !== 0); - t.end(); -}); - -tap.test('.collect() - call method - 2nd item in Array - should return an object where "name" is "nodejs_heap_space_size_used_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.equal(result[1].name, 'nodejs_heap_space_size_used_bytes'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[1].name, 'foo_nodejs_heap_space_size_used_bytes'); - t.end(); -}); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); +}); + +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "meta" is and object with metadata', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.type(result[0].meta, "object"); + // Labels can vary between node versions, just check its not just an empty array + t.ok(result[0].labels.length !== 0); + t.end(); + }, +); + +tap.test( + '.collect() - call method - 2nd item in Array - should return an object where "name" is "nodejs_heap_space_size_used_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.equal(result[1].name, "nodejs_heap_space_size_used_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 2nd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[1].name, "foo_nodejs_heap_space_size_used_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 2nd item in Array - should return an object with a "value" key', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.true(result[1].value !== null); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.ok(result[1].value !== null); + t.end(); }); tap.test('.collect() - call method - 2nd item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.equal(result[1].type, 1); - t.end(); -}); - -tap.test('.collect() - call method - 2nd item in Array - should return an object where "meta" is and object with metadata', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[1], 'object'); - t.type(result[1].meta, 'object'); - // Labels can vary between node versions, just check its not just an empty array - t.true(result[1].labels.length !== 0); - t.end(); -}); - -tap.test('.collect() - call method - 3rd item in Array - should return an object where "name" is "nodejs_heap_space_size_available_bytes"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.equal(result[2].name, 'nodejs_heap_space_size_available_bytes'); - t.end(); -}); - -tap.test('.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[2].name, 'foo_nodejs_heap_space_size_available_bytes'); - t.end(); -}); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.equal(result[1].type, 1); + t.end(); +}); + +tap.test( + '.collect() - call method - 2nd item in Array - should return an object where "meta" is and object with metadata', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[1], "object"); + t.type(result[1].meta, "object"); + // Labels can vary between node versions, just check its not just an empty array + t.ok(result[1].labels.length !== 0); + t.end(); + }, +); + +tap.test( + '.collect() - call method - 3rd item in Array - should return an object where "name" is "nodejs_heap_space_size_available_bytes"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.equal(result[2].name, "nodejs_heap_space_size_available_bytes"); + t.end(); + }, +); + +tap.test( + '.collect() - "prefix" on constructor is set - 3rd item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[2].name, "foo_nodejs_heap_space_size_available_bytes"); + t.end(); + }, +); tap.test('.collect() - call method - 3rd item in Array - should return an object with a "value" key', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.true(result[2].value !== null); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.ok(result[2].value !== null); + t.end(); }); tap.test('.collect() - call method - 3rd item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.equal(result[2].type, 1); - t.end(); -}); - -tap.test('.collect() - call method - 3rd item in Array - should return an object where "meta" is and object with metadata', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[2], 'object'); - t.type(result[2].meta, 'object'); - // Labels can vary between node versions, just check its not just an empty array - t.true(result[2].labels.length !== 0); - t.end(); -}); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.equal(result[2].type, 1); + t.end(); +}); + +tap.test( + '.collect() - call method - 3rd item in Array - should return an object where "meta" is and object with metadata', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[2], "object"); + t.type(result[2].meta, "object"); + // Labels can vary between node versions, just check its not just an empty array + t.ok(result[2].labels.length !== 0); + t.end(); + }, +); diff --git a/test/collector-version.js b/test/collector-version.js index d1bb87a..ff853b9 100644 --- a/test/collector-version.js +++ b/test/collector-version.js @@ -1,75 +1,84 @@ -'use strict'; +"use strict"; -const tap = require('tap'); +const tap = require("tap"); -const Collector = require('../lib/collector-version'); +const Collector = require("../lib/collector-version"); -tap.test('Constructor() - object type - should be CollectorVersion', (t) => { - const collector = new Collector(); - t.equal(Object.prototype.toString.call(collector), '[object CollectorVersion]'); - t.end(); +tap.test("Constructor() - object type - should be CollectorVersion", (t) => { + const collector = new Collector(); + t.equal(Object.prototype.toString.call(collector), "[object CollectorVersion]"); + t.end(); }); -tap.test('.collect() - call method - should return an Array with the length of 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.true(Array.isArray(result)); - t.equal(result.length, 1); - t.end(); +tap.test(".collect() - call method - should return an Array with the length of 1", (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.ok(Array.isArray(result)); + t.equal(result.length, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_version_info"', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.equal(result[0].name, 'nodejs_version_info'); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "name" is "nodejs_version_info"', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.equal(result[0].name, "nodejs_version_info"); + t.end(); + }, +); -tap.test('.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', (t) => { - const collector = new Collector('foo_'); - const result = collector.collect(); - t.equal(result[0].name, 'foo_nodejs_version_info'); - t.end(); -}); +tap.test( + '.collect() - "prefix" on constructor is set - 1st item in Array - should prepend prefix to "name" in metric', + (t) => { + const collector = new Collector("foo_"); + const result = collector.collect(); + t.equal(result[0].name, "foo_nodejs_version_info"); + t.end(); + }, +); tap.test('.collect() - call method - 1st item in Array - should return an object where "value" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].value, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].value, 1); + t.end(); }); tap.test('.collect() - call method - 1st item in Array - should return an object where "type" is 1', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.equal(result[0].type, 1); - t.end(); + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.equal(result[0].type, 1); + t.end(); }); -tap.test('.collect() - call method - 1st item in Array - should return an object where "labels" represent the version numbers as segmens', (t) => { - const collector = new Collector(); - const result = collector.collect(); - t.type(result[0], 'object'); - t.true(Array.isArray(result[0].labels)); - t.equal(result[0].labels[0].name, 'version'); - t.type(result[0].labels[0].value, 'string'); - t.equal(result[0].labels[1].name, 'major'); - t.true(Number.isInteger(result[0].labels[1].value)); - t.equal(result[0].labels[2].name, 'minor'); - t.true(Number.isInteger(result[0].labels[2].value)); - t.equal(result[0].labels[3].name, 'patch'); - t.true(Number.isInteger(result[0].labels[3].value)); - t.end(); -}); +tap.test( + '.collect() - call method - 1st item in Array - should return an object where "labels" represent the version numbers as segmens', + (t) => { + const collector = new Collector(); + const result = collector.collect(); + t.type(result[0], "object"); + t.ok(Array.isArray(result[0].labels)); + t.equal(result[0].labels[0].name, "version"); + t.type(result[0].labels[0].value, "string"); + t.equal(result[0].labels[1].name, "major"); + t.ok(Number.isInteger(result[0].labels[1].value)); + t.equal(result[0].labels[2].name, "minor"); + t.ok(Number.isInteger(result[0].labels[2].value)); + t.equal(result[0].labels[3].name, "patch"); + t.ok(Number.isInteger(result[0].labels[3].value)); + t.end(); + }, +); tap.test('.collect() - call method twice - should return "null" on second call', (t) => { - const collector = new Collector(); - const resultA = collector.collect(); - t.true(Array.isArray(resultA)); + const collector = new Collector(); + const resultA = collector.collect(); + t.ok(Array.isArray(resultA)); - const resultB = collector.collect(); - t.true(resultB === null); - t.end(); + const resultB = collector.collect(); + t.ok(resultB === null); + t.end(); }); diff --git a/test/process.js b/test/process.js index ce08a33..8332709 100644 --- a/test/process.js +++ b/test/process.js @@ -1,48 +1,48 @@ -'use strict'; +"use strict"; -const tap = require('tap'); -const stream = require('readable-stream'); -const Process = require('../'); +const tap = require("tap"); +const stream = require("readable-stream"); +const Process = require("../"); const destObjectStream = (done) => { - const arr = []; + const arr = []; - const dStream = new stream.Writable({ - objectMode: true, - write(chunk, encoding, callback) { - arr.push(chunk); - callback(); - }, - }); + const dStream = new stream.Writable({ + objectMode: true, + write(chunk, encoding, callback) { + arr.push(chunk); + callback(); + }, + }); - dStream.on('finish', () => { - done(arr); - }); + dStream.on("finish", () => { + done(arr); + }); - return dStream; + return dStream; }; /** * Constructor */ -tap.test('Constructor() - object type - should be MetricsProcess', (t) => { - const proc = new Process(); - t.equal(Object.prototype.toString.call(proc), '[object MetricsProcess]'); - t.end(); +tap.test("Constructor() - object type - should be MetricsProcess", (t) => { + const proc = new Process(); + t.equal(Object.prototype.toString.call(proc), "[object MetricsProcess]"); + t.end(); }); -tap.test('Constructor() - foo - should foo', (t) => { - const proc = new Process(); - const dest = destObjectStream((result) => { - t.true(Array.isArray(result)); - t.end(); - }); +tap.test("Constructor() - foo - should foo", (t) => { + const proc = new Process(); + const dest = destObjectStream((result) => { + t.ok(Array.isArray(result)); + t.end(); + }); - proc.on('collect:end', () => { - dest.end(); - }); + proc.on("collect:end", () => { + dest.end(); + }); - proc.pipe(dest); - proc.start(); + proc.pipe(dest); + proc.start(); });