Unverified Commit 6e77c071 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Update Next.js Telemetry Event Shapes (#8628)

* Update Next.js Telemetry Event Shapes

* Force value as-is
parent 61b8b732
...@@ -64,7 +64,10 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -64,7 +64,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
await verifyTypeScriptSetup(dir) await verifyTypeScriptSetup(dir)
let backgroundWork: (Promise<any> | undefined)[] = [] let backgroundWork: (Promise<any> | undefined)[] = []
backgroundWork.push(recordVersion(), recordNextPlugins(path.resolve(dir))) backgroundWork.push(
recordVersion({ cliCommand: 'build' }),
recordNextPlugins(path.resolve(dir))
)
console.log('Creating an optimized production build ...') console.log('Creating an optimized production build ...')
console.log() console.log()
...@@ -199,7 +202,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -199,7 +202,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log(chalk.green('Compiled successfully.\n')) console.log(chalk.green('Compiled successfully.\n'))
backgroundWork.push( backgroundWork.push(
recordBuildDuration({ recordBuildDuration({
numberOfPages: allPagePaths.length, totalPageCount: allPagePaths.length,
durationInSeconds: webpackBuildEnd[0], durationInSeconds: webpackBuildEnd[0],
}) })
) )
...@@ -410,7 +413,8 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -410,7 +413,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
recordBuildOptimize({ recordBuildOptimize({
durationInSeconds: analysisEnd[0], durationInSeconds: analysisEnd[0],
totalPageCount: allPagePaths.length, totalPageCount: allPagePaths.length,
staticOptimizedPages: staticPages.size, staticPageCount: staticPages.size,
ssrPageCount: allPagePaths.length - staticPages.size,
}) })
) )
......
...@@ -179,7 +179,7 @@ export default class DevServer extends Server { ...@@ -179,7 +179,7 @@ export default class DevServer extends Server {
await this.startWatcher() await this.startWatcher()
this.setDevReady() this.setDevReady()
recordVersion({ isDev: true }) recordVersion({ cliCommand: 'dev' })
} }
async close () { async close () {
......
import { record } from '../storage' import { record } from '../storage'
const EVENT_BUILD_DURATION = 'NEXT_BUILD_DURATION' const EVENT_BUILD_DURATION = 'NEXT_BUILD_COMPLETED'
type BuildDurationEventShape = { type EventBuildCompleted = {
durationInSeconds: number durationInSeconds: number
numberOfPages: number totalPageCount: number
} }
export function recordBuildDuration(event: BuildDurationEventShape) { export function recordBuildDuration(event: EventBuildCompleted) {
return record({ return record({
eventName: EVENT_BUILD_DURATION, eventName: EVENT_BUILD_DURATION,
payload: { payload: event,
seconds: event.durationInSeconds,
pageCount: event.numberOfPages,
},
}) })
} }
const EVENT_BUILD_OPTIMIZE = 'NEXT_BUILD_OPTIMIZE' const EVENT_BUILD_OPTIMIZE = 'NEXT_BUILD_OPTIMIZED'
type BuildAnalysisEventShape = { type EventBuildOptimized = {
durationInSeconds: number durationInSeconds: number
totalPageCount: number totalPageCount: number
staticOptimizedPages: number staticPageCount: number
ssrPageCount: number
} }
export function recordBuildOptimize(event: BuildAnalysisEventShape) { export function recordBuildOptimize(event: EventBuildOptimized) {
return record({ return record({
eventName: EVENT_BUILD_OPTIMIZE, eventName: EVENT_BUILD_OPTIMIZE,
payload: { payload: event,
seconds: event.durationInSeconds,
pageCount: event.totalPageCount,
pageAutomaticPrerenderedCount: event.staticOptimizedPages,
},
}) })
} }
import { record } from '../storage' import { record } from '../storage'
import findUp from 'find-up' import findUp from 'find-up'
const EVENT_PLUGIN_PRESENT = 'NEXT_PLUGIN_PRESENT' const EVENT_PLUGIN_PRESENT = 'NEXT_PACKAGE_DETECTED'
type PluginPresentEventShape = { type EventPackageDetected = {
pluginName: string packageName: string
pluginVersion: string packageVersion: string
} }
const plugins = [ const plugins = [
...@@ -177,9 +177,9 @@ export async function recordNextPlugins(dir: string) { ...@@ -177,9 +177,9 @@ export async function recordNextPlugins(dir: string) {
}[]).map(({ pluginName, pluginVersion }) => ({ }[]).map(({ pluginName, pluginVersion }) => ({
eventName: EVENT_PLUGIN_PRESENT, eventName: EVENT_PLUGIN_PRESENT,
payload: { payload: {
pluginName, packageName: pluginName,
pluginVersion, packageVersion: pluginVersion,
} as PluginPresentEventShape, } as EventPackageDetected,
})) }))
) )
} catch (_) {} } catch (_) {}
......
import { record } from '../storage' import { record } from '../storage'
const EVENT_VERSION = 'NEXT_VERSION' const EVENT_VERSION = 'NEXT_CLI_SESSION_STARTED'
export function recordVersion({ isDev = false } = {}) { type EventCliSessionStarted = {
nextVersion: string
nodeVersion: string
cliCommand: string
}
export function recordVersion(
event: Omit<EventCliSessionStarted, 'nextVersion' | 'nodeVersion'>
) {
// This should be an invariant, if it fails our build tooling is broken. // This should be an invariant, if it fails our build tooling is broken.
if (typeof process.env.__NEXT_VERSION !== 'string') { if (typeof process.env.__NEXT_VERSION !== 'string') {
return Promise.resolve() return Promise.resolve()
...@@ -11,9 +19,9 @@ export function recordVersion({ isDev = false } = {}) { ...@@ -11,9 +19,9 @@ export function recordVersion({ isDev = false } = {}) {
return record({ return record({
eventName: EVENT_VERSION, eventName: EVENT_VERSION,
payload: { payload: {
version: process.env.__NEXT_VERSION, nextVersion: process.env.__NEXT_VERSION,
nodeVersion: process.version, nodeVersion: process.version,
isDevelopment: isDev, cliCommand: event.cliCommand,
}, } as EventCliSessionStarted,
}) })
} }
...@@ -148,7 +148,15 @@ export function isTelemetryEnabled(): boolean { ...@@ -148,7 +148,15 @@ export function isTelemetryEnabled(): boolean {
} }
type TelemetryEvent = { eventName: string; payload: object } type TelemetryEvent = { eventName: string; payload: object }
type EventContext = {
anonymousId: string
projectId: string
sessionId: string
}
type EventBatchShape = {
eventName: string
fields: object
}
function _record(_events: TelemetryEvent | TelemetryEvent[]): Promise<any> { function _record(_events: TelemetryEvent | TelemetryEvent[]): Promise<any> {
let events: TelemetryEvent[] let events: TelemetryEvent[]
if (Array.isArray(_events)) { if (Array.isArray(_events)) {
...@@ -185,14 +193,17 @@ function _record(_events: TelemetryEvent | TelemetryEvent[]): Promise<any> { ...@@ -185,14 +193,17 @@ function _record(_events: TelemetryEvent | TelemetryEvent[]): Promise<any> {
return Promise.resolve() return Promise.resolve()
} }
const context = { anonymousProjectId: projectId, runId: randomRunId } const context: EventContext = {
anonymousId: anonymousId,
projectId: projectId!,
sessionId: randomRunId!,
}
return _postPayload(`https://telemetry.nextjs.org/api/v1/record`, { return _postPayload(`https://telemetry.nextjs.org/api/v1/record`, {
anonymousId,
context, context,
events: events.map(({ eventName, payload }) => ({ events: events.map(({ eventName, payload }) => ({
eventName, eventName,
fields: payload, fields: payload,
})), })) as Array<EventBatchShape>,
}) })
} }
......
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