Skip to content

Commit

Permalink
Tracing (#8695)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcins committed May 12, 2023
1 parent d3e0067 commit 482d23a
Show file tree
Hide file tree
Showing 39 changed files with 760 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/core/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@parcel/logger": "2.8.3",
"@parcel/package-manager": "2.8.3",
"@parcel/plugin": "2.8.3",
"@parcel/profiler": "2.8.3",
"@parcel/source-map": "^2.1.1",
"@parcel/types": "2.8.3",
"@parcel/utils": "2.8.3",
Expand Down
19 changes: 19 additions & 0 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {createBuildCache} from './buildCache';
import {getInvalidationId, getInvalidationHash} from './assetUtils';
import {optionsProxy} from './utils';
import {invalidateDevDeps} from './requests/DevDepRequest';
import {tracer, PluginTracer} from '@parcel/profiler';

type Opts = {|
config: ParcelConfig,
Expand Down Expand Up @@ -275,6 +276,7 @@ export default class PackagerRunner {
config: new PublicConfig(config, this.options),
options: new PluginOptions(this.options),
logger: new PluginLogger({origin: plugin.name}),
tracer: new PluginTracer({origin: plugin.name, category: 'loadConfig'}),
});
bundleConfigs.set(plugin.name, config);
}
Expand Down Expand Up @@ -394,7 +396,11 @@ export default class PackagerRunner {

let packager = await this.config.getPackager(bundle.name);
let {name, resolveFrom, plugin} = packager;
let measurement;
try {
measurement = tracer.createMeasurement(name, 'packaging', bundle.name, {
type: bundle.type,
});
return await plugin.package({
config: configs.get(name)?.result,
bundleConfig: bundleConfigs.get(name)?.result,
Expand All @@ -409,6 +415,7 @@ export default class PackagerRunner {
},
options: this.pluginOptions,
logger: new PluginLogger({origin: name}),
tracer: new PluginTracer({origin: name, category: 'package'}),
getInlineBundleContents: async (
bundle: BundleType,
bundleGraph: BundleGraphType<NamedBundleType>,
Expand Down Expand Up @@ -438,6 +445,7 @@ export default class PackagerRunner {
}),
});
} finally {
measurement && measurement.end();
// Add dev dependency for the packager. This must be done AFTER running it due to
// the potential for lazy require() that aren't executed until the request runs.
let devDepRequest = await createDevDependency(
Expand Down Expand Up @@ -495,7 +503,13 @@ export default class PackagerRunner {
};

for (let optimizer of optimizers) {
let measurement;
try {
measurement = tracer.createMeasurement(
optimizer.name,
'optimize',
bundle.name,
);
let next = await optimizer.plugin.optimize({
config: configs.get(optimizer.name)?.result,
bundleConfig: bundleConfigs.get(optimizer.name)?.result,
Expand All @@ -508,6 +522,10 @@ export default class PackagerRunner {
},
options: this.pluginOptions,
logger: new PluginLogger({origin: optimizer.name}),
tracer: new PluginTracer({
origin: optimizer.name,
category: 'optimize',
}),
});

optimized.type = next.type ?? optimized.type;
Expand All @@ -521,6 +539,7 @@ export default class PackagerRunner {
}),
});
} finally {
measurement && measurement.end();
// Add dev dependency for the optimizer. This must be done AFTER running it due to
// the potential for lazy require() that aren't executed until the request runs.
let devDepRequest = await createDevDependency(
Expand Down
5 changes: 5 additions & 0 deletions packages/core/core/src/Parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {Disposable} from '@parcel/events';
import {init as initSourcemaps} from '@parcel/source-map';
import {init as initHash} from '@parcel/hash';
import {toProjectPath} from './projectPath';
import {tracer} from '@parcel/profiler';

registerCoreWithSerializer();

Expand Down Expand Up @@ -104,6 +105,7 @@ export default class Parcel {
} else {
this.#farm = createWorkerFarm({
shouldPatchConsole: resolvedOptions.shouldPatchConsole,
shouldTrace: resolvedOptions.shouldTrace,
});
}

Expand Down Expand Up @@ -257,6 +259,9 @@ export default class Parcel {
if (options.shouldProfile) {
await this.startProfiling();
}
if (options.shouldTrace) {
tracer.enable();
}
this.#reporterRunner.report({
type: 'buildStart',
});
Expand Down
14 changes: 14 additions & 0 deletions packages/core/core/src/ReporterRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import logger, {
} from '@parcel/logger';
import PluginOptions from './public/PluginOptions';
import BundleGraph from './BundleGraph';
import {tracer, PluginTracer} from '@parcel/profiler';

type Opts = {|
config: ParcelConfig,
Expand All @@ -44,6 +45,7 @@ export default class ReporterRunner {
this.pluginOptions = new PluginOptions(this.options);

logger.onLog(event => this.report(event));
tracer.onTrace(event => this.report(event));

bus.on('reporterEvent', this.eventHandler);
instances.add(this);
Expand Down Expand Up @@ -93,14 +95,26 @@ export default class ReporterRunner {
}
for (let reporter of this.reporters) {
let measurement;
try {
// To avoid an infinite loop we don't measure trace events, as they'll
// result in another trace!
if (event.type !== 'trace') {
measurement = tracer.createMeasurement(reporter.name, 'reporter');
}
await reporter.plugin.report({
event,
options: this.pluginOptions,
logger: new PluginLogger({origin: reporter.name}),
tracer: new PluginTracer({
origin: reporter.name,
category: 'reporter',
}),
});
} catch (reportError) {
INTERNAL_ORIGINAL_CONSOLE.error(reportError);
} finally {
measurement && measurement.end();
}
}
} catch (err) {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
} from './projectPath';
import {invalidateOnFileCreateToInternal} from './utils';
import invariant from 'assert';
import {tracer, PluginTracer} from '@parcel/profiler';

type GenerateFunc = (input: UncommittedAsset) => Promise<GenerateOutput>;

Expand Down Expand Up @@ -463,6 +464,12 @@ export default class Transformation {
}

try {
const measurement = tracer.createMeasurement(
transformer.name,
'transform',
fromProjectPathRelative(initialAsset.value.filePath),
);

let transformerResults = await this.runTransformer(
pipeline,
asset,
Expand All @@ -473,6 +480,8 @@ export default class Transformation {
this.parcelConfig,
);

measurement && measurement.end();

for (let result of transformerResults) {
if (result instanceof UncommittedAsset) {
resultingAssets.push(result);
Expand Down Expand Up @@ -739,6 +748,10 @@ export default class Transformation {
parcelConfig: ParcelConfig,
): Promise<$ReadOnlyArray<TransformerResult | UncommittedAsset>> {
const logger = new PluginLogger({origin: transformerName});
const tracer = new PluginTracer({
origin: transformerName,
category: 'transform',
});

const resolve = async (
from: FilePath,
Expand Down Expand Up @@ -794,6 +807,7 @@ export default class Transformation {
ast: asset.ast,
options: pipeline.pluginOptions,
logger,
tracer,
})) &&
asset.generate
) {
Expand All @@ -814,6 +828,7 @@ export default class Transformation {
options: pipeline.pluginOptions,
resolve,
logger,
tracer,
});
if (ast) {
asset.setAST(ast);
Expand All @@ -830,6 +845,7 @@ export default class Transformation {
options: pipeline.pluginOptions,
resolve,
logger,
tracer,
});
let results = await normalizeAssets(this.options, transfomerResult);

Expand All @@ -842,6 +858,7 @@ export default class Transformation {
ast: asset.ast,
options: pipeline.pluginOptions,
logger,
tracer,
});
asset.clearAST();
return Promise.resolve(generated);
Expand All @@ -863,6 +880,7 @@ export default class Transformation {
options: pipeline.pluginOptions,
resolve,
logger,
tracer,
});

return Promise.all(
Expand Down
8 changes: 8 additions & 0 deletions packages/core/core/src/Validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Asset} from './public/Asset';
import PluginOptions from './public/PluginOptions';
import summarizeRequest from './summarizeRequest';
import {fromProjectPath, fromProjectPathRelative} from './projectPath';
import {PluginTracer} from '@parcel/profiler';

export type ValidationOpts = {|
config: ParcelConfig,
Expand Down Expand Up @@ -66,6 +67,10 @@ export default class Validation {
if (assets) {
let plugin = this.allValidators[validatorName];
let validatorLogger = new PluginLogger({origin: validatorName});
let validatorTracer = new PluginTracer({
origin: validatorName,
category: 'validator',
});
let validatorResults: Array<?ValidateResult> = [];
try {
// If the plugin supports the single-threading validateAll method, pass all assets to it.
Expand All @@ -74,6 +79,7 @@ export default class Validation {
assets: assets.map(asset => new Asset(asset)),
options: pluginOptions,
logger: validatorLogger,
tracer: validatorTracer,
resolveConfigWithPath: (
configNames: Array<string>,
assetFilePath: string,
Expand All @@ -98,6 +104,7 @@ export default class Validation {
asset: publicAsset,
options: pluginOptions,
logger: validatorLogger,
tracer: validatorTracer,
resolveConfig: (configNames: Array<string>) =>
resolveConfig(
this.options.inputFS,
Expand All @@ -113,6 +120,7 @@ export default class Validation {
options: pluginOptions,
config,
logger: validatorLogger,
tracer: validatorTracer,
});
validatorResults.push(validatorResult);
}),
Expand Down
16 changes: 15 additions & 1 deletion packages/core/core/src/applyRuntimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {mergeEnvironments} from './Environment';
import createAssetGraphRequest from './requests/AssetGraphRequest';
import {createDevDependency, runDevDepRequest} from './requests/DevDepRequest';
import {toProjectPath, fromProjectPathRelative} from './projectPath';
import {tracer, PluginTracer} from '@parcel/profiler';

type RuntimeConnection = {|
bundle: InternalBundle,
Expand Down Expand Up @@ -78,9 +79,16 @@ export default async function applyRuntimes<TResult>({

for (let bundle of bundles) {
for (let runtime of runtimes) {
let measurement;
try {
const namedBundle = NamedBundle.get(bundle, bundleGraph, options);
measurement = tracer.createMeasurement(
runtime.name,
'applyRuntime',
namedBundle.displayName,
);
let applied = await runtime.plugin.apply({
bundle: NamedBundle.get(bundle, bundleGraph, options),
bundle: namedBundle,
bundleGraph: new BundleGraph<INamedBundle>(
bundleGraph,
NamedBundle.get.bind(NamedBundle),
Expand All @@ -89,6 +97,10 @@ export default async function applyRuntimes<TResult>({
config: configs.get(runtime.name)?.result,
options: pluginOptions,
logger: new PluginLogger({origin: runtime.name}),
tracer: new PluginTracer({
origin: runtime.name,
category: 'applyRuntime',
}),
});

if (applied) {
Expand Down Expand Up @@ -157,6 +169,8 @@ export default async function applyRuntimes<TResult>({
origin: runtime.name,
}),
});
} finally {
measurement && measurement.end();
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core/src/assetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from './projectPath';
import {hashString} from '@parcel/hash';
import {BundleBehavior as BundleBehaviorMap} from './types';
import {PluginTracer} from '@parcel/profiler';

type AssetOptions = {|
id?: string,
Expand Down Expand Up @@ -172,6 +173,7 @@ async function _generateFromAST(asset: CommittedAsset | UncommittedAsset) {
ast,
options: new PluginOptions(asset.options),
logger: new PluginLogger({origin: pluginName}),
tracer: new PluginTracer({origin: pluginName, category: 'asset-generate'}),
});

let mapBuffer = map?.toBuffer();
Expand Down
Loading

0 comments on commit 482d23a

Please sign in to comment.