Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions providers/logging/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,32 @@ class ApplicationInsightsTransport extends Transport {
return
}

const properties = buildProperties(info)
if (info.level === 'error') {
if (info.stack) {
const err = new Error(info.message)
err.stack = info.stack
this.aiClient.trackException({ exception: err, properties })
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No catch here, so a throw from the AI client still escapes through Winston into the caller. The callback fires, but the calling code crashes. Catch it and drop the telemetry quietly (or console.error it). Losing a log line beats killing the request that produced it.

const properties = buildProperties(info)
if (info.level === 'error') {
if (info.stack) {
const err = new Error(info.message)
err.stack = info.stack
this.aiClient.trackException({ exception: err, properties })
} else {
this.aiClient.trackTrace({
message: info.message,
severity: appInsights.KnownSeverityLevel.Error,
properties
})
}
} else {
this.aiClient.trackTrace({
message: info.message,
severity: appInsights.KnownSeverityLevel.Error,
severity: mapLevel(info.level),
properties
})
}
} else {
this.aiClient.trackTrace({
message: info.message,
severity: mapLevel(info.level),
properties
})
} catch (err) {
console.error('ApplicationInsights telemetry failed', err)
} finally {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a catch paired with this. Without one the exception still propagates after callback() runs, so we get the callback guarantee but lose the safety.

callback()
}

callback()
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/unit/providers/logging/loggerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,32 @@ describe('logger', function () {
expect(trackTrace.firstCall.args[0].message).to.contain('plain error without stack')
expect(trackTrace.firstCall.args[0].severity).to.equal('Error')
})

it('still calls callback if trackTrace throws', done => {
const consoleError = sinon.stub(console, 'error')
trackTrace.throws(new Error('AI unavailable'))
const logger = factory({ aiClient })
logger.info('test message')
clock.runAll()
setImmediate(() => {
expect(trackTrace.callCount).to.equal(1)
consoleError.restore()
done()
})
clock.runAll()
})

it('still calls callback if trackException throws', done => {
const consoleError = sinon.stub(console, 'error')
trackException.throws(new Error('AI unavailable'))
const logger = factory({ aiClient })
logger.log({ level: 'error', message: 'boom', stack: 'Error: boom\n at test:1:1' })
clock.runAll()
setImmediate(() => {
expect(trackException.callCount).to.equal(1)
consoleError.restore()
done()
})
clock.runAll()
})
})