Skip to content

Commit

Permalink
Update original tracing API
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak committed Apr 16, 2024
1 parent df99038 commit 848bebd
Show file tree
Hide file tree
Showing 15 changed files with 560 additions and 487 deletions.
137 changes: 71 additions & 66 deletions packages/core/core/src/PackagerRunner.js
Expand Up @@ -396,51 +396,52 @@ export default class PackagerRunner {

let packager = await this.config.getPackager(bundle.name);
let {name, resolveFrom, plugin} = packager;
let measurement;
try {
return await tracer.measure(
{
measurement =
tracer.enabled &&
tracer.createTraceMeasurement({
name,
args: {bundle: {name: bundle.name, type: bundle.type}},
categories: ['packaging'],
});

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);
},
() =>
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};
},
}),
);
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 @@ -449,6 +450,8 @@ 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 @@ -506,37 +509,37 @@ export default class PackagerRunner {
};

for (let optimizer of optimizers) {
let measurement;
try {
await tracer.measure(
{
measurement =
tracer.enabled &&
tracer.createTraceMeasurement({
name: optimizer.name,
args: {bundle: bundle.name},
categories: ['optimize'],
});

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);
},
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;
},
);
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 @@ -545,6 +548,8 @@ 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
41 changes: 21 additions & 20 deletions packages/core/core/src/ReporterRunner.js
Expand Up @@ -95,31 +95,32 @@ 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!
await (event.type === 'trace'
? fn()
: tracer.measure(
{
name: reporter.name,
categories: ['reporter'],
},
fn,
));
if (event.type !== 'trace' && event.type !== 'traceStart') {
measurement =
tracer.enabled &&
tracer.createTraceMeasurement({
name: reporter.name,
categories: ['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
28 changes: 15 additions & 13 deletions packages/core/core/src/Transformation.js
Expand Up @@ -374,26 +374,28 @@ export default class Transformation {
}

try {
let transformerResults = await tracer.measure(
{
let measurement =
tracer.enabled &&
tracer.createTraceMeasurement({
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,
),
});

let transformerResults = await 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 848bebd

Please sign in to comment.