fix(webapp): correctly associate event-loop-blocked spans with parent span context (#2435)

This commit is contained in:
Eric Allam
2025-08-21 16:21:26 +01:00
committed by GitHub
parent bf2120d5a7
commit c7e3811794
2 changed files with 17 additions and 8 deletions
+2
View File
@@ -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<typeof EnvironmentSchema>;
+15 -8
View File
@@ -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<number, { type: string; start?: [number, number] }>();
const cache = new Map<number, { type: string; start?: [number, number]; parentCtx?: Context }>();
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();
}