Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/build/src/core/report_metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { closeClient, formatTags, InputStatsDOptions, startClient, validateStatsDOptions } from '../report/statsd.js'

export type Metric = {
type: 'increment'
type: 'increment' | 'timing'
name: string
value: number
tags: Record<string, string | string[]>
Expand Down
15 changes: 14 additions & 1 deletion packages/build/src/plugins_core/edge_functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,23 @@ const getMetrics = (manifest): Metric[] => {

const numUserEfs = totalEfs.length - numGenEfs

return [
const metrics: Metric[] = [
{ type: 'increment', name: 'buildbot.build.functions', value: numGenEfs, tags: { type: 'edge:generated' } },
{ type: 'increment', name: 'buildbot.build.functions', value: numUserEfs, tags: { type: 'edge:user' } },
]

const tarballDurationMs = manifest.bundling_timing?.tarball_ms

if (typeof tarballDurationMs === 'number') {
metrics.push({
type: 'timing',
name: 'buildbot.build.edge_functions.bundling_ms',
value: tarballDurationMs,
tags: { format: 'tarball' },
})
}

return metrics
}
// We run this core step if at least one of the functions directories (the
// one configured by the user or the internal one) exists. We use a dynamic
Expand Down
3 changes: 3 additions & 0 deletions packages/edge-bundler/node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ describe.skipIf(lt(denoVersion, '2.4.3'))(
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)

expect(manifest.bundling_timing).toEqual({ tarball_ms: expect.any(Number) })

const tarballPath = join(distPath, manifest.bundles[0].asset)
const tarballResult = await runTarball(tarballPath)
expect(tarballResult).toStrictEqual(expectedOutput)
Expand Down Expand Up @@ -879,6 +881,7 @@ describe.skipIf(lt(denoVersion, '2.4.3'))(
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)

expect(manifest.bundling_timing).toEqual({ tarball_ms: expect.any(Number) })
expect(manifest.bundles.length).toBe(1)
expect(manifest.bundles[0].format).toBe('eszip2')

Expand Down
32 changes: 21 additions & 11 deletions packages/edge-bundler/node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,28 @@ export const bundle = async (
}

const bundles: Bundle[] = []
let tarballBundleDurationMs: number | undefined

if (featureFlags.edge_bundler_generate_tarball || featureFlags.edge_bundler_dry_run_generate_tarball) {
const tarballPromise = bundleTarball({
basePath,
buildID,
debug,
deno,
distDirectory,
functions,
featureFlags,
importMap: importMap.clone(),
vendorDirectory: vendor?.directory,
})
const tarballPromise = (async () => {
const start = Date.now()

try {
return await bundleTarball({
basePath,
buildID,
debug,
deno,
distDirectory,
functions,
featureFlags,
importMap: importMap.clone(),
vendorDirectory: vendor?.directory,
})
} finally {
tarballBundleDurationMs = Date.now() - start
}
})()

if (featureFlags.edge_bundler_dry_run_generate_tarball) {
try {
Expand Down Expand Up @@ -214,6 +223,7 @@ export const bundle = async (
internalFunctionConfig,
importMap: importMapSpecifier,
layers: deployConfig.layers,
bundlingTiming: tarballBundleDurationMs === undefined ? undefined : { tarball_ms: tarballBundleDurationMs },
})

await vendor?.cleanup()
Expand Down
10 changes: 10 additions & 0 deletions packages/edge-bundler/node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ export interface EdgeFunctionConfig {
traffic_rules?: TrafficRules
}

interface BundlingTiming {
tarball_ms?: number
}

interface Manifest {
bundler_version: string
bundles: { asset: string; format: string }[]
bundling_timing?: BundlingTiming
import_map?: string
layers: { name: string; flag: string }[]
routes: Route[]
Expand All @@ -68,6 +73,7 @@ interface GenerateManifestOptions {
internalFunctionConfig?: Record<string, FunctionConfig>
layers?: Layer[]
userFunctionConfig?: Record<string, FunctionConfig>
bundlingTiming?: BundlingTiming
}

const removeEmptyConfigValues = (functionConfig: EdgeFunctionConfig) =>
Expand Down Expand Up @@ -149,6 +155,7 @@ const generateManifest = ({
internalFunctionConfig = {},
importMap,
layers = [],
bundlingTiming,
}: GenerateManifestOptions) => {
const preCacheRoutes: Route[] = []
const postCacheRoutes: Route[] = []
Expand Down Expand Up @@ -259,6 +266,9 @@ const generateManifest = ({
layers,
import_map: importMap,
function_config: sanitizeEdgeFunctionConfig(manifestFunctionConfig),
...(bundlingTiming && Object.values(bundlingTiming).some((value) => value !== undefined)
? { bundling_timing: bundlingTiming }
: {}),
}
const unroutedFunctions = functions.filter(({ name }) => !routedFunctions.has(name)).map(({ name }) => name)

Expand Down
9 changes: 9 additions & 0 deletions packages/edge-bundler/node/validation/manifest/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const excludedPatternsSchema = {
},
}

const bundlingTimingSchema = {
type: 'object',
properties: {
tarball_ms: { type: 'number', minimum: 0 },
},
additionalProperties: false,
}

const headersSchema = {
type: 'object',
patternProperties: {
Expand Down Expand Up @@ -112,6 +120,7 @@ const edgeManifestSchema = {
},
import_map: { type: 'string' },
bundler_version: { type: 'string' },
bundling_timing: bundlingTimingSchema,
function_config: { type: 'object', additionalProperties: functionConfigSchema },
},
additionalProperties: false,
Expand Down
Loading