Skip to content

Commit

Permalink
Stablize instrumentation.js (#68853)
Browse files Browse the repository at this point in the history
### What

- After adding a new API `onRequestError()` and a few fixes on `register()` for `instrumentation.js`, it's time to promote it as stable API.
- Add warning `"experimental.instrumentationHook is no longer needed to be configured in Next.js"` when you configure a `instrumentationHook` option in next.config.js

Related PRs
#67539, #67671, #67703, #67848, #67859, #67856, #67805, #68616, #68672, #68764, #68845 #68976, #68983

Closes NDX-89
  • Loading branch information
huozhi committed Aug 21, 2024
1 parent 8ff0b37 commit c9daf1f
Show file tree
Hide file tree
Showing 37 changed files with 53 additions and 159 deletions.
9 changes: 1 addition & 8 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,15 +862,9 @@ export default async function build(
)

const rootDir = path.join((pagesDir || appDir)!, '..')
const instrumentationHookEnabled = Boolean(
config.experimental.instrumentationHook
)

const includes = [
middlewareDetectionRegExp,
...(instrumentationHookEnabled
? [instrumentationHookDetectionRegExp]
: []),
instrumentationHookDetectionRegExp,
]

const rootPaths = Array.from(await getFilesInDir(rootDir))
Expand Down Expand Up @@ -1351,7 +1345,6 @@ export default async function build(
currentEntrypoints,
currentEntryIssues,
manifestLoader,
nextConfig: config,
devRewrites: undefined,
productionRewrites: customRoutes.rewrites,
logErrors: false,
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
.optional(),
optimizePackageImports: z.array(z.string()).optional(),
optimizeServerReact: z.boolean().optional(),
instrumentationHook: z.boolean().optional(),
clientTraceMetadata: z.array(z.string()).optional(),
turbotrace: z
.object({
Expand Down
6 changes: 0 additions & 6 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,6 @@ export interface ExperimentalConfig {
*/
webpackMemoryOptimizations?: boolean

/**
*
*/
instrumentationHook?: boolean

/**
* The array of the meta tags to the client injected by tracing propagation data.
*/
Expand Down Expand Up @@ -1036,7 +1031,6 @@ export const defaultConfig: NextConfig = {
turbotrace: undefined,
typedRoutes: false,
typedEnv: false,
instrumentationHook: false,
clientTraceMetadata: undefined,
parallelServerCompiles: false,
parallelServerBuildTraces: false,
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,13 @@ function assignDefaults(
silent
)

warnOptionHasBeenDeprecated(
result,
'experimental.instrumentationHook',
'`experimental.instrumentationHook` is no longer needed to be configured in Next.js',
silent
)

warnOptionHasBeenMovedOutOfExperimental(
result,
'bundlePagesExternals',
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ export async function createHotReloaderTurbopack(

currentEntryIssues,
manifestLoader,
nextConfig: opts.nextConfig,
devRewrites: opts.fsChecker.rewrites,
productionRewrites: undefined,
logErrors: true,
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/server/dev/turbopack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ export async function handleEntrypoints({

currentEntryIssues,
manifestLoader,
nextConfig,
devRewrites,
productionRewrites,
logErrors,
Expand All @@ -721,7 +720,6 @@ export async function handleEntrypoints({

currentEntryIssues: EntryIssuesMap
manifestLoader: TurbopackManifestLoader
nextConfig: NextConfigComplete
devRewrites: SetupOpts['fsChecker']['rewrites'] | undefined
productionRewrites: CustomRoutes['rewrites'] | undefined
logErrors: boolean
Expand Down Expand Up @@ -793,7 +791,7 @@ export async function handleEntrypoints({

currentEntrypoints.global.middleware = middleware

if (nextConfig.experimental.instrumentationHook && instrumentation) {
if (instrumentation) {
const processInstrumentation = async (
name: string,
prop: 'nodeJs' | 'edge'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,7 @@ async function startWatcher(opts: SetupOpts) {
]
continue
}
if (
isInstrumentationHookFile(rootFile) &&
nextConfig.experimental.instrumentationHook
) {
if (isInstrumentationHookFile(rootFile)) {
serverFields.actualInstrumentationHookFile = rootFile
await propagateServerField(
opts,
Expand Down
5 changes: 1 addition & 4 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ export default class NextNodeServer extends BaseServer<
}

protected async loadInstrumentationModule() {
if (
!this.serverOptions.dev &&
!!this.nextConfig.experimental.instrumentationHook
) {
if (!this.serverOptions.dev) {
try {
this.instrumentation = await dynamicRequire(
resolve(
Expand Down
1 change: 0 additions & 1 deletion test/e2e/app-dir/next-after-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module.exports = {
experimental: {
after: true,
testProxy: true,
instrumentationHook: true,
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ describe('instrumentation-hook-rsc', () => {
describe('instrumentation', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
nextConfig: {
experimental: {
instrumentationHook: true,
},
},
skipDeployment: true,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ module.exports = {
flyingShuttle: {
mode: 'full',
},
instrumentationHook: true,
},
}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/general/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
36 changes: 9 additions & 27 deletions test/e2e/instrumentation-hook/instrumentation-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,15 @@ const describeCase = (
})
}
describe('Instrumentation Hook', () => {
// TODO: investigate the failure with esm import
// createNextDescribe(
// 'with-esm-import',
// {
// files: path.join(__dirname, 'with-esm-import'),
// nextConfig: {
// experimental: {
// instrumentationHook: true,
// },
// },
// dependencies: {
// // This test is mostly for compatibility with this package
// '@vercel/otel': 'latest',
// },
// skipDeployment: true,
// },
// ({ next }) => {
// eslint-disable-next-line jest/no-commented-out-tests
// it('with-esm-import should run the instrumentation hook', async () => {
// await next.render('/')
// await check(
// () => next.cliOutput,
// /register in instrumentation\.js is running/
// )
// })
// }
// )
describeCase('with-esm-import', ({ next }) => {
it('with-esm-import should run the instrumentation hook', async () => {
await next.render('/')
await check(
() => next.cliOutput,
/register in instrumentation\.js is running/
)
})
})

describeCase('with-middleware', ({ next }) => {
it('with-middleware should run the instrumentation hook', async () => {
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/register-once/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-edge-api/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-edge-page/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
1 change: 1 addition & 0 deletions test/e2e/instrumentation-hook/with-esm-import/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as otel from '@vercel/otel'
import * as mod from 'my-lib'

export function register() {
console.log('register in instrumentation.js is running')
// make sure that this is not tree-shaken
if (process.env.DOESNT_EXIST_1234) otel()
if (process.env.DOESNT_EXIST_1234) mod.c()
}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-esm-import/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-middleware/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-node-api/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/instrumentation-hook/with-node-page/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/on-request-error/basic/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/on-request-error/dynamic-routes/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/on-request-error/isr/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
6 changes: 1 addition & 5 deletions test/e2e/on-request-error/server-action-error/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
experimental: {
instrumentationHook: true,
clientTraceMetadata: [
'my-parent-span-id',
'my-test-key-1',
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/opentelemetry/instrumentation/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
4 changes: 2 additions & 2 deletions test/e2e/opentelemetry/instrumentation/opentelemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('opentelemetry', () => {
},
},
]) {
// turbopack does not support experimental.instrumentationHook
// turbopack does not support instrumentation.js
;(process.env.TURBOPACK || process.env.__NEXT_EXPERIMENTAL_PPR
? describe.skip
: describe)(env.name, () => {
Expand Down Expand Up @@ -836,7 +836,7 @@ describe('opentelemetry with disabled fetch tracing', () => {
await collector.shutdown()
})

// turbopack does not support experimental.instrumentationHook
// turbopack does not support instrumentation.js
;(process.env.TURBOPACK || process.env.__NEXT_EXPERIMENTAL_PPR
? describe.skip
: describe)('root context', () => {
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/rsc-layers-transform/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Config Experimental Warning', () => {
experimental: {
workerThreads: true,
scrollRestoration: true,
instrumentationHook: true,
parallelServerCompiles: true,
cpus: 2,
}
}
Expand All @@ -162,7 +162,7 @@ describe('Config Experimental Warning', () => {
experimental: {
workerThreads: true,
scrollRestoration: true,
instrumentationHook: true,
parallelServerCompiles: true,
cpus: 2,
}
}
Expand All @@ -172,7 +172,7 @@ describe('Config Experimental Warning', () => {
expect(stdout).toMatch(' · cpus')
expect(stdout).toMatch(' · workerThreads')
expect(stdout).toMatch(' · scrollRestoration')
expect(stdout).toMatch(' · instrumentationHook')
expect(stdout).toMatch(' · parallelServerCompiles')
})

it('should show unrecognized experimental features in warning but not in start log experiments section', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
instrumentationHook: true,
},
}
module.exports = {}

0 comments on commit c9daf1f

Please sign in to comment.