Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: Added a shim to externalize all 3rd party libraries the Node.js…
Browse files Browse the repository at this point in the history
… agent instruments
  • Loading branch information
bizob2828 committed Mar 13, 2024
1 parent 2c3146a commit 127e3c0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ require('@newrelic/next')
/* ... the rest of your program ... */
```

## Load instrumentation

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.

Add the following to `next.config.js`

```js
const nrExternals = require('@newrelic/next/load-externals')
const nextConfig = {
webpack: nrExternals
};

module.exports = nextConfig;
```

### Custom Next.js servers

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.
Expand Down
2 changes: 1 addition & 1 deletion THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ This product includes source derived from [lockfile-lint](https://github.com/lir

### newrelic

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):
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):

```
Apache License
Expand Down
15 changes: 15 additions & 0 deletions load-externals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const instrumentedLibraries = require('newrelic/lib/instrumentations')() || {}
const libNames = Object.keys(instrumentedLibraries)
module.exports = function loadExternals(config) {
if (config.target.includes('node')) {
config.externals.push(...libNames)
}

return config
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"THIRD_PARTY_NOTICES.md",
"index.js",
"nr-hooks.js",
"load-externals.js",
"lib/"
],
"repository": {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/load-externals.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const tap = require('tap')
const loadExternals = require('../../load-externals')

tap.test('should load libs to webpack externals', async (t) => {
const config = {
target: 'node-20.x',
externals: ['next']
}
loadExternals(config)
t.ok(config.externals.length > 1, 'should add all libraries agent supports to the externals list')
})

tap.test('should not add externals when target is not node', async (t) => {
const config = {
target: 'web',
externals: ['next']
}
loadExternals(config)
t.ok(config.externals.length === 1, 'should not agent libraries when target is not node')
})
10 changes: 5 additions & 5 deletions third_party_manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdated": "Thu Mar 07 2024 11:23:23 GMT-0500 (Eastern Standard Time)",
"lastUpdated": "Mon Mar 11 2024 17:07:20 GMT-0400 (Eastern Daylight Time)",
"projectName": "New Relic Next.js Instrumentation",
"projectUrl": "https://github.com/newrelic/newrelic-node-nextjs",
"includeOptDeps": false,
Expand Down Expand Up @@ -110,15 +110,15 @@
"email": "liran.tal@gmail.com",
"url": "https://github.com/lirantal"
},
"newrelic@11.12.0": {
"newrelic@11.5.0": {
"name": "newrelic",
"version": "11.12.0",
"version": "11.5.0",
"range": "^11.12.0",
"licenses": "Apache-2.0",
"repoUrl": "https://github.com/newrelic/node-newrelic",
"versionedRepoUrl": "https://github.com/newrelic/node-newrelic/tree/v11.12.0",
"versionedRepoUrl": "https://github.com/newrelic/node-newrelic/tree/v11.5.0",
"licenseFile": "node_modules/newrelic/LICENSE",
"licenseUrl": "https://github.com/newrelic/node-newrelic/blob/v11.12.0/LICENSE",
"licenseUrl": "https://github.com/newrelic/node-newrelic/blob/v11.5.0/LICENSE",
"licenseTextSource": "file",
"publisher": "New Relic Node.js agent team",
"email": "nodejs@newrelic.com"
Expand Down

0 comments on commit 127e3c0

Please sign in to comment.