Skip to content
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

avoid accessing performance APIs if fetch is untracked #71164

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,19 @@ export function createPatchedFetcher(
url = undefined
}
const fetchUrl = url?.href ?? ''
const fetchStart = performance.timeOrigin + performance.now()
const method = init?.method?.toUpperCase() || 'GET'

// Do create a new span trace for internal fetches in the
// non-verbose mode.
const isInternal = (init?.next as any)?.internal === true
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 workUnitStore = workUnitAsyncStorage.getStore()
Expand Down Expand Up @@ -540,7 +546,7 @@ export function createPatchedFetcher(
}

return originFetch(input, clonedInit).then(async (res) => {
if (!isStale) {
if (!isStale && fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
Expand Down Expand Up @@ -736,15 +742,17 @@ export function createPatchedFetcher(
}

if (cachedFetchData) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit',
cacheWarning,
status: cachedFetchData.status || 200,
method: init?.method || 'GET',
})
if (fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit',
cacheWarning,
status: cachedFetchData.status || 200,
method: init?.method || 'GET',
})
}

const response = new Response(
Buffer.from(cachedFetchData.body, 'base64'),
Expand Down
Loading