From 17714cb235452a029b0260c5824d4e62edf1f4cb Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Thu, 21 Aug 2025 16:10:11 +0100 Subject: [PATCH] fix(webapp): correctly associate event-loop-blocked spans with parent span context --- apps/webapp/app/env.server.ts | 2 ++ apps/webapp/app/eventLoopMonitor.server.ts | 23 ++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apps/webapp/app/env.server.ts b/apps/webapp/app/env.server.ts index 9c95f902c3..10bb697c7f 100644 --- a/apps/webapp/app/env.server.ts +++ b/apps/webapp/app/env.server.ts @@ -1060,6 +1060,8 @@ const EnvironmentSchema = z.object({ // AI Run Filter AI_RUN_FILTER_MODEL: z.string().optional(), + + EVENT_LOOP_MONITOR_THRESHOLD_MS: z.coerce.number().int().default(100), }); export type Environment = z.infer; diff --git a/apps/webapp/app/eventLoopMonitor.server.ts b/apps/webapp/app/eventLoopMonitor.server.ts index db25a28137..42e982bdb9 100644 --- a/apps/webapp/app/eventLoopMonitor.server.ts +++ b/apps/webapp/app/eventLoopMonitor.server.ts @@ -1,10 +1,12 @@ import { createHook } from "node:async_hooks"; import { singleton } from "./utils/singleton"; import { tracer } from "./v3/tracer.server"; +import { env } from "./env.server"; +import { context, Context } from "@opentelemetry/api"; -const THRESHOLD_NS = 1e8; // 100ms +const THRESHOLD_NS = env.EVENT_LOOP_MONITOR_THRESHOLD_MS * 1e6; -const cache = new Map(); +const cache = new Map(); function init(asyncId: number, type: string, triggerAsyncId: number, resource: any) { cache.set(asyncId, { @@ -26,6 +28,7 @@ function before(asyncId: number) { cache.set(asyncId, { ...cached, start: process.hrtime(), + parentCtx: context.active(), }); } @@ -47,13 +50,17 @@ function after(asyncId: number) { if (diffNs > THRESHOLD_NS) { const time = diffNs / 1e6; // in ms - const newSpan = tracer.startSpan("event-loop-blocked", { - startTime: new Date(new Date().getTime() - time), - attributes: { - asyncType: cached.type, - label: "EventLoopMonitor", + const newSpan = tracer.startSpan( + "event-loop-blocked", + { + startTime: new Date(new Date().getTime() - time), + attributes: { + asyncType: cached.type, + label: "EventLoopMonitor", + }, }, - }); + cached.parentCtx + ); newSpan.end(); }