Unverified Commit 5ea70b85 authored by Nabeel Sulieman's avatar Nabeel Sulieman Committed by GitHub

Next Cache Telemetry (#47939)

### What?

Report additional telemetry related to cached fetches:

- Fetch Index number to group related fetches (cache-get, cache-set,
origin)
- Origin URL map cache key to original upstream URL

### Why?

This is needed for fetch cache telemetry on the Vercel platform.

### How?

Telemetry is provided through optional parameters added to the fetch
call configuration. It is similar to the `next: {revalidate: X}` and
`{next: { internal: true }}` fields.

The origin URL and fetch index are calculated in the patch-fetch
function and are passed down to the caching classes as needed. These
fields are optional and ignored by the `FileSystemCache`.
parent b5f785aa
...@@ -24,6 +24,8 @@ export interface StaticGenerationStore { ...@@ -24,6 +24,8 @@ export interface StaticGenerationStore {
dynamicUsageDescription?: string dynamicUsageDescription?: string
dynamicUsageStack?: string dynamicUsageStack?: string
nextFetchId?: number
} }
export type StaticGenerationAsyncStorage = export type StaticGenerationAsyncStorage =
......
...@@ -5,6 +5,13 @@ import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' ...@@ -5,6 +5,13 @@ import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
interface NextFetchCacheParams {
internal?: boolean
fetchType?: string
fetchIdx?: number
originUrl?: string
}
export default class FetchCache implements CacheHandler { export default class FetchCache implements CacheHandler {
private headers: Record<string, string> private headers: Record<string, string>
private cacheEndpoint?: string private cacheEndpoint?: string
...@@ -66,7 +73,12 @@ export default class FetchCache implements CacheHandler { ...@@ -66,7 +73,12 @@ export default class FetchCache implements CacheHandler {
} }
} }
public async get(key: string, fetchCache?: boolean) { public async get(
key: string,
fetchCache?: boolean,
originUrl?: string,
fetchIdx?: number
) {
if (!fetchCache) return null if (!fetchCache) return null
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
...@@ -81,13 +93,18 @@ export default class FetchCache implements CacheHandler { ...@@ -81,13 +93,18 @@ export default class FetchCache implements CacheHandler {
if (!data && this.cacheEndpoint) { if (!data && this.cacheEndpoint) {
try { try {
const start = Date.now() const start = Date.now()
const fetchParams: NextFetchCacheParams = {
internal: true,
fetchType: 'fetch-get',
originUrl,
fetchIdx,
}
const res = await fetch( const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/${key}`, `${this.cacheEndpoint}/v1/suspense-cache/${key}`,
{ {
method: 'GET', method: 'GET',
headers: this.headers, headers: this.headers,
// @ts-expect-error next: fetchParams as NextFetchRequestConfig,
next: { internal: true },
} }
) )
...@@ -150,7 +167,9 @@ export default class FetchCache implements CacheHandler { ...@@ -150,7 +167,9 @@ export default class FetchCache implements CacheHandler {
public async set( public async set(
key: string, key: string,
data: CacheHandlerValue['value'], data: CacheHandlerValue['value'],
fetchCache?: boolean fetchCache?: boolean,
originUrl?: string,
fetchIdx?: number
) { ) {
if (!fetchCache) return if (!fetchCache) return
...@@ -174,14 +193,19 @@ export default class FetchCache implements CacheHandler { ...@@ -174,14 +193,19 @@ export default class FetchCache implements CacheHandler {
data.data.headers['cache-control'] data.data.headers['cache-control']
} }
const body = JSON.stringify(data) const body = JSON.stringify(data)
const fetchParams: NextFetchCacheParams = {
internal: true,
fetchType: 'fetch-set',
originUrl,
fetchIdx,
}
const res = await fetch( const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/${key}`, `${this.cacheEndpoint}/v1/suspense-cache/${key}`,
{ {
method: 'POST', method: 'POST',
headers: this.headers, headers: this.headers,
body: body, body: body,
// @ts-expect-error next: fetchParams as NextFetchRequestConfig,
next: { internal: true },
} }
) )
......
...@@ -39,7 +39,9 @@ export class CacheHandler { ...@@ -39,7 +39,9 @@ export class CacheHandler {
public async get( public async get(
_key: string, _key: string,
_fetchCache?: boolean _fetchCache?: boolean,
_originUrl?: string,
_fetchIdx?: number
): Promise<CacheHandlerValue | null> { ): Promise<CacheHandlerValue | null> {
return {} as any return {} as any
} }
...@@ -47,7 +49,9 @@ export class CacheHandler { ...@@ -47,7 +49,9 @@ export class CacheHandler {
public async set( public async set(
_key: string, _key: string,
_data: IncrementalCacheValue | null, _data: IncrementalCacheValue | null,
_fetchCache?: boolean _fetchCache?: boolean,
_originUrl?: string,
_fetchIdx?: number
): Promise<void> {} ): Promise<void> {}
} }
...@@ -266,7 +270,9 @@ export class IncrementalCache { ...@@ -266,7 +270,9 @@ export class IncrementalCache {
async get( async get(
pathname: string, pathname: string,
fetchCache?: boolean, fetchCache?: boolean,
revalidate?: number revalidate?: number,
originUrl?: string,
fetchIdx?: number
): Promise<IncrementalCacheEntry | null> { ): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode // we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging // so that getStaticProps is always called for easier debugging
...@@ -279,7 +285,12 @@ export class IncrementalCache { ...@@ -279,7 +285,12 @@ export class IncrementalCache {
pathname = this._getPathname(pathname, fetchCache) pathname = this._getPathname(pathname, fetchCache)
let entry: IncrementalCacheEntry | null = null let entry: IncrementalCacheEntry | null = null
const cacheData = await this.cacheHandler?.get(pathname, fetchCache) const cacheData = await this.cacheHandler?.get(
pathname,
fetchCache,
originUrl,
fetchIdx
)
if (cacheData?.value?.kind === 'FETCH') { if (cacheData?.value?.kind === 'FETCH') {
revalidate = revalidate || cacheData.value.revalidate revalidate = revalidate || cacheData.value.revalidate
...@@ -337,7 +348,14 @@ export class IncrementalCache { ...@@ -337,7 +348,14 @@ export class IncrementalCache {
curRevalidate, curRevalidate,
revalidateAfter, revalidateAfter,
} }
this.set(pathname, entry.value, curRevalidate, fetchCache) this.set(
pathname,
entry.value,
curRevalidate,
fetchCache,
originUrl,
fetchIdx
)
} }
return entry return entry
} }
...@@ -347,7 +365,9 @@ export class IncrementalCache { ...@@ -347,7 +365,9 @@ export class IncrementalCache {
pathname: string, pathname: string,
data: IncrementalCacheValue | null, data: IncrementalCacheValue | null,
revalidateSeconds?: number | false, revalidateSeconds?: number | false,
fetchCache?: boolean fetchCache?: boolean,
originUrl?: string,
fetchIdx?: number
) { ) {
if (this.dev && !fetchCache) return if (this.dev && !fetchCache) return
// fetchCache has upper limit of 2MB per-entry currently // fetchCache has upper limit of 2MB per-entry currently
...@@ -374,7 +394,13 @@ export class IncrementalCache { ...@@ -374,7 +394,13 @@ export class IncrementalCache {
initialRevalidateSeconds: revalidateSeconds, initialRevalidateSeconds: revalidateSeconds,
} }
} }
await this.cacheHandler?.set(pathname, data, fetchCache) await this.cacheHandler?.set(
pathname,
data,
fetchCache,
originUrl,
fetchIdx
)
} catch (error) { } catch (error) {
console.warn('Failed to update prerender cache for', pathname, error) console.warn('Failed to update prerender cache for', pathname, error)
} }
......
...@@ -25,7 +25,7 @@ export function patchFetch({ ...@@ -25,7 +25,7 @@ export function patchFetch({
input: RequestInfo | URL, input: RequestInfo | URL,
init: RequestInit | undefined init: RequestInit | undefined
) => { ) => {
let url let url: URL | undefined
try { try {
url = new URL(input instanceof Request ? input.url : input) url = new URL(input instanceof Request ? input.url : input)
url.username = '' url.username = ''
...@@ -163,6 +163,7 @@ export function patchFetch({ ...@@ -163,6 +163,7 @@ export function patchFetch({
!autoNoCache && !autoNoCache &&
(typeof staticGenerationStore.revalidate === 'undefined' || (typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof revalidate === 'number' && (typeof revalidate === 'number' &&
typeof staticGenerationStore.revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate)) revalidate < staticGenerationStore.revalidate))
) { ) {
staticGenerationStore.revalidate = revalidate staticGenerationStore.revalidate = revalidate
...@@ -222,8 +223,18 @@ export function patchFetch({ ...@@ -222,8 +223,18 @@ export function patchFetch({
} }
} }
const originUrl = url?.toString() ?? ''
const fetchIdx = staticGenerationStore.nextFetchId ?? 1
staticGenerationStore.nextFetchId = fetchIdx + 1
const doOriginalFetch = async () => { const doOriginalFetch = async () => {
return originFetch(input, init).then(async (res) => { // add metadata to init without editing the original
const clonedInit = {
...init,
next: { ...init?.next, fetchType: 'origin', fetchIdx, originUrl },
}
return originFetch(input, clonedInit).then(async (res) => {
if ( if (
res.ok && res.ok &&
staticGenerationStore.incrementalCache && staticGenerationStore.incrementalCache &&
...@@ -256,7 +267,9 @@ export function patchFetch({ ...@@ -256,7 +267,9 @@ export function patchFetch({
revalidate, revalidate,
}, },
revalidate, revalidate,
true true,
originUrl,
fetchIdx
) )
} catch (err) { } catch (err) {
console.warn(`Failed to set fetch cache`, input, err) console.warn(`Failed to set fetch cache`, input, err)
...@@ -277,7 +290,9 @@ export function patchFetch({ ...@@ -277,7 +290,9 @@ export function patchFetch({
: await staticGenerationStore.incrementalCache.get( : await staticGenerationStore.incrementalCache.get(
cacheKey, cacheKey,
true, true,
revalidate revalidate,
originUrl,
fetchIdx
) )
if (entry?.value && entry.value.kind === 'FETCH') { if (entry?.value && entry.value.kind === 'FETCH') {
......
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