diff --git a/packages/core/src/metrics/index.js b/packages/core/src/metrics/index.js index 0aeb48b659..1ea09a9368 100644 --- a/packages/core/src/metrics/index.js +++ b/packages/core/src/metrics/index.js @@ -23,7 +23,7 @@ exports.init = _config => { /** * Depending on what kind of data the metric or snapshot attributes represents, a number of different payloads are * valid. Ultimately, it depends on what the backend understands. - * @typedef {string|Object.|Array.} SnapshotOrMetricsPayload + * @typedef {Number|string|Object.|Array.} SnapshotOrMetricsPayload */ /** diff --git a/packages/shared-metrics/src/activeHandles.js b/packages/shared-metrics/src/activeHandles.js deleted file mode 100644 index 9e39a689cb..0000000000 --- a/packages/shared-metrics/src/activeHandles.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * (c) Copyright IBM Corp. 2021 - * (c) Copyright Instana Inc. and contributors 2016 - */ - -'use strict'; - -exports.payloadPrefix = 'activeHandles'; - -Object.defineProperty(exports, 'currentPayload', { - get: function () { - // TODO: _getActiveHandles is deprecated. Replace with getActiveResourcesInfo. - // https://nodejs.org/api/deprecations.html#dep0161-process_getactiverequests-and-process_getactivehandles - // Added in v16. - // refs https://jsw.ibm.com/browse/INSTA-64277 - // @ts-ignore - return process._getActiveHandles().length; - } -}); diff --git a/packages/shared-metrics/src/activeRequests.js b/packages/shared-metrics/src/activeRequests.js deleted file mode 100644 index f65c470bd7..0000000000 --- a/packages/shared-metrics/src/activeRequests.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * (c) Copyright IBM Corp. 2021 - * (c) Copyright Instana Inc. and contributors 2016 - */ - -'use strict'; - -exports.payloadPrefix = 'activeRequests'; - -Object.defineProperty(exports, 'currentPayload', { - get: function () { - // TODO: _getActiveRequests is deprecated. Replace with getActiveResourcesInfo. - // https://nodejs.org/api/deprecations.html#dep0161-process_getactiverequests-and-process_getactivehandles - // Added in v16. - // refs https://jsw.ibm.com/browse/INSTA-64277 - // @ts-ignore - return process._getActiveRequests().length; - } -}); diff --git a/packages/shared-metrics/src/activeResources.js b/packages/shared-metrics/src/activeResources.js new file mode 100644 index 0000000000..1025a4dc5f --- /dev/null +++ b/packages/shared-metrics/src/activeResources.js @@ -0,0 +1,13 @@ +/* + * (c) Copyright IBM Corp. 2026 + */ + +'use strict'; + +exports.payloadPrefix = 'activeResources'; + +Object.defineProperty(exports, 'currentPayload', { + get: function () { + return process.getActiveResourcesInfo().length; + } +}); diff --git a/packages/shared-metrics/src/index.js b/packages/shared-metrics/src/index.js index 54fbcb6913..7722cb602f 100644 --- a/packages/shared-metrics/src/index.js +++ b/packages/shared-metrics/src/index.js @@ -5,8 +5,7 @@ 'use strict'; -const activeHandles = require('./activeHandles'); -const activeRequests = require('./activeRequests'); +const activeResources = require('./activeResources'); const args = require('./args'); const dependencies = require('./dependencies'); const directDependencies = require('./directDependencies'); @@ -25,8 +24,7 @@ const util = require('./util'); /** @type {Array.} */ const allMetrics = [ - activeHandles, - activeRequests, + activeResources, args, dependencies, directDependencies, diff --git a/packages/shared-metrics/test/activeRequests_test.js b/packages/shared-metrics/test/activeRequests_test.js deleted file mode 100644 index 7068087cc1..0000000000 --- a/packages/shared-metrics/test/activeRequests_test.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - * (c) Copyright IBM Corp. 2021 - * (c) Copyright Instana Inc. and contributors 2019 - */ - -'use strict'; - -const expect = require('chai').expect; -const { uninstrumentedFs: fs } = require('@instana/core'); - -const activeRequests = require('../src/activeRequests'); - -describe('metrics.activeRequests', () => { - it('should export active requests count', () => { - // @ts-ignore - expect(activeRequests.currentPayload).to.equal(process._getActiveRequests().length); - }); - - it('should update requests count for a fs.open', () => { - const activeRequestBefore = activeRequests.currentPayload; - for (let i = 0; i < 13; i++) { - fs.open(__filename, 'r', () => {}); - } - expect(activeRequests.currentPayload).to.equal(activeRequestBefore + 13); - }); -}); diff --git a/packages/shared-metrics/test/activeHandles_test.js b/packages/shared-metrics/test/activeResources_test.js similarity index 50% rename from packages/shared-metrics/test/activeHandles_test.js rename to packages/shared-metrics/test/activeResources_test.js index 9d46d39633..dd0345ad54 100644 --- a/packages/shared-metrics/test/activeHandles_test.js +++ b/packages/shared-metrics/test/activeResources_test.js @@ -7,37 +7,40 @@ const expect = require('chai').expect; const net = require('net'); +const fs = require('fs'); const config = require('@instana/core/test/config'); const testUtils = require('@instana/core/test/test_util'); -const activeHandles = require('../src/activeHandles'); +const activeResources = require('../src/activeResources'); -describe('metrics.activeHandles', function () { +describe('metrics.activeResources', function () { this.timeout(config.getTestTimeout()); - it('should export active handle count', () => { - // @ts-ignore - expect(activeHandles.currentPayload).to.equal(process._getActiveHandles().length); + it('should export active resource count', () => { + // [ 'TTYWrap', 'TTYWrap', 'TTYWrap', 'PipeWrap', 'ProcessWrap' ] + // Teletypewriter = stdin, stdout, stderr + // PipeWrap = internal pipe for process communication (test -> app) + // ProcessWrap = the child process itself + expect(activeResources.currentPayload).to.equal(5); }); - // This test is only compatible with Node.js versions lower than 11. - // The failure is due to changes introduced in commit: - // https://github.com/nodejs/node/commit/ccc3bb73db. - // We can reintroduce the test once we switch from the deprecated - // _getActiveHandles flag to getActiveResourcesInfo, which is more reliable. - // More details: https://nodejs.org/api/process.html#processgetactiveresourcesinfo - // For additional context, see: https://github.com/instana/nodejs/pull/1387 - - it.skip('should update handle count for a setTimeout', () => { - const previousCount = activeHandles.currentPayload; + it('should update resource count for a setTimeout', () => { const timeoutHandle = setTimeout(() => {}, 100); - // TODO: getActiveResourcesInfo returns the correct value, but `_getActiveHandles` does not - // for v23. - expect(activeHandles.currentPayload).to.equal(previousCount + 1); + // [ 'TTYWrap', 'TTYWrap', 'TTYWrap', 'PipeWrap', 'ProcessWrap', 'Timeout' ] + expect(activeResources.currentPayload).to.equal(6); clearTimeout(timeoutHandle); }); + it('should update requests count for a fs.open', () => { + const initialCount = activeResources.currentPayload; + const count = 13; + for (let i = 0; i < count; i++) { + fs.open(__filename, 'r', () => {}); + } + expect(activeResources.currentPayload).to.equal(initialCount + count); + }); + describe('with net client/server', () => { /* eslint-disable max-len */ // Inspired by @@ -50,11 +53,10 @@ describe('metrics.activeHandles', function () { const connections = []; /** @type {Array.<*>} */ const clients = []; - /** @type {*} */ - let activeHandlesBefore; + let initialActiveResources = 0; - beforeEach(() => { - activeHandlesBefore = activeHandles.currentPayload; + before(() => { + initialActiveResources = activeResources.currentPayload; server = net .createServer(function listener(c) { connections.push(c); @@ -62,7 +64,7 @@ describe('metrics.activeHandles', function () { .listen(0, makeConnection); }); - afterEach(() => { + after(() => { clients.forEach(client => { client.destroy(); }); @@ -77,12 +79,11 @@ describe('metrics.activeHandles', function () { () => new Promise((resolve, reject) => { if (clients.length >= maxClients) { - // At least one handle should exist per client and one per connection, that's why we expect - // (2 * maxClients) more handles than we had initially. However, other things are happening in the Node.js - // runtime as well while this test is running, so it might actually happen that some of the unrelated - // handles that existed initially have since been removed, which is why we allow for a little wiggle room - // (-4 at the end). Without this wiggle room, this test is flaky. - expect(activeHandles.currentPayload).to.be.at.least(activeHandlesBefore + 2 * maxClients - 4); + // Initially is 5 with the test setup. + // 1 TCPServerWrap because of the server + // Max Clients is 8 (each 2 TCPSocketWrap because of client and server side) + // 1 timeoutWrap because of the makeConnection timeout + expect(activeResources.currentPayload).to.be.at.least(initialActiveResources + 1 * (maxClients * 2) + 1); resolve(); } else { reject(new Error('Still waiting for more clients to connect.'));