Skip to content

Commit

Permalink
Add measure and measureAsync tracing API
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak committed Apr 12, 2024
1 parent 4cd9467 commit 9f8c706
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 429 deletions.
140 changes: 73 additions & 67 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,47 +396,52 @@ 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,
bundle,
bundleGraph: new BundleGraph<NamedBundleType>(
bundleGraph,
NamedBundle.get.bind(NamedBundle),
this.options,
),
getSourceMapReference: map => {
return this.getSourceMapReference(bundle, map);
},
options: this.pluginOptions,
logger: new PluginLogger({origin: name}),
tracer: new PluginTracer({origin: name, category: 'package'}),
getInlineBundleContents: async (
bundle: BundleType,
bundleGraph: BundleGraphType<NamedBundleType>,
) => {
if (bundle.bundleBehavior !== 'inline') {
throw new Error(
'Bundle is not inline and unable to retrieve contents',
);
}

let res = await this.getBundleResult(
bundleToInternalBundle(bundle),
// $FlowFixMe
bundleGraphToInternalBundleGraph(bundleGraph),
configs,
bundleConfigs,
);

return {contents: res.contents};
return await tracer.measureAsync(
{
name,
args: {bundle: bundle.name},
categories: ['packaging'],
},
});
// eslint-disable-next-line require-await
async () =>
plugin.package({
config: configs.get(name)?.result,
bundleConfig: bundleConfigs.get(name)?.result,
bundle,
bundleGraph: new BundleGraph<NamedBundleType>(
bundleGraph,
NamedBundle.get.bind(NamedBundle),
this.options,
),
getSourceMapReference: map => {
return this.getSourceMapReference(bundle, map);
},
options: this.pluginOptions,
logger: new PluginLogger({origin: name}),
tracer: new PluginTracer({origin: name, category: 'package'}),
getInlineBundleContents: async (
bundle: BundleType,
bundleGraph: BundleGraphType<NamedBundleType>,
) => {
if (bundle.bundleBehavior !== 'inline') {
throw new Error(
'Bundle is not inline and unable to retrieve contents',
);
}

let res = await this.getBundleResult(
bundleToInternalBundle(bundle),
// $FlowFixMe
bundleGraphToInternalBundleGraph(bundleGraph),
configs,
bundleConfigs,
);

return {contents: res.contents};
},
}),
);
} catch (e) {
throw new ThrowableDiagnostic({
diagnostic: errorToDiagnostic(e, {
Expand All @@ -445,7 +450,6 @@ 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 @@ -503,34 +507,37 @@ 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,
bundle,
bundleGraph,
contents: optimized.contents,
map: optimized.map,
getSourceMapReference: map => {
return this.getSourceMapReference(bundle, map);
await tracer.measureAsync(
{
name: optimizer.name,
args: {name: bundle.name},
categories: ['optimize'],
},
options: this.pluginOptions,
logger: new PluginLogger({origin: optimizer.name}),
tracer: new PluginTracer({
origin: optimizer.name,
category: 'optimize',
}),
});

optimized.type = next.type ?? optimized.type;
optimized.contents = next.contents;
optimized.map = next.map;
async () => {
let next = await optimizer.plugin.optimize({
config: configs.get(optimizer.name)?.result,
bundleConfig: bundleConfigs.get(optimizer.name)?.result,
bundle,
bundleGraph,
contents: optimized.contents,
map: optimized.map,
getSourceMapReference: map => {
return this.getSourceMapReference(bundle, map);
},
options: this.pluginOptions,
logger: new PluginLogger({origin: optimizer.name}),
tracer: new PluginTracer({
origin: optimizer.name,
category: 'optimize',
}),
});

optimized.type = next.type ?? optimized.type;
optimized.contents = next.contents;
optimized.map = next.map;
},
);
} catch (e) {
throw new ThrowableDiagnostic({
diagnostic: errorToDiagnostic(e, {
Expand All @@ -539,7 +546,6 @@ 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
36 changes: 21 additions & 15 deletions packages/core/core/src/ReporterRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,32 @@ export default class ReporterRunner {
}
for (let reporter of this.reporters) {
let measurement;
try {
// eslint-disable-next-line require-await
let fn = async () =>
reporter.plugin.report({
event,
options: this.pluginOptions,
logger: new PluginLogger({origin: reporter.name}),
tracer: new PluginTracer({
origin: reporter.name,
category: 'reporter',
}),
});

// 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',
}),
});
await (event.type === 'trace'
? fn()
: tracer.measureAsync(
{
name: reporter.name,
categories: ['reporter'],
},
fn,
));
} catch (reportError) {
INTERNAL_ORIGINAL_CONSOLE.error(reportError);
} finally {
measurement && measurement.end();
}
}
} catch (err) {
Expand Down
35 changes: 19 additions & 16 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,27 @@ export default class Transformation {
}

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

let transformerResults = await this.runTransformer(
pipeline,
asset,
transformer.plugin,
transformer.name,
transformer.config,
transformer.configKeyPath,
this.parcelConfig,
let transformerResults = await tracer.measureAsync(
{
name: transformer.name,
args: {
filename: fromProjectPathRelative(initialAsset.value.filePath),
},
categories: ['transform'],
},
// eslint-disable-next-line require-await
async () =>
this.runTransformer(
pipeline,
asset,
transformer.plugin,
transformer.name,
transformer.config,
transformer.configKeyPath,
this.parcelConfig,
),
);

measurement && measurement.end();

for (let result of transformerResults) {
if (result instanceof UncommittedAsset) {
resultingAssets.push(result);
Expand Down

0 comments on commit 9f8c706

Please sign in to comment.