Skip to content
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
2 changes: 2 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
23 changes: 15 additions & 8 deletions apps/webapp/app/eventLoopMonitor.server.ts
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -26,6 +28,7 @@ function before(asyncId: number) {
cache.set(asyncId, {
...cached,
start: process.hrtime(),
parentCtx: context.active(),
});
}

Expand All @@ -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();
}
Expand Down
Loading