Unverified Commit 53628fba authored by Keen Yee Liau's avatar Keen Yee Liau Committed by GitHub

Create TelemetryPlugin for collecting feature usage metrics (#29145)

This commit creates a new Webpack `TelemetryPlugin` to gather information
about usage of certain features, for example `next/image` and `next/script`.
parent ebc1b0e5
...@@ -63,6 +63,7 @@ import { ...@@ -63,6 +63,7 @@ import {
eventBuildCompleted, eventBuildCompleted,
eventBuildOptimize, eventBuildOptimize,
eventCliSession, eventCliSession,
eventBuildFeatureUsage,
eventNextPlugins, eventNextPlugins,
eventTypeCheckCompleted, eventTypeCheckCompleted,
} from '../telemetry/events' } from '../telemetry/events'
...@@ -91,6 +92,7 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' ...@@ -91,6 +92,7 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack' import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { NextConfigComplete } from '../server/config-shared' import { NextConfigComplete } from '../server/config-shared'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
export type SsgRoute = { export type SsgRoute = {
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
...@@ -1619,6 +1621,12 @@ export default async function build( ...@@ -1619,6 +1621,12 @@ export default async function build(
}) })
) )
const telemetryPlugin = clientConfig.plugins?.find(isTelemetryPlugin)
if (telemetryPlugin) {
const events = eventBuildFeatureUsage(telemetryPlugin)
telemetry.record(events)
}
if (ssgPages.size > 0) { if (ssgPages.size > 0) {
const finalDynamicRoutes: PrerenderManifest['dynamicRoutes'] = {} const finalDynamicRoutes: PrerenderManifest['dynamicRoutes'] = {}
tbdPrerenderRoutes.forEach((tbdRoute) => { tbdPrerenderRoutes.forEach((tbdRoute) => {
...@@ -1777,3 +1785,7 @@ function generateClientSsgManifest( ...@@ -1777,3 +1785,7 @@ function generateClientSsgManifest(
clientSsgManifestContent clientSsgManifestContent
) )
} }
function isTelemetryPlugin(plugin: unknown): plugin is TelemetryPlugin {
return plugin instanceof TelemetryPlugin
}
...@@ -49,6 +49,7 @@ import { ServerlessPlugin } from './webpack/plugins/serverless-plugin' ...@@ -49,6 +49,7 @@ import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin' import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'
import { regexLikeCss } from './webpack/config/blocks/css' import { regexLikeCss } from './webpack/config/blocks/css'
import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin' import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
import type { Span } from '../trace' import type { Span } from '../trace'
import isError from '../lib/is-error' import isError from '../lib/is-error'
...@@ -1386,6 +1387,7 @@ export default async function getBaseWebpackConfig( ...@@ -1386,6 +1387,7 @@ export default async function getBaseWebpackConfig(
minimized: true, minimized: true,
}, },
}), }),
!dev && !isServer && isWebpack5 && new TelemetryPlugin(),
].filter(Boolean as any as ExcludesFalse), ].filter(Boolean as any as ExcludesFalse),
} }
......
import type { Compiler, Compilation } from 'webpack'
type Feature = 'next/image' | 'next/script' | 'next/dynamic'
interface FeatureUsage {
featureName: Feature
invocationCount: number
}
/**
* A vertex in the module graph.
*/
interface Module {
type: string
identifier(): string
}
/**
* An edge in the module graph.
*/
interface Connection {
originModule: unknown
}
// Map of a feature module to the file it belongs in the next package.
const FEATURE_MODULE_MAP: ReadonlyMap<Feature, string> = new Map([
['next/image', '/next/image.js'],
['next/script', '/next/script.js'],
['next/dynamic', '/next/dynamic.js'],
])
/**
* Plugin that queries the ModuleGraph to look for modules that correspond to
* certain features (e.g. next/image and next/script) and record how many times
* they are imported.
*/
export class TelemetryPlugin {
private usageTracker = new Map<Feature, FeatureUsage>()
constructor() {
for (const featureName of FEATURE_MODULE_MAP.keys()) {
this.usageTracker.set(featureName, {
featureName,
invocationCount: 0,
})
}
}
apply(compiler: Compiler): void {
compiler.hooks.make.tapAsync(
TelemetryPlugin.name,
async (compilation: Compilation, callback: () => void) => {
compilation.hooks.finishModules.tapAsync(
TelemetryPlugin.name,
async (modules: Set<Module>, modulesFinish: () => void) => {
for (const module of modules) {
const feature = findFeatureInModule(module)
if (!feature) {
continue
}
const connections = (
compilation as any
).moduleGraph.getIncomingConnections(module)
const originModules =
findUniqueOriginModulesInConnections(connections)
this.usageTracker.get(feature)!.invocationCount =
originModules.size
}
modulesFinish()
}
)
callback()
}
)
}
usages(): FeatureUsage[] {
return [...this.usageTracker.values()]
}
}
/**
* Determine if there is a feature of interest in the specified 'module'.
*/
function findFeatureInModule(module: Module): Feature | undefined {
if (module.type !== 'javascript/auto') {
return
}
for (const [feature, path] of FEATURE_MODULE_MAP) {
if (module.identifier().replace(/\\/g, '/').endsWith(path)) {
return feature
}
}
}
/**
* Find unique origin modules in the specified 'connections', which possibly
* contains more than one connection for a module due to different types of
* dependency.
*/
function findUniqueOriginModulesInConnections(
connections: Connection[]
): Set<unknown> {
const originModules = new Set()
for (const connection of connections) {
if (!originModules.has(connection.originModule)) {
originModules.add(connection.originModule)
}
}
return originModules
}
import { TelemetryPlugin } from '../../build/webpack/plugins/telemetry-plugin'
const REGEXP_DIRECTORY_DUNDER = const REGEXP_DIRECTORY_DUNDER =
/[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i /[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i
const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i
...@@ -117,3 +119,20 @@ export function eventBuildOptimize( ...@@ -117,3 +119,20 @@ export function eventBuildOptimize(
}, },
} }
} }
const EVENT_BUILD_FEATURE_USAGE = 'NEXT_BUILD_FEATURE_USAGE'
type EventBuildFeatureUsage = {
featureName: string
invocationCount: number
}
export function eventBuildFeatureUsage(
telemetryPlugin: TelemetryPlugin
): Array<{ eventName: string; payload: EventBuildFeatureUsage }> {
return telemetryPlugin.usages().map(({ featureName, invocationCount }) => ({
eventName: EVENT_BUILD_FEATURE_USAGE,
payload: {
featureName,
invocationCount,
},
}))
}
import Image from 'next/image'
import profilePic from '../public/small.jpg'
function About() {
return (
<>
<h1>My Homepage</h1>
<Image src={profilePic} alt="Picture of the author" />
<p>Welcome to my homepage!</p>
</>
)
}
export default About
import Script from 'next/script'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('./about'))
export default function Home() {
return (
<>
<Script src="https://www.google-analytics.com/analytics.js" />
<DynamicComponent />
</>
)
}
...@@ -331,8 +331,8 @@ describe('Telemetry CLI', () => { ...@@ -331,8 +331,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"staticPropsPageCount": 2/) expect(event1).toMatch(/"staticPropsPageCount": 2/)
expect(event1).toMatch(/"serverPropsPageCount": 1/) expect(event1).toMatch(/"serverPropsPageCount": 1/)
expect(event1).toMatch(/"ssrPageCount": 1/) expect(event1).toMatch(/"ssrPageCount": 1/)
expect(event1).toMatch(/"staticPageCount": 2/) expect(event1).toMatch(/"staticPageCount": 4/)
expect(event1).toMatch(/"totalPageCount": 6/) expect(event1).toMatch(/"totalPageCount": 8/)
}) })
it('detects isSrcDir dir correctly for `next dev`', async () => { it('detects isSrcDir dir correctly for `next dev`', async () => {
...@@ -560,4 +560,21 @@ describe('Telemetry CLI', () => { ...@@ -560,4 +560,21 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/)
expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/)
}) })
it('emits telemery for usage of image, script & dynamic', async () => {
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
const regex = /NEXT_BUILD_FEATURE_USAGE[\s\S]+?{([\s\S]+?)}/g
const image = regex.exec(stderr).pop()
expect(image).toContain(`"featureName": "next/image"`)
expect(image).toContain(`"invocationCount": 1`)
const script = regex.exec(stderr).pop()
expect(script).toContain(`"featureName": "next/script"`)
expect(script).toContain(`"invocationCount": 1`)
const dynamic = regex.exec(stderr).pop()
expect(dynamic).toContain(`"featureName": "next/dynamic"`)
expect(dynamic).toContain(`"invocationCount": 1`)
})
}) })
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