-
Notifications
You must be signed in to change notification settings - Fork 29.7k
fix: ensure onRequestError is invoked when otel enabled #83343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { connection } from 'next/server' | ||
|
|
||
| export async function GET() { | ||
| await connection() | ||
| throw new Error('route-node-error') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { Suspense } from 'react' | ||
|
|
||
| export default function Layout({ children }) { | ||
| return ( | ||
| <html> | ||
| <body> | ||
| <Suspense>{children}</Suspense> | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { connection } from 'next/server' | ||
|
|
||
| export default async function Page() { | ||
| await connection() | ||
| throw new Error('server-page-node-error') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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) | ||
| } | ||
huozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 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', | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 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, | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.