Skip to content

Commit

Permalink
fix: Unhandled promise rejection for session import (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley authored Jun 26, 2024
1 parent 37f8bb0 commit 03efcf3
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/features/utils/__mocks__/agent-session.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const setupAgentSession = jest.fn()
export const setupAgentSession = jest.fn(() => {
return {}
})
3 changes: 2 additions & 1 deletion src/features/utils/__mocks__/feature-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const FeatureBase = jest.fn(function (agentIdentifier, aggregator, featur

this.ee = {
abort: jest.fn(),
on: jest.fn()
on: jest.fn(),
emit: jest.fn()
}
this.blocked = false
})
5 changes: 4 additions & 1 deletion src/features/utils/aggregate-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AggregateBase extends FeatureBase {
* @returns {Promise}
*/
waitForFlags (flagNames = []) {
return new Promise((resolve, reject) => {
const flagsPromise = new Promise((resolve, reject) => {
if (activatedFeatures[this.agentIdentifier]) {
resolve(buildOutput(activatedFeatures[this.agentIdentifier]))
} else {
Expand All @@ -32,6 +32,9 @@ export class AggregateBase extends FeatureBase {
})
}
})
return flagsPromise.catch(err => {
this.ee.emit('internal-error', [err])
})
}

drain () {
Expand Down
3 changes: 2 additions & 1 deletion src/features/utils/instrument-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class InstrumentBase extends FeatureBase {
}
} catch (e) {
warn('A problem occurred when starting up session manager. This page will not start or extend any session.', e)
this.ee.emit('internal-error', [e])
if (this.featureName === FEATURE_NAMES.sessionReplay) this.abortHandler?.() // SR should stop recording if session DNE
}

Expand Down Expand Up @@ -127,6 +128,6 @@ export class InstrumentBase extends FeatureBase {
*/
#shouldImportAgg (featureName, session) {
if (featureName === FEATURE_NAMES.sessionReplay) return canImportReplayAgg(this.agentIdentifier, session)
return true
return !(featureName === FEATURE_NAMES.sessionTrace && !session)
}
}
28 changes: 28 additions & 0 deletions tests/specs/npm/index.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ describe.withBrowsersMatching(es2022Support)('basic npm agent', () => {
expect(errorsPromise).toBeDefined()
expect(pageActionPromise).toBeDefined()
})

it('vite-react-wrapper should not break agent when session manager cannot be imported', async () => {
await browser.destroyAgentSession()
await browser.testHandle.scheduleReply('assetServer', {
test: function (request) {
const url = new URL(request.url, 'resolve://')
return (url.pathname.includes('agent-session'))
},
permanent: true,
statusCode: 500,
body: ''
})

const [ajaxPromise] = await Promise.all([
browser.testHandle.expectAjaxTimeSlices(),
browser.url(await browser.testHandle.assetURL('test-builds/vite-react-wrapper/index.html'))
])
expect(ajaxPromise.request.body.xhr).toBeDefined()

const agentSession = await browser.getAgentSessionInfo()
Object.values(agentSession.agentSessions).forEach(val =>
expect(val).toEqual({})
)
Object.values(agentSession.agentSessionInstances).forEach(val =>
expect(val).toEqual({})
)
expect(agentSession.localStorage).toEqual({})
})
})

async function getAgentProps (variablePath) {
Expand Down
1 change: 1 addition & 0 deletions tools/testing-server/plugins/test-handle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = fp(async function (fastify, testServer) {
}
if (!!testId && request.url.startsWith('/tests/assets') && request.url.includes('.html')) {
reply.header('set-cookie', `test-id=${testId};path=/build/`)
reply.header('set-cookie', `test-id=${testId};path=/tests/assets/`)
}

if (request.query.nonce) {
Expand Down
18 changes: 13 additions & 5 deletions tools/wdio/plugins/custom-commands.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,26 @@ export default class CustomCommands {
const agentSessionsJSON = await browser.execute(function () {
return JSON.stringify(Object.entries(newrelic.initializedAgents)
.reduce(function (aggregate, agentEntry) {
aggregate[agentEntry[0]] = agentEntry[1].runtime.session.state
if (agentEntry[1].runtime.session) {
aggregate[agentEntry[0]] = agentEntry[1].runtime.session.state
} else {
aggregate[agentEntry[0]] = {}
}
return aggregate
}, {}))
})
const agentSessions = JSON.parse(agentSessionsJSON)
const agentSessionInstances = await browser.execute(function () {
return Object.entries(newrelic.initializedAgents)
.reduce(function (aggregate, agentEntry) {
aggregate[agentEntry[0]] = {
key: agentEntry[1].runtime.session.key,
isNew: agentEntry[1].runtime.session.isNew,
initialized: agentEntry[1].runtime.session.initialized
if (agentEntry[1].runtime.session) {
aggregate[agentEntry[0]] = {
key: agentEntry[1].runtime.session.key,
isNew: agentEntry[1].runtime.session.isNew,
initialized: agentEntry[1].runtime.session.initialized
}
} else {
aggregate[agentEntry[0]] = {}
}
return aggregate
}, {})
Expand Down

0 comments on commit 03efcf3

Please sign in to comment.