Commit 39c3cd62 authored by yiminghe's avatar yiminghe Committed by Jiachi Liu

fix: ensure onRequestError is invoked when otel enabled (#83343)

Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 07d1cbc9
...@@ -1244,8 +1244,7 @@ export async function handler( ...@@ -1244,8 +1244,7 @@ export async function handler(
) )
} }
} catch (err) { } catch (err) {
// if we aren't wrapped by base-server handle here if (!(err instanceof NoFallbackError)) {
if (!activeSpan && !(err instanceof NoFallbackError)) {
await routeModule.onRequestError( await routeModule.onRequestError(
req, req,
err, err,
......
...@@ -448,8 +448,7 @@ export async function handler( ...@@ -448,8 +448,7 @@ export async function handler(
) )
} }
} catch (err) { } catch (err) {
// if we aren't wrapped by base-server handle here if (!(err instanceof NoFallbackError)) {
if (!activeSpan && !(err instanceof NoFallbackError)) {
await routeModule.onRequestError(req, err, { await routeModule.onRequestError(req, err, {
routerKind: 'App Router', routerKind: 'App Router',
routePath: normalizedSrcPage, routePath: normalizedSrcPage,
......
import { connection } from 'next/server'
export async function GET() {
await connection()
throw new Error('route-node-error')
}
import { Suspense } from 'react'
export default function Layout({ children }) {
return (
<html>
<body>
<Suspense>{children}</Suspense>
</body>
</html>
)
}
import { connection } from 'next/server'
export default async function Page() {
await connection()
throw new Error('server-page-node-error')
}
import fs from 'fs'
import fsp from 'fs/promises'
import path from 'path'
const dir = path.join(path.dirname(new URL(import.meta.url).pathname), '../..')
const logPath = path.join(dir, 'output-log.json')
export async function POST(req) {
let payloadString = ''
const decoder = new TextDecoder()
const reader = req.clone().body.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
payloadString += decoder.decode(value)
}
const payload = JSON.parse(payloadString)
const json = fs.existsSync(logPath)
? JSON.parse(await fsp.readFile(logPath, 'utf8'))
: {}
if (!json[payload.message]) {
json[payload.message] = {
payload,
count: 1,
}
} else {
json[payload.message].count++
}
await fsp.writeFile(logPath, JSON.stringify(json, null, 2), 'utf8')
console.log(`[instrumentation] write-log:${payload.message}`)
return new Response(null, { status: 204 })
}
import { registerOTel } from '@vercel/otel'
export const onRequestError = async (err, request, context) => {
await fetch(`http://localhost:${process.env.PORT}/write-log`, {
method: 'POST',
body: JSON.stringify({
message: err.message,
request,
context,
}),
headers: {
'Content-Type': 'application/json',
},
})
}
export function register() {
registerOTel({
serviceName: 'test',
})
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { getOutputLogJson } from '../_testing/utils'
describe('on-request-error - otel', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
packageJson: {
dependencies: {
'@vercel/otel': '^1.13.0',
},
},
})
if (skipped) {
return
}
const outputLogPath = 'output-log.json'
async function validateErrorRecord({
errorMessage,
url,
renderSource,
}: {
errorMessage: string
url: string
renderSource: string | undefined
}) {
// Assert the instrumentation is called
await retry(async () => {
const recordLogLines = next.cliOutput
.split('\n')
.filter((log) => log.includes('[instrumentation] write-log'))
expect(recordLogLines).toEqual(
expect.arrayContaining([expect.stringContaining(errorMessage)])
)
// TODO: remove custom duration in case we increase the default.
}, 5000)
const json = await getOutputLogJson(next, outputLogPath)
const record = json[errorMessage]
const { payload } = record
const { request } = payload
expect(request.path).toBe(url)
expect(record).toMatchObject({
count: 1,
payload: {
message: errorMessage,
request: { method: 'GET', headers: { accept: '*/*' } },
...(renderSource ? { context: { renderSource } } : undefined),
},
})
}
beforeAll(async () => {
await next.patchFile(outputLogPath, '{}')
})
describe('app router', () => {
it('should catch server component page error in node runtime', async () => {
await next.fetch('/server-page')
await validateErrorRecord({
errorMessage: 'server-page-node-error',
url: '/server-page',
renderSource: 'react-server-components',
})
})
it('should catch app routes error in node runtime', async () => {
await next.fetch('/app-route')
await validateErrorRecord({
errorMessage: 'route-node-error',
url: '/app-route',
renderSource: undefined,
})
})
})
})
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