Skip to content

Commit

Permalink
Gracefully handle telemetry not being available (#44986)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Jan 18, 2023
1 parent 6d09f0b commit 8f0acdf
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/next/src/build/webpack/plugins/middleware-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ function getExtractMetadata(params: {
return async () => {
metadataByEntry.clear()
const resolver = compilation.resolverFactory.get('normal')
const telemetry: Telemetry = traceGlobals.get('telemetry')
const telemetry: Telemetry | undefined = traceGlobals.get('telemetry')

for (const [entryName, entry] of compilation.entries) {
if (entry.options.runtime !== EDGE_RUNTIME_WEBPACK) {
Expand Down Expand Up @@ -683,7 +683,7 @@ function getExtractMetadata(params: {
}

if (edgeFunctionConfig?.config?.unstable_allowDynamicGlobs) {
telemetry.record({
telemetry?.record({
eventName: 'NEXT_EDGE_ALLOW_DYNAMIC_USED',
payload: {
...edgeFunctionConfig,
Expand Down Expand Up @@ -773,7 +773,7 @@ function getExtractMetadata(params: {
}
}

telemetry.record({
telemetry?.record({
eventName: EVENT_BUILD_FEATURE_USAGE,
payload: {
featureName: 'vercelImageGeneration',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/telemetry/events/swc-load-failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type EventSwcLoadFailure = {
export async function eventSwcLoadFailure(
event?: EventSwcLoadFailure['payload']
): Promise<void> {
const telemetry: Telemetry = traceGlobals.get('telemetry')
const telemetry: Telemetry | undefined = traceGlobals.get('telemetry')
// can't continue if telemetry isn't set
if (!telemetry) return

Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/trace/report/to-telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Telemetry } from '../../telemetry/storage'
import { traceGlobals } from '../shared'

const TRACE_EVENT_ACCESSLIST = new Map(
Expand All @@ -11,7 +12,7 @@ const reportToTelemetry = (spanName: string, duration: number) => {
if (!eventName) {
return
}
const telemetry = traceGlobals.get('telemetry')
const telemetry: Telemetry | undefined = traceGlobals.get('telemetry')
if (!telemetry) {
return
}
Expand Down
9 changes: 8 additions & 1 deletion packages/next/src/trace/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export type SpanId = number

export const traceGlobals: Map<any, any> = new Map()
let _traceGlobals: Map<any, any> = (global as any)._traceGlobals

if (!_traceGlobals) {
_traceGlobals = new Map()
}
;(global as any)._traceGlobals = _traceGlobals

export const traceGlobals: Map<any, any> = _traceGlobals
export const setGlobal = (key: any, val: any) => {
traceGlobals.set(key, val)
}
17 changes: 13 additions & 4 deletions test/e2e/yarn-pnp/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { NextInstance } from 'test/lib/next-modes/base'

jest.setTimeout(2 * 60 * 1000)

export function runTests(example = '') {
export function runTests(
example = '',
testPath = '/',
expectedContent = ['index page']
) {
const versionParts = process.versions.node.split('.').map((i) => Number(i))

if ((global as any).isNextDeploy) {
Expand Down Expand Up @@ -42,7 +46,7 @@ export function runTests(example = '') {
prev.push(`${cur}@${dependencies[cur]}`)
return prev
}, [] as string[])
return `yarn set version 4.0.0-rc.13 && yarn config set enableGlobalCache true && yarn config set compressionLevel 0 && yarn add ${pkgs.join(
return `yarn set version berry && yarn config set enableGlobalCache true && yarn config set compressionLevel 0 && yarn add ${pkgs.join(
' '
)}`
},
Expand All @@ -55,9 +59,14 @@ export function runTests(example = '') {
afterAll(() => next?.destroy())

it(`should compile and serve the index page correctly ${example}`, async () => {
const res = await fetchViaHTTP(next.url, '/')
const res = await fetchViaHTTP(next.url, testPath)
expect(res.status).toBe(200)
expect(await res.text()).toContain('<html')

const text = await res.text()

for (const content of expectedContent) {
expect(text).toContain(content)
}
})
} else {
it('should not run PnP test for older node versions', () => {})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/yarn-pnp/test/with-eslint.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runTests } from './utils'

describe('yarn PnP', () => {
runTests('with-eslint')
runTests('with-eslint', '/', ['<html', 'Home', 'fake-script'])
})
2 changes: 1 addition & 1 deletion test/e2e/yarn-pnp/test/with-mdx.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runTests } from './utils'

describe('yarn PnP', () => {
runTests('with-mdx')
runTests('with-mdx', '/', ['Look, a button', 'Hello'])
})
4 changes: 3 additions & 1 deletion test/e2e/yarn-pnp/test/with-next-sass.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { runTests } from './utils'

describe('yarn PnP', () => {
runTests('with-next-sass')
runTests('with-next-sass', '/', [
'Hello World, I am being styled using SCSS Modules',
])
})

0 comments on commit 8f0acdf

Please sign in to comment.