diff --git a/main.js b/main.js index 9f3b5c92..ed8e27bd 100644 --- a/main.js +++ b/main.js @@ -23,7 +23,6 @@ const consentMiddleware = require('./src/middleware/consent'); // logging and monitoring const metrics = require('next-metrics'); -const serviceMetrics = require('./src/lib/service-metrics'); const logger = require('@dotcom-reliability-kit/logger'); // utils @@ -132,7 +131,7 @@ const getAppContainer = (options) => { ); if (options.withServiceMetrics) { - instrumentListen.addMetrics(serviceMetrics.init()); + metrics.fetch.instrument(); } if (options.withBackendAuthentication) { diff --git a/package-lock.json b/package-lock.json index a591c354..8b9e0e75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "express": "^4.17.3", "isomorphic-fetch": "^3.0.0", "n-health": "^13.0.0", - "next-metrics": "^12.12.0", + "next-metrics": "^12.15.0", "semver": "^7.3.7" }, "bin": { @@ -5734,16 +5734,16 @@ "dev": true }, "node_modules/next-metrics": { - "version": "12.12.0", - "resolved": "https://registry.npmjs.org/next-metrics/-/next-metrics-12.12.0.tgz", - "integrity": "sha512-jTBMxv43rYOrqvp/YI+58obu0oKg4UvbbA/VacuJCAbhNM6uPO45kGJzpPAsJEhvh/SyUc7uHyt/RHBEfzQgJA==", + "version": "12.15.0", + "resolved": "https://registry.npmjs.org/next-metrics/-/next-metrics-12.15.0.tgz", + "integrity": "sha512-8t0eqQA6fS0cEXMqD3HV7VuZ5nN5y4Q+DyZm08T3tGOMdxhhFJP2pRcQtWHwlK3XZCTTXoKKehmaJxrJo1oHXw==", "dependencies": { - "@dotcom-reliability-kit/logger": "^3.0.3", + "@dotcom-reliability-kit/logger": "^3.1.4", "lodash": "^4.17.21", "metrics": "^0.1.8" }, "engines": { - "node": "18.x || 20.x", + "node": "18.x || 20.x || 22.x", "npm": "8.x || 9.x || 10.x" } }, @@ -12998,11 +12998,11 @@ "dev": true }, "next-metrics": { - "version": "12.12.0", - "resolved": "https://registry.npmjs.org/next-metrics/-/next-metrics-12.12.0.tgz", - "integrity": "sha512-jTBMxv43rYOrqvp/YI+58obu0oKg4UvbbA/VacuJCAbhNM6uPO45kGJzpPAsJEhvh/SyUc7uHyt/RHBEfzQgJA==", + "version": "12.15.0", + "resolved": "https://registry.npmjs.org/next-metrics/-/next-metrics-12.15.0.tgz", + "integrity": "sha512-8t0eqQA6fS0cEXMqD3HV7VuZ5nN5y4Q+DyZm08T3tGOMdxhhFJP2pRcQtWHwlK3XZCTTXoKKehmaJxrJo1oHXw==", "requires": { - "@dotcom-reliability-kit/logger": "^3.0.3", + "@dotcom-reliability-kit/logger": "^3.1.4", "lodash": "^4.17.21", "metrics": "^0.1.8" } diff --git a/package.json b/package.json index 2a27da7b..95eb1c45 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "express": "^4.17.3", "isomorphic-fetch": "^3.0.0", "n-health": "^13.0.0", - "next-metrics": "^12.12.0", + "next-metrics": "^12.15.0", "semver": "^7.3.7" }, "devDependencies": { diff --git a/src/lib/health-checks.js b/src/lib/health-checks.js index 9daf6ebf..75abf199 100644 --- a/src/lib/health-checks.js +++ b/src/lib/health-checks.js @@ -10,7 +10,6 @@ const logger = require('@dotcom-reliability-kit/logger'); const herokuLogDrainCheck = require('./heroku-log-drain-check'); const metricsHealthCheck = require('./metrics-healthcheck'); const supportedNodeJsVersionCheck = require('./supported-node-js-version-check'); -const unRegisteredServicesHealthCheck = require('./unregistered-services-healthCheck'); /** * @param {ExpressApp} app @@ -34,7 +33,6 @@ module.exports = (app, options, meta) => { /** @type {Healthcheck[]} */ const defaultChecks = [ ...tickingMetricChecks, - unRegisteredServicesHealthCheck.setAppName(meta.name), metricsHealthCheck(meta.name), supportedNodeJsVersionCheck(meta.name) ]; diff --git a/src/lib/service-metrics.js b/src/lib/service-metrics.js deleted file mode 100644 index cbaa838c..00000000 --- a/src/lib/service-metrics.js +++ /dev/null @@ -1,34 +0,0 @@ -/** -* @typedef {import("../../typings/metrics").Healthcheck} Healthcheck -*/ - -const metrics = require('next-metrics'); -const unRegisteredServicesHealthCheck = require('./unregistered-services-healthCheck'); - -/** @type {Record} */ -let unregisteredServices = {}; - -/** -* @returns {TickingMetric} -*/ -module.exports = { - init: () => { - const id = setInterval(() => { - unRegisteredServicesHealthCheck.updateCheck(unregisteredServices); - }, 1 * 60 * 1000); - - metrics.fetch.instrument({ - onUninstrumented: function (/** @type {string} */ url) { - if (typeof url === 'string') { - unregisteredServices[url.split('?')[0]] = true; - } - } - }); - - const stop = () => { - clearInterval(id); - }; - - return { stop }; - } -}; diff --git a/src/lib/unregistered-services-healthCheck.js b/src/lib/unregistered-services-healthCheck.js deleted file mode 100644 index fc41de16..00000000 --- a/src/lib/unregistered-services-healthCheck.js +++ /dev/null @@ -1,49 +0,0 @@ -let lastCheckOk = true; -let lastCheckOutput = 'All services\' metrics are set up in Next-metrics'; -let panicGuide = 'Don\'t panic'; - -/** @type {Date} */ -let lastCheckTime; - -/** - * @type {import("../../typings/metrics").InitHealthCheck} - */ -module.exports = { - setAppName: (appName) => { - return { - getStatus: () => { - const region = process.env.REGION === 'US' ? 'us' : 'eu'; - - return { - id: `all-services-registered-${region}`, - name: `Metrics: All services for ${appName} registered in next-metrics in ${region}`, - ok: lastCheckOk, - checkOutput: lastCheckOutput, - lastUpdated: lastCheckTime, - panicGuide: panicGuide, - severity: 3, - businessImpact: - 'We don\'t have any visibility with unregistered services.', - technicalSummary: - 'Set up services\' metrics in next-metrics/lib/metrics/services.js to send to Graphite.' - }; - } - }; - }, - updateCheck: (unregisteredServices) => { - lastCheckTime = new Date(); - - if (Object.keys(unregisteredServices).length > 0) { - lastCheckOutput = - Object.keys(unregisteredServices).join(', ') + - ' services called but no metrics set up.'; - panicGuide = - 'See next-metrics/lib/metrics/services.js and set metrics for the service, then release next-metrics and rebuild this app.'; - lastCheckOk = false; - } else { - lastCheckOutput = 'All services\' metrics are set up in Next-metrics'; - panicGuide = 'Don\'t panic'; - lastCheckOk = true; - } - } -}; diff --git a/test/app/clear-interval.test.js b/test/app/clear-interval.test.js index 0a2d9896..c4379438 100644 --- a/test/app/clear-interval.test.js +++ b/test/app/clear-interval.test.js @@ -3,16 +3,9 @@ const nextExpress = require('../../main'); const InstrumentListen = require('../../src/lib/instrument-listen'); const expect = require('chai').expect; const healthChecks = require('../../src/lib/health-checks'); -const serviceMetrics = require('../../src/lib/service-metrics'); const sinon = require('sinon'); describe('clears intervals', () => { - it('should return object with stop function in service-metrics', () => { - const result = serviceMetrics.init(); - expect(result.stop).to.not.be.undefined; - result.stop(); - }); - it('should return an array of objects with stop functions in health-checks', () => { const app = express(); @@ -29,21 +22,6 @@ describe('clears intervals', () => { }, 0); }); - it('should call stop() which clears all intervals if a metric is added to instrumentListen', () => { - const app = express(); - const instrumentListen = new InstrumentListen(app, {}, []); - const spy = sinon.spy(); - const stub = sinon.stub(serviceMetrics, 'init'); - stub.returns({ - stop: spy - }); - instrumentListen.addMetrics(serviceMetrics.init()); - - app.close(); - - expect(spy.called).to.be.true; - }); - it('should close server in app.close if there is a live server', () => { const app = express(); const instrumentListen = new InstrumentListen(app, {}, [Promise.resolve()]); diff --git a/typings/metrics.d.ts b/typings/metrics.d.ts index bf4be6eb..6ecaaa6c 100644 --- a/typings/metrics.d.ts +++ b/typings/metrics.d.ts @@ -17,8 +17,3 @@ export type TickingMetric = { export type Healthcheck = { getStatus: () => HealthcheckStatus; }; - -export interface InitHealthCheck { - setAppName: (appName: string) => Metrics.Healthcheck; - updateCheck: (unregisteredServices: Record) => void; -}