Unverified Commit f9f5be0e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Ensure instrumentation register only call once (#67805)

### What

Ensure instrumentation.js register only called once

### Why

In #67539 we accidentally make it call twice in both next-dev-ser and
next-server. Adding a condition to ensure the one in next-server is only
called in production runtime.
parent 58166a87
......@@ -290,9 +290,6 @@ export default class DevServer extends Server {
const telemetry = new Telemetry({ distDir: this.distDir })
await super.prepareImpl()
await this.startServerSpan
.traceChild('run-instrumentation-hook')
.traceAsyncFn(() => this.runInstrumentationHookIfAvailable())
await this.matchers.reload()
// Store globals again to preserve changes made by the instrumentation hook.
......@@ -623,10 +620,10 @@ export default class DevServer extends Server {
return instrumentationModule
}
private async runInstrumentationHookIfAvailable() {
if (this.instrumentation) {
await this.instrumentation.register?.()
}
protected async runInstrumentationHookIfAvailable() {
await this.startServerSpan
.traceChild('run-instrumentation-hook')
.traceAsyncFn(() => this.instrumentation?.register?.())
}
protected async ensureEdgeFunction({
......
......@@ -309,7 +309,7 @@ export default class NextNodeServer extends BaseServer<
protected async loadInstrumentationModule() {
if (
!this.serverOptions.dev &&
this.nextConfig.experimental.instrumentationHook
!!this.nextConfig.experimental.instrumentationHook
) {
try {
this.instrumentation = await dynamicRequire(
......@@ -332,10 +332,11 @@ export default class NextNodeServer extends BaseServer<
protected async prepareImpl() {
await super.prepareImpl()
await this.runInstrumentationHookIfAvailable()
}
if (this.instrumentation) {
await this.instrumentation.register?.()
}
protected async runInstrumentationHookIfAvailable() {
await this.instrumentation?.register?.()
}
protected loadEnvConfig({
......
export function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('register-log')
}
}
module.exports = {
experimental: {
instrumentationHook: true,
},
}
export default function Page() {
return 'foo'
}
import { nextTestSetup } from 'e2e-utils'
describe('instrumentation-hook - register-once', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
it('should only register once', async () => {
await next.fetch('/foo')
expect(next.cliOutput).toIncludeRepeated('register-log', 1)
})
})
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