Skip to content

Commit

Permalink
support file urls
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Jan 23, 2025
1 parent c95acdc commit 3aac7ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/health-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const fs = require('node:fs')
const crypto = require('node:crypto')
const path = require('node:path')
const { fileURLToPath } = require('node:url')

const defaultLogger = require('./logger').child({ component: 'HealthReporter' })

Expand Down Expand Up @@ -92,7 +93,7 @@ class HealthReporter {
setInterval = global.setInterval
} = {}) {
const enabled = agentConfig.agent_control?.enabled
const outDir = agentConfig.agent_control?.health?.delivery_location
let outDir = agentConfig.agent_control?.health?.delivery_location
let checkInterval = agentConfig.agent_control?.health?.frequency

this.#logger = logger
Expand All @@ -107,6 +108,9 @@ class HealthReporter {
return
}

if (outDir.includes('://') === true) {
outDir = fileURLToPath(outDir)
}
const dirCheck = directoryAvailable(outDir)
if (dirCheck.available === false) {
this.#logger.error('health check output directory not accessible, skipping health reporting', { error: dirCheck.error })
Expand Down
28 changes: 27 additions & 1 deletion test/unit/agent/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ test('should not throw with valid config', () => {
assert.equal(agent.config.agent_enabled, false)
})

test('should initialize health reporter', () => {
test('agent control should initialize health reporter', () => {
delete process.env.NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION
const dest = os.tmpdir()
process.env.NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION = dest
const config = configurator.initialize({
agent_enabled: false,
agent_control: {
Expand All @@ -86,6 +88,30 @@ test('should initialize health reporter', () => {
assert.equal(agent.healthReporter.destFile.startsWith(dest), true)
})

test('agent control writes to file uri destinations', (t, end) => {
delete process.env.NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION
const dest = 'file://' + os.tmpdir()
process.env.NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION = dest.replace('file://', '')
const config = configurator.initialize({
agent_enabled: false,
agent_control: {
enabled: true,
health: {
delivery_location: dest
}
}
})
const agent = new Agent(config)

setTimeout(check, 1_500)

function check() {
const data = fs.readFileSync(agent.healthReporter.destFile)
assert.equal(data.toString().startsWith('healthy: true'), true, 'should have a healthy report')
end()
}
})

test('when loaded with defaults', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
Expand Down

0 comments on commit 3aac7ef

Please sign in to comment.