diff --git a/README.md b/README.md index 34f165d0..3b850562 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 7ec669ca..10568cb8 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -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 diff --git a/load-externals.js b/load-externals.js new file mode 100644 index 00000000..8398b76d --- /dev/null +++ b/load-externals.js @@ -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 +} diff --git a/package.json b/package.json index 96773b87..caf42847 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "THIRD_PARTY_NOTICES.md", "index.js", "nr-hooks.js", + "load-externals.js", "lib/" ], "repository": { diff --git a/tests/unit/load-externals.test.js b/tests/unit/load-externals.test.js new file mode 100644 index 00000000..bec9c45a --- /dev/null +++ b/tests/unit/load-externals.test.js @@ -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') +}) diff --git a/third_party_manifest.json b/third_party_manifest.json index 3e2b8996..7bb3748a 100644 --- a/third_party_manifest.json +++ b/third_party_manifest.json @@ -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, @@ -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"