Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(otel): attach body reader before shadow work (#17657)

Valery Meleshkincb760ea79e72
diff --git a/web/src/pages/api/public/otel/v1/traces/index.ts b/web/src/pages/api/public/otel/v1/traces/index.ts
index b8c563b6ea..74516f6739 100644
--- a/web/src/pages/api/public/otel/v1/traces/index.ts
+++ b/web/src/pages/api/public/otel/v1/traces/index.ts
@@ -42,19 +42,27 @@ export default withMiddlewares({
// Mark project as using OTEL API
await markProjectAsOtelUser(auth.scope.projectId);
+ const maxBodyBytes = env.LANGFUSE_OTEL_INGESTION_MAX_BODY_BYTES;
+ // Start reading before shadow work so a fast request cannot finish while
+ // the stream is still unobserved.
+ const bodyResultPromise = readOtelRequestBody(req, maxBodyBytes).then(
+ (body) => ({ success: true as const, body }),
+ (error: unknown) => ({ success: false as const, error }),
+ );
+
if (env.LANGFUSE_OTEL_INGESTION_WORKER_SHADOW_ENABLED === "true") {
const { startOtelIngestionWorkerAdmissionShadow } =
await import("@/src/server/otel/otelIngestionWorkerShadow");
startOtelIngestionWorkerAdmissionShadow(res, auth.scope.projectId);
}
- const maxBodyBytes = env.LANGFUSE_OTEL_INGESTION_MAX_BODY_BYTES;
-
let body: Buffer;
let encodedBodyBytes: number;
let bodyFailureMessage = "Failed to read request body";
try {
- body = await readOtelRequestBody(req, maxBodyBytes);
+ const bodyResult = await bodyResultPromise;
+ if (!bodyResult.success) throw bodyResult.error;
+ body = bodyResult.body;
encodedBodyBytes = body.byteLength;
if (req.headers["content-encoding"]?.includes("gzip")) {