Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should not cache tracing instance #9086

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/rspack-tracing/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function initOpenTelemetry(): Promise<void>;
export function shutdownOpenTelemetry(): Promise<void>;
export { trace, propagation, context } from "@opentelemetry/api";
export type { TraceAPI, ContextAPI, PropagationAPI } from "@opentelemetry/api";
export type { Tracer, Context } from "@opentelemetry/api";
60 changes: 44 additions & 16 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SourceMapSource
} from "webpack-sources";

import type { Context, Tracer } from "@rspack/tracing";
import type { ContextAPI, PropagationAPI, TraceAPI } from "@rspack/tracing";
import type { Compilation } from "../Compilation";
import type { Compiler } from "../Compiler";
import { Module } from "../Module";
Expand Down Expand Up @@ -340,36 +340,64 @@ function getCurrentLoader(
}
return null;
}
let tracingCache!: { tracer?: Tracer; activeContext?: Context };
async function tryTrace(context: JsLoaderContext) {

// FIXME: a temporary fix, we may need to change @rspack/tracing to commonjs really fix it
let cachedTracing:
| {
trace: TraceAPI;
propagation: PropagationAPI;
context: ContextAPI;
}
| null
| undefined;

async function getCachedTracing() {
// disable tracing in non-profile mode
if (!process.env.RSPACK_PROFILE) {
return {};
cachedTracing = null;
return cachedTracing;
}
try {
const {
trace,
propagation,
context: tracingContext
} = await import("@rspack/tracing");
if (cachedTracing) {
return cachedTracing;
}
if (cachedTracing === undefined) {
hardfist marked this conversation as resolved.
Show resolved Hide resolved
try {
const tracing = await import("@rspack/tracing");
cachedTracing = {
trace: tracing.trace,
propagation: tracing.propagation,
context: tracing.context
};
return cachedTracing;
} catch (e) {
cachedTracing = null;
return cachedTracing;
}
} else {
cachedTracing = null;
return cachedTracing;
}
}

async function tryTrace(context: JsLoaderContext) {
const cachedTracing = await getCachedTracing();
if (cachedTracing) {
const { trace, propagation, context: tracingContext } = cachedTracing;
const tracer = trace.getTracer("rspack-loader-runner");
const activeContext = propagation.extract(
tracingContext.active(),
context.__internal__tracingCarrier
);
tracingCache = { tracer, activeContext };
return tracingCache;
} catch (error) {
tracingCache = {};
return tracingCache;
return { trace, tracer, activeContext };
}
return null;
}

export async function runLoaders(
compiler: Compiler,
context: JsLoaderContext
): Promise<JsLoaderContext> {
const { tracer, activeContext } = await tryTrace(context);
const { tracer, activeContext } = (await tryTrace(context)) ?? {};

const loaderState = context.loaderState;

Expand Down
Loading