Skip to content

Commit

Permalink
Add measure tracing API
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak committed Apr 15, 2024
1 parent 179f63a commit 408a1c8
Show file tree
Hide file tree
Showing 14 changed files with 577 additions and 429 deletions.
139 changes: 72 additions & 67 deletions packages/core/core/src/PackagerRunner.js
Expand Up @@ -396,47 +396,51 @@ 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.measure(
{
name,
args: {bundle: {name: bundle.name, type: bundle.type}},
categories: ['packaging'],
},
});
() =>
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 +449,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 +506,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.measure(
{
name: optimizer.name,
args: {bundle: 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 +545,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
35 changes: 20 additions & 15 deletions packages/core/core/src/ReporterRunner.js
Expand Up @@ -95,26 +95,31 @@ export default class ReporterRunner {
}
for (let reporter of this.reporters) {
let measurement;
try {
let fn = () =>
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.measure(
{
name: reporter.name,
categories: ['reporter'],
},
fn,
));
} catch (reportError) {
INTERNAL_ORIGINAL_CONSOLE.error(reportError);
} finally {
measurement && measurement.end();
}
}
} catch (err) {
Expand Down
34 changes: 18 additions & 16 deletions packages/core/core/src/Transformation.js
Expand Up @@ -374,24 +374,26 @@ 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.measure(
{
name: transformer.name,
args: {
filename: fromProjectPathRelative(initialAsset.value.filePath),
},
categories: ['transform'],
},
() =>
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 408a1c8

Please sign in to comment.