Skip to content

Commit

Permalink
feat: mark @rspack/tracing as peer dependency (#9008)
Browse files Browse the repository at this point in the history
* feat: mark `@rspack/tracing` as peer dependency

* Update packages/rspack/src/exports.ts

Co-authored-by: neverland <[email protected]>

* chore: update

---------

Co-authored-by: neverland <[email protected]>
  • Loading branch information
2 people authored and hardfist committed Jan 14, 2025
1 parent 4deec10 commit 5296f39
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 31 deletions.
10 changes: 8 additions & 2 deletions packages/rspack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"@discoveryjs/json-ext": "^0.5.7",
"@rspack/dev-server": "1.0.10",
"@rspack/tracing": "workspace:*",
"colorette": "2.0.19",
"exit-hook": "^4.0.0",
"interpret": "^3.1.1",
Expand All @@ -46,6 +45,7 @@
"devDependencies": {
"@rslib/core": "0.2.2",
"@rspack/core": "workspace:*",
"@rspack/tracing": "workspace:*",
"@types/interpret": "^1.1.3",
"@types/rechoir": "^0.6.1",
"@types/webpack-bundle-analyzer": "^4.6.0",
Expand All @@ -57,7 +57,13 @@
"typescript": "^5.7.2"
},
"peerDependencies": {
"@rspack/core": "^1.0.0-alpha || ^1.x"
"@rspack/core": "^1.0.0-alpha || ^1.x",
"@rspack/tracing": "^1.x"
},
"peerDependenciesMeta": {
"@rspack/tracing": {
"optional": true
}
},
"publishConfig": {
"access": "public",
Expand Down
7 changes: 5 additions & 2 deletions packages/rspack-tracing/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@rspack/tracing",
"version": "1.0.12",
"private": true,
"version": "1.2.0-beta.0",
"type": "module",
"description": "Internal tracing package for rspack",
"homepage": "https://rspack.dev",
Expand All @@ -11,6 +10,10 @@
"url": "https://github.com/web-infra-dev/rspack",
"directory": "packages/rspack-tracing"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"license": "MIT",
"main": "./index.js",
"types": "./index.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,25 @@
"webpack-dev-server": "5.0.4",
"webpack-sources": "3.2.3",
"zod": "^3.23.8",
"zod-validation-error": "3.4.0"
"zod-validation-error": "3.4.0",
"@rspack/tracing": "workspace:*"
},
"dependencies": {
"@module-federation/runtime-tools": "0.8.4",
"@rspack/binding": "workspace:*",
"@rspack/tracing": "workspace:*",
"@rspack/lite-tapable": "1.0.1",
"caniuse-lite": "^1.0.30001616"
},
"peerDependencies": {
"@swc/helpers": ">=0.5.1"
"@swc/helpers": ">=0.5.1",
"@rspack/tracing": "^1.x"
},
"peerDependenciesMeta": {
"@swc/helpers": {
"optional": true
},
"@rspack/tracing": {
"optional": true
}
}
}
19 changes: 15 additions & 4 deletions packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,25 @@ export const experiments: Experiments = {
async register(filter, layer, output) {
registerGlobalTrace(filter, layer, output);
if (layer === "otel") {
const { initOpenTelemetry } = await import("@rspack/tracing");
await initOpenTelemetry();
try {
const { initOpenTelemetry } = await import("@rspack/tracing");
await initOpenTelemetry();
} catch (error) {
console.error(
"Failed to import `@rspack/tracing` package. Please install `@rspack/tracing` to enable OpenTelemetry tracing.",
error
);
}
}
},
async cleanup() {
cleanupGlobalTrace();
const { shutdownOpenTelemetry } = await import("@rspack/tracing");
await shutdownOpenTelemetry();
try {
const { shutdownOpenTelemetry } = await import("@rspack/tracing");
await shutdownOpenTelemetry();
} catch (error) {
// ignore cleanup tracing error
}
}
},
RemoveDuplicateModulesPlugin
Expand Down
37 changes: 23 additions & 14 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,29 @@ function getCurrentLoader(
return null;
}

async function tryTrace(context: JsLoaderContext) {
try {
const {
trace,
propagation,
context: tracingContext
} = await import("@rspack/tracing");
const tracer = trace.getTracer("rspack-loader-runner");
const activeContext = propagation.extract(
tracingContext.active(),
context.__internal__tracingCarrier
);
return { tracer, activeContext };
} catch (error) {
return { tracer: null, activeContext: null };
}
}

export async function runLoaders(
compiler: Compiler,
context: JsLoaderContext
): Promise<JsLoaderContext> {
const {
trace,
propagation,
context: tracingContext
} = await import("@rspack/tracing");
const tracer = trace.getTracer("rspack-loader-runner");
const activeContext = propagation.extract(
tracingContext.active(),
context.__internal__tracingCarrier
);
const { tracer, activeContext } = await tryTrace(context);

const loaderState = context.loaderState;

Expand Down Expand Up @@ -810,7 +819,7 @@ export async function runLoaders(
currentLoaderObject.pitchExecuted = true;
if (!fn) continue;

const span = tracer.startSpan(
const span = tracer?.startSpan(
"LoaderRunner:pitch",
{
attributes: {
Expand All @@ -825,7 +834,7 @@ export async function runLoaders(
loaderContext.previousRequest,
currentLoaderObject.data
])) || [];
span.end();
span?.end();

const hasArg = args.some(value => value !== undefined);

Expand Down Expand Up @@ -862,7 +871,7 @@ export async function runLoaders(
const args = [content, sourceMap, additionalData];
convertArgs(args, !!currentLoaderObject.raw);

const span = tracer.startSpan(
const span = tracer?.startSpan(
"LoaderRunner:normal",
{
attributes: {
Expand All @@ -873,7 +882,7 @@ export async function runLoaders(
);
[content, sourceMap, additionalData] =
(await runSyncOrAsync(fn, loaderContext, args)) || [];
span.end();
span?.end();
}

context.content = isNil(content) ? null : toBuffer(content);
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5296f39

Please sign in to comment.