Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
39c3cd62
Commit
39c3cd62
authored
Sep 03, 2025
by
yiminghe
Committed by
Jiachi Liu
Sep 12, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: ensure onRequestError is invoked when otel enabled (#83343)
Co-authored-by:
Jiachi Liu
<
inbox@huozhi.im
>
parent
07d1cbc9
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
169 additions
and
4 deletions
+169
-4
app-page.ts
packages/next/src/build/templates/app-page.ts
+1
-2
app-route.ts
packages/next/src/build/templates/app-route.ts
+1
-2
route.js
test/e2e/on-request-error/otel/app/app-route/route.js
+6
-0
layout.js
test/e2e/on-request-error/otel/app/layout.js
+11
-0
page.js
test/e2e/on-request-error/otel/app/server-page/page.js
+6
-0
route.js
test/e2e/on-request-error/otel/app/write-log/route.js
+40
-0
instrumentation.js
test/e2e/on-request-error/otel/instrumentation.js
+21
-0
otel.test.ts
test/e2e/on-request-error/otel/otel.test.ts
+83
-0
No files found.
packages/next/src/build/templates/app-page.ts
View file @
39c3cd62
...
@@ -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,
...
...
packages/next/src/build/templates/app-route.ts
View file @
39c3cd62
...
@@ -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,
...
...
test/e2e/on-request-error/otel/app/app-route/route.js
0 → 100644
View file @
39c3cd62
import
{
connection
}
from
'next/server'
export
async
function
GET
()
{
await
connection
()
throw
new
Error
(
'route-node-error'
)
}
test/e2e/on-request-error/otel/app/layout.js
0 → 100644
View file @
39c3cd62
import
{
Suspense
}
from
'react'
export
default
function
Layout
({
children
})
{
return
(
<
html
>
<
body
>
<
Suspense
>
{
children
}
<
/Suspense
>
<
/body
>
<
/html
>
)
}
test/e2e/on-request-error/otel/app/server-page/page.js
0 → 100644
View file @
39c3cd62
import
{
connection
}
from
'next/server'
export
default
async
function
Page
()
{
await
connection
()
throw
new
Error
(
'server-page-node-error'
)
}
test/e2e/on-request-error/otel/app/write-log/route.js
0 → 100644
View file @
39c3cd62
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
})
}
test/e2e/on-request-error/otel/instrumentation.js
0 → 100644
View file @
39c3cd62
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'
,
})
}
test/e2e/on-request-error/otel/otel.test.ts
0 → 100644
View file @
39c3cd62
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
,
})
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment