Skip to content

Commit

Permalink
fix time calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Jan 23, 2025
1 parent f093ad4 commit c95acdc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
11 changes: 9 additions & 2 deletions lib/health-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`,
Expand Down Expand Up @@ -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`)
Expand Down
22 changes: 11 additions & 11 deletions test/unit/lib/health-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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' })
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c95acdc

Please sign in to comment.