Skip to content

Commit 05ae60a

Browse files
committed
address rollup issue
1 parent e4465c1 commit 05ae60a

File tree

7 files changed

+191
-2
lines changed

7 files changed

+191
-2
lines changed

src/loaders/micro-agent.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,23 @@ export class MicroAgent extends MicroAgentBase {
9999
so as to avoid the race condition of things like session and sharedAggregator not being ready by features that uses them right away. */
100100
nonAutoFeatures.forEach(f => {
101101
if (enabledFeatures[f] && featureNames.includes(f)) {
102-
import(`../features/${f}/aggregate`).then(({ Aggregate }) => {
102+
let lazyImport
103+
/** Define these imports with static strings to not break tools like roll-up */
104+
switch (f) {
105+
case 'jserrors':
106+
lazyImport = import('../features/jserrors/aggregate')
107+
break
108+
case 'generic_events':
109+
lazyImport = import('../features/generic_events/aggregate')
110+
break
111+
case 'metrics':
112+
lazyImport = import('../features/metrics/aggregate')
113+
break
114+
case 'logging':
115+
lazyImport = import('../features/logging/aggregate')
116+
break
117+
}
118+
lazyImport.then(({ Aggregate }) => {
103119
this.features[f] = new Aggregate(this)
104120
this.runtime.harvester.initializedAggregates.push(this.features[f]) // so that harvester will poll this feature agg on interval
105121
}).catch(err => warn(25, err))

tests/specs/npm/micro-agent.e2e.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,69 @@ describe('micro-agent', () => {
9292
}
9393
})
9494

95+
// https://new-relic.atlassian.net/browse/NR-453240 <-- issue with rollup seen here around dynamic imports
96+
it('Smoke Test - Can send distinct payloads of all relevant data types to 2 distinct app IDs - ROLL UP BUNDLE', async () => {
97+
const [rumCapture, errorsCapture, insightsCapture, logsCapture] = await browser.testHandle.createNetworkCaptures('bamServer', [
98+
{ test: testRumRequest },
99+
{ test: testErrorsRequest },
100+
{ test: testInsRequest },
101+
{ test: testLogsRequest }
102+
])
103+
await browser.url(await browser.testHandle.assetURL('test-builds/rollup-micro-agent/index.html'))
104+
105+
const [rumHarvests, errorsHarvests, insightsHarvests, logsHarvest] = await Promise.all([
106+
rumCapture.waitForResult({ totalCount: 2 }),
107+
errorsCapture.waitForResult({ totalCount: 2 }),
108+
insightsCapture.waitForResult({ totalCount: 2 }),
109+
logsCapture.waitForResult({ totalCount: 2, timeout: 15000 })
110+
])
111+
112+
// these props will get set to true once a test has matched it
113+
// if it gets tried again, the test will fail, since these should all
114+
// only have one distinct matching payload
115+
const tests = {
116+
1: { rum: false, err: false, pa: false, log: false },
117+
2: { rum: false, err: false, pa: false, log: false }
118+
}
119+
120+
// each type of test should check that:
121+
// each payload exists once per appId
122+
// each payload should have internal attributes matching it to the right appId
123+
rumHarvests.forEach(({ request: { query, body } }) => {
124+
expect(ranOnce(query.a, 'rum')).toEqual(true)
125+
expect(payloadMatchesAppId(query.a, body.ja.customAttr)).toEqual(true)
126+
})
127+
128+
errorsHarvests.forEach(({ request: { query, body } }) => {
129+
expect(ranOnce(query.a, 'err')).toEqual(true)
130+
expect(payloadMatchesAppId(query.a, body.err[0].params.message)).toEqual(true)
131+
})
132+
133+
insightsHarvests.forEach(({ request: { query, body } }) => {
134+
expect(ranOnce(query.a, 'pa')).toEqual(true)
135+
const data = body.ins[0]
136+
expect(payloadMatchesAppId(query.a, data.val, data.actionName, data.customAttr)).toEqual(true)
137+
})
138+
139+
logsHarvest.forEach(({ request: { query, body } }) => {
140+
const data = JSON.parse(body)[0]
141+
expect(ranOnce(data.common.attributes.appId, 'log')).toEqual(true)
142+
expect(payloadMatchesAppId(data.common.attributes.appId, data.logs[0].message)).toEqual(true)
143+
})
144+
145+
function payloadMatchesAppId (appId, ...props) {
146+
// each payload in this test is decorated with data that matches its appId for ease of testing
147+
return props.every(p => Number(appId) === Number(p))
148+
}
149+
150+
// eslint-disable-next-line
151+
function ranOnce (appId, type) {
152+
if (tests[appId][type]) return false
153+
tests[appId][type] = true
154+
return true
155+
}
156+
})
157+
95158
it('returns null on top-level spa api interaction call', async () => {
96159
await browser.url(await browser.testHandle.assetURL('test-builds/browser-agent-wrapper/micro-agent.html'))
97160

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "rollup-micro-agent",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "rollup -c"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"type": "commonjs",
14+
"dependencies": {
15+
"@newrelic/browser-agent": "file:../../../temp/newrelic-browser-agent-1.298.0.tgz"
16+
},
17+
"devDependencies": {
18+
"@rollup/plugin-commonjs": "^28.0.6",
19+
"@rollup/plugin-html": "^2.0.0",
20+
"@rollup/plugin-json": "^6.1.0",
21+
"@rollup/plugin-node-resolve": "^16.0.2",
22+
"rollup": "^4.52.4"
23+
}
24+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// rollup.config.mjs
2+
import html from '@rollup/plugin-html'
3+
import path, { dirname } from 'path'
4+
import { fileURLToPath } from 'url'
5+
import resolve from '@rollup/plugin-node-resolve' // <-- Import resolve
6+
import commonjs from '@rollup/plugin-commonjs'
7+
8+
const __filename = fileURLToPath(import.meta.url)
9+
const __dirname = dirname(__filename)
10+
11+
/**
12+
* Custom template function for @rollup/plugin-html
13+
* @param {object} params - Rollup output parameters
14+
* @returns {string} The final HTML string
15+
*/
16+
const customTemplate = ({ attributes, files, meta, publicPath, title }) => {
17+
const scriptTag = files.js
18+
.map(({ fileName }) => `<script src="${publicPath}${fileName}" type="module"></script>`)
19+
.join('\n')
20+
21+
// 3. Construct the HTML string with the generated script and your placeholders
22+
return (`
23+
<html>
24+
<head>
25+
<title>${title}</title>
26+
{init}
27+
{config}
28+
${scriptTag}
29+
</head>
30+
<body>
31+
<h1>This is a generic page that is instrumented by the NPM agent</h1>
32+
</body>
33+
</html>
34+
`)
35+
}
36+
37+
export default {
38+
input: 'src/micro-agent.js',
39+
output: {
40+
dir: path.resolve(__dirname, '../../../tests/assets/test-builds/rollup-micro-agent')
41+
},
42+
plugins: [
43+
resolve({ browser: true }),
44+
html({
45+
// Optional: Configuration for the generated HTML file
46+
47+
// Set the title of the HTML page
48+
title: 'My Rollup App',
49+
50+
// Optional: Set the filename for the generated HTML file
51+
fileName: 'index.html',
52+
53+
template: customTemplate
54+
}),
55+
commonjs({
56+
dynamicRequireTargets: [
57+
'node_modules/@newrelic/browser-agent/features/**/*.js'
58+
]
59+
})
60+
]
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { MicroAgent } from '@newrelic/browser-agent/loaders/micro-agent'
2+
3+
var opts = {
4+
info: NREUM.info,
5+
init: NREUM.init
6+
}
7+
window.agent1 = new MicroAgent({ ...opts, info: { ...opts.info, applicationID: 1 } })
8+
window.agent2 = new MicroAgent({ ...opts, info: { ...opts.info, applicationID: 2 } })
9+
10+
// each payload in this test is decorated with data that matches its appId for ease of testing
11+
window.agent1.setCustomAttribute('customAttr', '1')
12+
window.agent2.setCustomAttribute('customAttr', '2')
13+
14+
// each payload in this test is decorated with data that matches its appId for ease of testing
15+
window.agent1.noticeError('1')
16+
window.agent2.noticeError('2')
17+
18+
// each payload in this test is decorated with data that matches its appId for ease of testing
19+
window.agent1.addPageAction('1', { val: 1 })
20+
window.agent2.addPageAction('2', { val: 2 })
21+
22+
// each payload in this test is decorated with data that matches its appId for ease of testing
23+
window.agent1.log('1')
24+
window.agent2.log('2')

tools/test-builds/vite-react-17-wrapper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "index.js",
55
"license": "MIT",
66
"dependencies": {
7-
"@newrelic/browser-agent": "file:../../../temp/newrelic-browser-agent-1.297.0.tgz",
7+
"@newrelic/browser-agent": "file:../../../temp/newrelic-browser-agent-1.298.0.tgz",
88
"react": "^17.0.2",
99
"react-dom": "^17.0.2",
1010
"react-popper": "^1.3.2"

0 commit comments

Comments
 (0)