Unverified Commit db764c35 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Add support for async instrumentation register (#48575)

parent 2679ab67
...@@ -23,30 +23,30 @@ export function register() { ...@@ -23,30 +23,30 @@ export function register() {
} }
``` ```
However, we recommend importing files with side effects using `require` from within your `register` function instead. The following example demonstrates a basic usage of `require` in a `register` function: However, we recommend importing files with side effects using `import` from within your `register` function instead. The following example demonstrates a basic usage of `import` in a `register` function:
```ts ```ts
// /instrumentation.ts // /instrumentation.ts
export function register() { export async function register() {
require('package-with-side-effect') await import('package-with-side-effect')
} }
``` ```
By doing this, you can colocate all of your side effects in one place in your code, and avoid any unintended consequences from importing files. By doing this, you can colocate all of your side effects in one place in your code, and avoid any unintended consequences from importing files.
We call `register` in all environments, so it's necessary to conditionally require any code that doesn't support both `edge` and `nodejs`. You can use the environment variable `NEXT_RUNTIME` to get the current environment. Importing an environment-specific code would look like this: We call `register` in all environments, so it's necessary to conditionally import any code that doesn't support both `edge` and `nodejs`. You can use the environment variable `NEXT_RUNTIME` to get the current environment. Importing an environment-specific code would look like this:
```ts ```ts
// /instrumentation.ts // /instrumentation.ts
export function register() { export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') { if (process.env.NEXT_RUNTIME === 'nodejs') {
require('./instrumentation-node') await import('./instrumentation-node')
} }
if (process.env.NEXT_RUNTIME === 'edge') { if (process.env.NEXT_RUNTIME === 'edge') {
require('./instrumentation-edge') await import('./instrumentation-edge')
} }
} }
``` ```
...@@ -60,13 +60,13 @@ npm install @opentelemetry/sdk-node @opentelemetry/resources @opentelemetry/sema ...@@ -60,13 +60,13 @@ npm install @opentelemetry/sdk-node @opentelemetry/resources @opentelemetry/sema
``` ```
Now you can initialize `NodeSDK` in your `instrumentation.ts`. Now you can initialize `NodeSDK` in your `instrumentation.ts`.
OpenTelemetry APIs are not compatible with edge runtime, so you need to make sure that you are importing them only when `process.env.NEXT_RUNTIME === 'nodejs'`. Conditionally importing with an `require` doesn't play well with typescript. We recommend using a conditionally `require`ing new file `instrumentation.node.ts` which can use normal `import`s: OpenTelemetry APIs are not compatible with edge runtime, so you need to make sure that you are importing them only when `process.env.NEXT_RUNTIME === 'nodejs'`. We recommend creating a new file `instrumentation.node.ts` which you conditionally import only when using node:
```ts ```ts
// instrumentation.ts // instrumentation.ts
export function register() { export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') { if (process.env.NEXT_RUNTIME === 'nodejs') {
require('./instrumentation.node.ts') await import('./instrumentation.node.ts')
} }
} }
``` ```
...@@ -90,7 +90,7 @@ sdk.start() ...@@ -90,7 +90,7 @@ sdk.start()
``` ```
Doing this is equivalent to using `@vercel/otel`, but it's possible to modify and extend. Doing this is equivalent to using `@vercel/otel`, but it's possible to modify and extend.
For example, you could use `@opentelemetry/exporter-trace-otlp-grpc` instead of `@opentelemetry/exporter-trace-otlp-http`. For example, you could use `@opentelemetry/exporter-trace-otlp-grpc` instead of `@opentelemetry/exporter-trace-otlp-http` or you can specify more resource attributes.
## Testing your instrumentation ## Testing your instrumentation
......
...@@ -21,7 +21,10 @@ import findUp from 'next/dist/compiled/find-up' ...@@ -21,7 +21,10 @@ import findUp from 'next/dist/compiled/find-up'
import { join as pathJoin, relative, resolve as pathResolve, sep } from 'path' import { join as pathJoin, relative, resolve as pathResolve, sep } from 'path'
import Watchpack from 'next/dist/compiled/watchpack' import Watchpack from 'next/dist/compiled/watchpack'
import { ampValidation } from '../../build/output' import { ampValidation } from '../../build/output'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../../lib/constants' import {
INSTRUMENTATION_HOOK_FILENAME,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
} from '../../lib/constants'
import { fileExists } from '../../lib/file-exists' import { fileExists } from '../../lib/file-exists'
import { findPagesDir } from '../../lib/find-pages-dir' import { findPagesDir } from '../../lib/find-pages-dir'
import loadCustomRoutes from '../../lib/load-custom-routes' import loadCustomRoutes from '../../lib/load-custom-routes'
...@@ -1496,9 +1499,9 @@ export default class DevServer extends Server { ...@@ -1496,9 +1499,9 @@ export default class DevServer extends Server {
const instrumentationHook = await require(pathJoin( const instrumentationHook = await require(pathJoin(
this.distDir, this.distDir,
'server', 'server',
'instrumentation' INSTRUMENTATION_HOOK_FILENAME
)) ))
instrumentationHook.register() await instrumentationHook.register()
} catch (err: any) { } catch (err: any) {
err.message = `An error occurred while loading instrumentation hook: ${err.message}` err.message = `An error occurred while loading instrumentation hook: ${err.message}`
throw err throw err
......
...@@ -340,8 +340,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -340,8 +340,7 @@ export default class NextNodeServer extends BaseServer {
'server', 'server',
INSTRUMENTATION_HOOK_FILENAME INSTRUMENTATION_HOOK_FILENAME
)) ))
await instrumentationHook.register?.()
instrumentationHook.register?.()
} catch (err: any) { } catch (err: any) {
if (err.code !== 'MODULE_NOT_FOUND') { if (err.code !== 'MODULE_NOT_FOUND') {
err.message = `An error occurred while loading instrumentation hook: ${err.message}` err.message = `An error occurred while loading instrumentation hook: ${err.message}`
......
...@@ -59,9 +59,34 @@ export type AdapterOptions = { ...@@ -59,9 +59,34 @@ export type AdapterOptions = {
IncrementalCache?: typeof import('../lib/incremental-cache').IncrementalCache IncrementalCache?: typeof import('../lib/incremental-cache').IncrementalCache
} }
async function registerInstrumentation() {
if (
'_ENTRIES' in globalThis &&
_ENTRIES.middleware_instrumentation &&
_ENTRIES.middleware_instrumentation.register
) {
try {
await _ENTRIES.middleware_instrumentation.register()
} catch (err: any) {
err.message = `An error occurred while loading instrumentation hook: ${err.message}`
throw err
}
}
}
let registerInstrumentationPromise: Promise<void> | null = null
function ensureInstrumentationRegistered() {
if (!registerInstrumentationPromise) {
registerInstrumentationPromise = registerInstrumentation()
}
return registerInstrumentationPromise
}
export async function adapter( export async function adapter(
params: AdapterOptions params: AdapterOptions
): Promise<FetchEventResult> { ): Promise<FetchEventResult> {
await ensureInstrumentationRegistered()
// TODO-APP: use explicit marker for this // TODO-APP: use explicit marker for this
const isEdgeRendering = typeof self.__BUILD_MANIFEST !== 'undefined' const isEdgeRendering = typeof self.__BUILD_MANIFEST !== 'undefined'
...@@ -334,16 +359,6 @@ export function enhanceGlobals() { ...@@ -334,16 +359,6 @@ export function enhanceGlobals() {
configurable: false, configurable: false,
}) })
if ( // Eagerly fire instrumentation hook to make the startup faster.
'_ENTRIES' in globalThis && void ensureInstrumentationRegistered()
_ENTRIES.middleware_instrumentation &&
_ENTRIES.middleware_instrumentation.register
) {
try {
_ENTRIES.middleware_instrumentation.register()
} catch (err: any) {
err.message = `An error occurred while loading instrumentation hook: ${err.message}`
throw err
}
}
} }
...@@ -82,6 +82,20 @@ describe('Instrumentation Hook', () => { ...@@ -82,6 +82,20 @@ describe('Instrumentation Hook', () => {
}) })
}) })
describeCase('with-async-node-page', ({ next }) => {
it('with-async-node-page should run the instrumentation hook', async () => {
const page = await next.render('/')
expect(page).toContain('Node - finished: true')
})
})
describeCase('with-async-edge-page', ({ next }) => {
it('with-async-edge-page should run the instrumentation hook', async () => {
const page = await next.render('/')
expect(page).toContain('Edge - finished: true')
})
})
describeCase('general', ({ next, isNextDev }) => { describeCase('general', ({ next, isNextDev }) => {
it('should not overlap with a instrumentation page', async () => { it('should not overlap with a instrumentation page', async () => {
const page = await next.render('/instrumentation') const page = await next.render('/instrumentation')
......
/*global globalThis*/
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
export async function register() {
await sleep(1000)
globalThis.instrumentationFinished = true
}
export function register() { export async function register() {
if (process.env.NEXT_RUNTIME === 'edge') { if (process.env.NEXT_RUNTIME === 'edge') {
console.log('instrumentation hook on the edge') console.log('instrumentation hook on the edge')
const { register } = await import('./instrumentation.edge')
await register()
} else if (process.env.NEXT_RUNTIME === 'nodejs') { } else if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('instrumentation hook on nodejs') console.log('instrumentation hook on nodejs')
} else { } else {
......
export default function Page({ finished }) {
return <p>{`Edge - finished: ${finished}`}</p>
}
export function getServerSideProps() {
return {
props: {
finished: Boolean(globalThis.instrumentationFinished),
},
}
}
export const config = {
runtime: 'experimental-edge',
}
export async function register() {
if (process.env.NEXT_RUNTIME === 'edge') {
console.log('instrumentation hook on the edge')
} else if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('instrumentation hook on nodejs')
const { register } = await import('./instrumentation.node')
await register()
} else {
await require('this should fail')
}
}
/*global globalThis*/
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
export async function register() {
await sleep(1000)
globalThis.instrumentationFinished = true
}
export default function Page({ finished }) {
return <p>{`Node - finished: ${finished}`}</p>
}
export async function getServerSideProps() {
return {
props: {
finished: Boolean(globalThis.instrumentationFinished),
},
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment