Unverified Commit 7b48a1e2 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fetching logging on edge (#56108)

Support fetching logging working with edge runtime. When you enable edge runtime and experimental logging config in `next.config.js` you should be able to see the data fetching logs like edge runtime

```
 GET /default-cache 200 in 4304ms
 │ GET https://next-data-api-endpoint.vercel.app/api/random?no-cache 200 in 373ms (cache: SKIP)
 │  │  Cache missed reason: (cache: no-cache)
 │  │ GET https://next-data-api-endpoint.vercel.app/api/random?force-cache 200 in 163ms (cache: SKIP)
 │  │  │  Cache missed reason: (cache-control: no-cache (hard refresh))
 │  │ GET https://next-data-api-endpoint.vercel.app/api/random?revalidate-0 200 in 196ms (cache: SKIP)
 ```

Closes #55495
Closes NEXT-1661
parent 2ff2c0b4
...@@ -148,7 +148,11 @@ export function getRender({ ...@@ -148,7 +148,11 @@ export function getRender({
const extendedRes = new WebNextResponse() const extendedRes = new WebNextResponse()
handler(extendedReq, extendedRes) handler(extendedReq, extendedRes)
const result = await extendedRes.toResponse()
return await extendedRes.toResponse() // fetchMetrics is attached to the web request that going through the server,
// wait for the handler result is ready and attach it back to the original request.
;(request as any).fetchMetrics = extendedReq.fetchMetrics
return result
} }
} }
import type { AsyncLocalStorage } from 'async_hooks' import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../../server/lib/incremental-cache' import type { IncrementalCache } from '../../server/lib/incremental-cache'
import type { DynamicServerError } from './hooks-server-context' import type { DynamicServerError } from './hooks-server-context'
import type { FetchMetrics } from '../../server/base-http'
import { createAsyncLocalStorage } from './async-local-storage' import { createAsyncLocalStorage } from './async-local-storage'
export interface StaticGenerationStore { export interface StaticGenerationStore {
...@@ -36,16 +37,7 @@ export interface StaticGenerationStore { ...@@ -36,16 +37,7 @@ export interface StaticGenerationStore {
tags?: string[] tags?: string[]
revalidatedTags?: string[] revalidatedTags?: string[]
fetchMetrics?: Array<{ fetchMetrics?: FetchMetrics
url: string
idx: number
end: number
start: number
method: string
status: number
cacheReason: string
cacheStatus: 'hit' | 'miss' | 'skip'
}>
isDraftMode?: boolean isDraftMode?: boolean
} }
......
...@@ -267,6 +267,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -267,6 +267,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
`Invariant: Render expects to have staticGenerationAsyncStorage, none found` `Invariant: Render expects to have staticGenerationAsyncStorage, none found`
) )
} }
staticGenerationStore.fetchMetrics = [] staticGenerationStore.fetchMetrics = []
extraRenderResultMeta.fetchMetrics = staticGenerationStore.fetchMetrics extraRenderResultMeta.fetchMetrics = staticGenerationStore.fetchMetrics
......
...@@ -11,6 +11,17 @@ export interface BaseNextRequestConfig { ...@@ -11,6 +11,17 @@ export interface BaseNextRequestConfig {
trailingSlash?: boolean | undefined trailingSlash?: boolean | undefined
} }
export type FetchMetrics = Array<{
url: string
idx: number
end: number
start: number
method: string
status: number
cacheReason: string
cacheStatus: 'hit' | 'miss' | 'skip'
}>
export abstract class BaseNextRequest<Body = any> { export abstract class BaseNextRequest<Body = any> {
protected _cookies: NextApiRequestCookies | undefined protected _cookies: NextApiRequestCookies | undefined
public abstract headers: IncomingHttpHeaders public abstract headers: IncomingHttpHeaders
......
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http' import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'
import type { FetchMetrics } from './index'
import { toNodeOutgoingHttpHeaders } from '../web/utils' import { toNodeOutgoingHttpHeaders } from '../web/utils'
import { BaseNextRequest, BaseNextResponse } from './index' import { BaseNextRequest, BaseNextResponse } from './index'
export class WebNextRequest extends BaseNextRequest<ReadableStream | null> { export class WebNextRequest extends BaseNextRequest<ReadableStream | null> {
public request: Request public request: Request
public headers: IncomingHttpHeaders public headers: IncomingHttpHeaders
public fetchMetrics?: FetchMetrics
constructor(request: Request) { constructor(request: Request) {
const url = new URL(request.url) const url = new URL(request.url)
......
...@@ -1845,6 +1845,10 @@ export default class NextNodeServer extends BaseServer { ...@@ -1845,6 +1845,10 @@ export default class NextNodeServer extends BaseServer {
getRequestMeta(params.req, '_nextIncrementalCache'), getRequestMeta(params.req, '_nextIncrementalCache'),
}) })
if (result.fetchMetrics) {
;(params.req as any).fetchMetrics = result.fetchMetrics
}
if (!params.res.statusCode || params.res.statusCode < 400) { if (!params.res.statusCode || params.res.statusCode < 400) {
params.res.statusCode = result.response.status params.res.statusCode = result.response.status
params.res.statusMessage = result.response.statusText params.res.statusMessage = result.response.statusText
......
...@@ -23,6 +23,7 @@ import { PrerenderManifest } from '../../build' ...@@ -23,6 +23,7 @@ import { PrerenderManifest } from '../../build'
class NextRequestHint extends NextRequest { class NextRequestHint extends NextRequest {
sourcePage: string sourcePage: string
fetchMetrics?: FetchEventResult['fetchMetrics']
constructor(params: { constructor(params: {
init: RequestInit init: RequestInit
...@@ -329,5 +330,6 @@ export async function adapter( ...@@ -329,5 +330,6 @@ export async function adapter(
return { return {
response: finalResponse, response: finalResponse,
waitUntil: Promise.all(event[waitUntilSymbol]), waitUntil: Promise.all(event[waitUntilSymbol]),
fetchMetrics: request.fetchMetrics,
} }
} }
...@@ -4,6 +4,7 @@ import type { NextFetchEvent } from './spec-extension/fetch-event' ...@@ -4,6 +4,7 @@ import type { NextFetchEvent } from './spec-extension/fetch-event'
import type { NextResponse } from './spec-extension/response' import type { NextResponse } from './spec-extension/response'
import type { CloneableBody } from '../body-streams' import type { CloneableBody } from '../body-streams'
import type { OutgoingHttpHeaders } from 'http' import type { OutgoingHttpHeaders } from 'http'
import type { FetchMetrics } from '../base-http'
export interface RequestData { export interface RequestData {
geo?: { geo?: {
...@@ -37,6 +38,7 @@ export type NodejsRequestData = Omit<RequestData, 'body'> & { ...@@ -37,6 +38,7 @@ export type NodejsRequestData = Omit<RequestData, 'body'> & {
export interface FetchEventResult { export interface FetchEventResult {
response: Response response: Response
waitUntil: Promise<any> waitUntil: Promise<any>
fetchMetrics?: FetchMetrics
} }
export type NextMiddlewareResult = export type NextMiddlewareResult =
......
...@@ -49,102 +49,126 @@ createNextDescribe( ...@@ -49,102 +49,126 @@ createNextDescribe(
}, },
}, },
({ next, isNextDev }) => { ({ next, isNextDev }) => {
describe('with verbose logging', () => { function runTests({ hasLogging }: { hasLogging: boolean }) {
it('should only log requests in dev mode', async () => { if (hasLogging) {
const outputIndex = next.cliOutput.length it('should only log requests in dev mode', async () => {
await next.fetch('/default-cache')
await check(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
const hasLogs = logs.includes('GET /default-cache 200')
if (isNextDev && hasLogs) {
return 'success'
}
if (!isNextDev && !hasLogs) {
return 'success'
}
}, 'success')
})
if (isNextDev) {
it("should log 'skip' cache status with a reason when cache: 'no-cache' is used", async () => {
const outputIndex = next.cliOutput.length const outputIndex = next.cliOutput.length
await next.fetch('/default-cache') await next.fetch('/default-cache')
await check(() => { await check(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex)) const logs = stripAnsi(next.cliOutput.slice(outputIndex))
const hasLogs = logs.includes('GET /default-cache 200')
const logEntry = logs.find((log) =>
log.url.includes('api/random?no-cache')
)
// expend full url if (isNextDev && hasLogs) {
expect(logs.every((log) => log.url.includes('..'))).toBe(false) return 'success'
}
if (logEntry?.cache === 'cache: no-cache') { if (!isNextDev && !hasLogs) {
return 'success' return 'success'
} }
}, 'success') }, 'success')
}) })
it("should log 'skip' cache status with a reason when revalidate: 0 is used", async () => { if (isNextDev) {
const outputIndex = next.cliOutput.length it("should log 'skip' cache status with a reason when cache: 'no-cache' is used", async () => {
await next.fetch('/default-cache') const outputIndex = next.cliOutput.length
await check(() => { await next.fetch('/default-cache')
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) => await check(() => {
log.url.includes('api/random?revalidate-0') const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
)
if (logEntry?.cache === 'revalidate: 0') { const logEntry = logs.find((log) =>
return 'success' log.url.includes('api/random?no-cache')
} )
}, 'success')
})
it("should log 'skip' cache status with a reason when the browser indicates caching should be ignored", async () => { // expend full url
const outputIndex = next.cliOutput.length expect(logs.every((log) => log.url.includes('..'))).toBe(false)
await next.fetch('/default-cache', {
headers: { 'Cache-Control': 'no-cache' }, if (logEntry?.cache === 'cache: no-cache') {
return 'success'
}
}, 'success')
}) })
await check(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) => it("should log 'skip' cache status with a reason when revalidate: 0 is used", async () => {
log.url.includes('api/random?auto-cache') const outputIndex = next.cliOutput.length
) await next.fetch('/default-cache')
await check(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
if (logEntry?.cache === 'cache-control: no-cache (hard refresh)') { const logEntry = logs.find((log) =>
return 'success' log.url.includes('api/random?revalidate-0')
)
if (logEntry?.cache === 'revalidate: 0') {
return 'success'
}
}, 'success')
})
it("should log 'skip' cache status with a reason when the browser indicates caching should be ignored", async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache', {
headers: { 'Cache-Control': 'no-cache' },
})
await check(() => {
const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
const logEntry = logs.find((log) =>
log.url.includes('api/random?auto-cache')
)
if (
logEntry?.cache === 'cache-control: no-cache (hard refresh)'
) {
return 'success'
}
}, 'success')
})
}
} else {
it('should not log fetch requests at all', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache')
await check(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
if (logs.includes('GET /default-cache 200')) {
return 'fail'
} }
return 'success'
}, 'success') }, 'success')
}) })
} }
}
describe('with verbose logging', () => {
runTests({ hasLogging: true })
}) })
describe('with default logging', () => { describe('with verbose logging for edge runtime', () => {
beforeAll(async () => { beforeAll(async () => {
await next.stop() await next.stop()
await next.deleteFile('next.config.js') const layoutContent = await next.readFile('app/layout.js')
await next.patchFile(
'app/layout.js',
layoutContent + `\nexport const runtime = 'edge'`
)
await next.start() await next.start()
}) })
it('should not log fetch requests at all', async () => { runTests({ hasLogging: false })
const outputIndex = next.cliOutput.length })
await next.fetch('/default-cache')
await check(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
if (logs.includes('GET /default-cache 200')) {
return 'fail'
}
return 'success' describe('with default logging', () => {
}, 'success') beforeAll(async () => {
await next.stop()
await next.deleteFile('next.config.js')
await next.start()
}) })
runTests({ hasLogging: false })
}) })
} }
) )
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