🪢 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 { VERSION } from "@/src/constants/VERSION";
import { env } from "@/src/env.mjs";
import { prisma } from "@langfuse/shared/src/db";
import {
convertDateToClickhouseDateTime,
logger,
queryClickhouse,
traceException,
} from "@langfuse/shared/src/server";
type HealthCheckInput = {
failIfDatabaseUnavailable: boolean;
failIfNoRecentEvents: boolean;
};
export type HealthCheckResult = {
isHealthy: boolean;
status: string;
version: string;
};
export const runHealthCheck = async ({
failIfDatabaseUnavailable,
failIfNoRecentEvents,
}: HealthCheckInput): Promise<HealthCheckResult> => {
const version = VERSION.replace("v", "");
try {
try {
if (failIfDatabaseUnavailable) {
await prisma.$queryRaw`SELECT 1;`;
}
} catch (error) {
logger.error("Couldn't connect to database", error);
traceException(error);
return {
isHealthy: false,
status: "Database not available",
version,
};
}
try {
if (failIfNoRecentEvents) {
const now = new Date();
const clickhouseNow = convertDateToClickhouseDateTime(now);
if (env.LANGFUSE_MIGRATION_V4_WRITE_MODE === "events_only") {
// In events_only mode the legacy traces/observations tables are no
// longer written, so they would always look stale. Ingestion
// completeness is instead reflected by recent rows in events_core
// (the query-optimized projection of events_full).
const events = await queryClickhouse<{ span_id: string }>({
query: `
SELECT span_id
FROM events_core
WHERE start_time <= {now: DateTime64(3)}
AND start_time >= {now: DateTime64(3)} - INTERVAL 3 MINUTE
LIMIT 1
`,
params: {
now: clickhouseNow,
},
});
if (events.length === 0) {
return {
isHealthy: false,
status: "No events within the last 3 minutes",
version,
};
}
} else {
const traces = await queryClickhouse<{ id: string }>({
query: `
SELECT id
FROM traces
WHERE timestamp <= {now: DateTime64(3)}
AND timestamp >= {now: DateTime64(3)} - INTERVAL 3 MINUTE
LIMIT 1
`,
params: {
now: clickhouseNow,
},
});
const observations = await queryClickhouse<{ id: string }>({
query: `
SELECT id
FROM observations
WHERE start_time <= {now: DateTime64(3)}
AND start_time >= {now: DateTime64(3)} - INTERVAL 3 MINUTE
LIMIT 1
`,
params: {
now: clickhouseNow,
},
});
if (traces.length === 0 || observations.length === 0) {
return {
isHealthy: false,
status: `No ${traces.length === 0 ? "traces" : "observations"} within the last 3 minutes`,
version,
};
}
}
}
} catch (error) {
logger.error("Couldn't fetch recent events", error);
traceException(error);
return {
isHealthy: false,
status: "Couldn't fetch recent events",
version,
};
}
} catch (error) {
traceException(error);
logger.error("Health check failed", error);
return {
isHealthy: false,
status: "Health check failed",
version,
};
}
return {
isHealthy: true,
status: "OK",
version,
};
};