🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
import {
getCurrentSpan,
logger,
MonitorQueue,
QueueJobs,
} from "@langfuse/shared/src/server";
import { MonitorScheduler } from "@langfuse/shared/monitors/server";
import { prisma } from "@langfuse/shared/src/db";
import { v4 } from "uuid";
import { PeriodicExclusiveRunner } from "../../utils/PeriodicExclusiveRunner";
const tickIntervalMs = 30_000;
const lockTtlSeconds = 90;
/** MonitorRunner ticks one shard of the MonitorScheduler. */
export class MonitorRunner extends PeriodicExclusiveRunner {
private readonly schedulerId: number;
private readonly totalSchedulers: number;
private readonly scheduler: MonitorScheduler;
protected get defaultIntervalMs(): number {
return tickIntervalMs;
}
constructor(schedulerId: number, totalSchedulers: number) {
super({
name: `MonitorRunner(${schedulerId + 1}/${totalSchedulers})`,
metricName: "monitor_runner",
metricScope: String(schedulerId + 1),
lockKey: `langfuse:monitor:${schedulerId}`,
lockTtlSeconds,
onUnavailable: "fail",
});
this.schedulerId = schedulerId;
this.totalSchedulers = totalSchedulers;
const queue = MonitorQueue.getInstance();
if (!queue) {
throw new Error(
`${this.instanceName}: MonitorQueue is unavailable; refusing to start without the ability to publish monitor events`,
);
}
this.scheduler = new MonitorScheduler({
schedulerId,
totalSchedulers,
db: prisma,
publish: async (event) => {
await queue.add(QueueJobs.MonitorJob, {
timestamp: new Date(),
id: v4(),
payload: event,
name: QueueJobs.MonitorJob,
});
},
});
}
public override start(): void {
logger.info(`Starting ${this.instanceName}`, {
schedulerId: this.schedulerId,
totalSchedulers: this.totalSchedulers,
intervalMs: tickIntervalMs,
});
super.start();
}
protected async execute(): Promise<void> {
await this.withLock(async () => {
const count = await this.scheduler.schedule(new Date());
getCurrentSpan()?.setAttribute("monitor.published_count", count);
logger.info(`${this.instanceName}: published ${count} events`);
});
}
}