From c95acdc655b658739459318a26d3930a69aed1b7 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 23 Jan 2025 07:54:50 -0500 Subject: [PATCH] fix time calculations --- lib/health-reporter.js | 11 +++++++++-- test/unit/lib/health-reporter.test.js | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/health-reporter.js b/lib/health-reporter.js index f390982aaa..67974c7ed3 100644 --- a/lib/health-reporter.js +++ b/lib/health-reporter.js @@ -28,8 +28,15 @@ const VALID_CODES = new Map([ ['NR-APM-300', 'An unexpected error occurred.'] ]) +function getTime() { + // `process.hrtime.bigint` does not return a value relative to the epoch. + // So we have to perform this lossy calculation because the spec is + // insisting on nanoseconds. + return Date.now() * 1_000_000 +} + function writeStatus({ file, healthy = true, code, msg, startTime, callback } = {}) { - const currentTime = Number(process.hrtime.bigint()) + const currentTime = getTime() const yaml = [ `healthy: ${healthy}`, `status: '${msg}'`, @@ -113,7 +120,7 @@ class HealthReporter { checkInterval = parseInt(checkInterval, 10) * 1_000 } - this.#startTime = Number(process.hrtime.bigint()) + this.#startTime = getTime() const uuid = crypto.randomUUID().replaceAll('-', '') this.#destFile = path.join(outDir, `health-${uuid}.yaml`) diff --git a/test/unit/lib/health-reporter.test.js b/test/unit/lib/health-reporter.test.js index 6ccf2183b3..a923845552 100644 --- a/test/unit/lib/health-reporter.test.js +++ b/test/unit/lib/health-reporter.test.js @@ -26,13 +26,13 @@ test.beforeEach((ctx) => { ctx.nr = {} ctx.nr.accessOrigin = fs.accessSync ctx.nr.writeFileOrig = fs.writeFile - ctx.nr.bigintOrig = process.hrtime.bigint + ctx.nr.nowOrig = Date.now fs.accessSync = () => true - let count = 0n - process.hrtime.bigint = () => { - count += 1n + let count = 0 + Date.now = () => { + count += 1 return count } @@ -72,7 +72,7 @@ test.beforeEach((ctx) => { test.afterEach((ctx) => { fs.accessSync = ctx.nr.accessOrig fs.writeFile = ctx.nr.writeFileOrig - process.hrtime.bigint = ctx.nr.bigintOrig + Date.now = ctx.nr.nowOrig }) test('requires enabled to be true', (t) => { @@ -145,8 +145,8 @@ test('initializes and writes to destination', async (t) => { 'healthy: true', "status: 'Healthy.'", 'last_error: NR-APM-000', - 'start_time_unix_nano: 1', - 'status_time_unix_nano: 2' + 'start_time_unix_nano: 1000000', + 'status_time_unix_nano: 2000000' ].join('\n') ) plan.deepStrictEqual(options, { encoding: 'utf8' }) @@ -236,8 +236,8 @@ test('stop leaves last error code in place', async (t) => { 'healthy: false', "status: 'HTTP error communicating with New Relic.'", 'last_error: NR-APM-004', - 'start_time_unix_nano: 1', - 'status_time_unix_nano: 3' + 'start_time_unix_nano: 1000000', + 'status_time_unix_nano: 3000000' ].join('\n') ) callback() @@ -268,8 +268,8 @@ test('stop sets shutdown status', async (t) => { 'healthy: true', "status: 'Agent has shutdown.'", 'last_error: NR-APM-099', - 'start_time_unix_nano: 1', - 'status_time_unix_nano: 3' + 'start_time_unix_nano: 1000000', + 'status_time_unix_nano: 3000000' ].join('\n') ) callback()