Skip to content

Commit 35a5ec7

Browse files
authored
Merge pull request newrelic#175 from newrelic/externalize-libs
feat: Added a shim to externalize all 3rd party libraries the Node.js agent instruments
2 parents 9699c33 + b55d8e1 commit 35a5ec7

File tree

6 files changed

+64
-6
lines changed

6 files changed

+64
-6
lines changed

merged/nextjs/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ require('@newrelic/next')
3131
/* ... the rest of your program ... */
3232
```
3333

34+
## Load instrumentation
35+
36+
Since Next.js uses webpack to bundle, auto-instrumentation of 3rd party libraries will not work without a shim. The Next.js instrumentation will load as the Next.js project externalizes next.js files. However, other libraries that are being used will not be externalized and thus not instrumented. Follow these steps to ensure your calls to libraries are properly instrumented.
37+
38+
Add the following to `next.config.js`
39+
40+
```js
41+
const nrExternals = require('@newrelic/next/load-externals')
42+
const nextConfig = {
43+
webpack: nrExternals
44+
};
45+
46+
module.exports = nextConfig;
47+
```
48+
3449
### Custom Next.js servers
3550

3651
If you are using next as a [custom server](https://nextjs.org/docs/advanced-features/custom-server), you're probably not running your application with the `next` CLI. In that scenario we recommend running the Next.js instrumentation as follows.

merged/nextjs/THIRD_PARTY_NOTICES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ This product includes source derived from [lockfile-lint](https://github.com/lir
964964

965965
### newrelic
966966

967-
This product includes source derived from [newrelic](https://github.com/newrelic/node-newrelic) ([v11.12.0](https://github.com/newrelic/node-newrelic/tree/v11.12.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/node-newrelic/blob/v11.12.0/LICENSE):
967+
This product includes source derived from [newrelic](https://github.com/newrelic/node-newrelic) ([v11.5.0](https://github.com/newrelic/node-newrelic/tree/v11.5.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/node-newrelic/blob/v11.5.0/LICENSE):
968968

969969
```
970970
Apache License

merged/nextjs/load-externals.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
const instrumentedLibraries = require('newrelic/lib/instrumentations')() || {}
8+
const libNames = Object.keys(instrumentedLibraries)
9+
module.exports = function loadExternals(config) {
10+
if (config.target.includes('node')) {
11+
config.externals.push(...libNames)
12+
}
13+
14+
return config
15+
}

merged/nextjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"THIRD_PARTY_NOTICES.md",
2121
"index.js",
2222
"nr-hooks.js",
23+
"load-externals.js",
2324
"lib/"
2425
],
2526
"repository": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const tap = require('tap')
9+
const loadExternals = require('../../load-externals')
10+
11+
tap.test('should load libs to webpack externals', async (t) => {
12+
const config = {
13+
target: 'node-20.x',
14+
externals: ['next']
15+
}
16+
loadExternals(config)
17+
t.ok(config.externals.length > 1, 'should add all libraries agent supports to the externals list')
18+
})
19+
20+
tap.test('should not add externals when target is not node', async (t) => {
21+
const config = {
22+
target: 'web',
23+
externals: ['next']
24+
}
25+
loadExternals(config)
26+
t.ok(config.externals.length === 1, 'should not agent libraries when target is not node')
27+
})

merged/nextjs/third_party_manifest.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lastUpdated": "Thu Mar 07 2024 11:23:23 GMT-0500 (Eastern Standard Time)",
2+
"lastUpdated": "Mon Mar 11 2024 17:07:20 GMT-0400 (Eastern Daylight Time)",
33
"projectName": "New Relic Next.js Instrumentation",
44
"projectUrl": "https://github.com/newrelic/newrelic-node-nextjs",
55
"includeOptDeps": false,
@@ -110,15 +110,15 @@
110110
"email": "[email protected]",
111111
"url": "https://github.com/lirantal"
112112
},
113-
"newrelic@11.12.0": {
113+
"newrelic@11.5.0": {
114114
"name": "newrelic",
115-
"version": "11.12.0",
115+
"version": "11.5.0",
116116
"range": "^11.12.0",
117117
"licenses": "Apache-2.0",
118118
"repoUrl": "https://github.com/newrelic/node-newrelic",
119-
"versionedRepoUrl": "https://github.com/newrelic/node-newrelic/tree/v11.12.0",
119+
"versionedRepoUrl": "https://github.com/newrelic/node-newrelic/tree/v11.5.0",
120120
"licenseFile": "node_modules/newrelic/LICENSE",
121-
"licenseUrl": "https://github.com/newrelic/node-newrelic/blob/v11.12.0/LICENSE",
121+
"licenseUrl": "https://github.com/newrelic/node-newrelic/blob/v11.5.0/LICENSE",
122122
"licenseTextSource": "file",
123123
"publisher": "New Relic Node.js agent team",
124124
"email": "[email protected]"

0 commit comments

Comments
 (0)