Unverified Commit 46025af7 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

avoid accessing performance APIs if fetch is untracked (#71164)

If a request is marked as internal, we don't record fetch metrics, so we
don't need to track a `fetchStart`. We're investigating cases where
`performance` APIs might be unavailable in particular runtimes and can
safely exclude internal requests from being subject to the possibility
of it not being available.
parent ceebc676
...@@ -168,13 +168,19 @@ export function createPatchedFetcher( ...@@ -168,13 +168,19 @@ export function createPatchedFetcher(
url = undefined url = undefined
} }
const fetchUrl = url?.href ?? '' const fetchUrl = url?.href ?? ''
const fetchStart = performance.timeOrigin + performance.now()
const method = init?.method?.toUpperCase() || 'GET' const method = init?.method?.toUpperCase() || 'GET'
// Do create a new span trace for internal fetches in the // Do create a new span trace for internal fetches in the
// non-verbose mode. // non-verbose mode.
const isInternal = (init?.next as any)?.internal === true const isInternal = (init?.next as any)?.internal === true
const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1' const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1'
// We don't track fetch metrics for internal fetches
// so it's not critical that we have a start time, as it won't be recorded.
// This is to workaround a flaky issue where performance APIs might
// not be available and will require follow-up investigation.
const fetchStart: number | undefined = isInternal
? undefined
: performance.timeOrigin + performance.now()
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore() const workUnitStore = workUnitAsyncStorage.getStore()
...@@ -540,7 +546,7 @@ export function createPatchedFetcher( ...@@ -540,7 +546,7 @@ export function createPatchedFetcher(
} }
return originFetch(input, clonedInit).then(async (res) => { return originFetch(input, clonedInit).then(async (res) => {
if (!isStale) { if (!isStale && fetchStart) {
trackFetchMetric(workStore, { trackFetchMetric(workStore, {
start: fetchStart, start: fetchStart,
url: fetchUrl, url: fetchUrl,
...@@ -736,15 +742,17 @@ export function createPatchedFetcher( ...@@ -736,15 +742,17 @@ export function createPatchedFetcher(
} }
if (cachedFetchData) { if (cachedFetchData) {
trackFetchMetric(workStore, { if (fetchStart) {
start: fetchStart, trackFetchMetric(workStore, {
url: fetchUrl, start: fetchStart,
cacheReason, url: fetchUrl,
cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit', cacheReason,
cacheWarning, cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit',
status: cachedFetchData.status || 200, cacheWarning,
method: init?.method || 'GET', status: cachedFetchData.status || 200,
}) method: init?.method || 'GET',
})
}
const response = new Response( const response = new Response(
Buffer.from(cachedFetchData.body, 'base64'), Buffer.from(cachedFetchData.body, 'base64'),
......
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