Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: add new /ready endpoint to separate liveness from readiness (#3222)

Steffen Schmitze3a837b561fb
diff --git a/web/src/pages/api/public/health.ts b/web/src/pages/api/public/health.ts
index 0ee89ae1f8..a1a93ec289 100644
--- a/web/src/pages/api/public/health.ts
+++ b/web/src/pages/api/public/health.ts
@@ -1,7 +1,6 @@
import { VERSION } from "@/src/constants";
import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
import { telemetry } from "@/src/features/telemetry";
-import { isSigtermReceived } from "@/src/utils/shutdown";
import { prisma } from "@langfuse/shared/src/db";
import { traceException } from "@langfuse/shared/src/server";
import { type NextApiRequest, type NextApiResponse } from "next";
@@ -16,15 +15,6 @@ export default async function handler(
const failIfNoRecentEvents = req.query.failIfNoRecentEvents === "true";
try {
- if (isSigtermReceived()) {
- console.log(
- "Health check failed: SIGTERM / SIGINT received, shutting down.",
- );
- return res.status(500).json({
- status: "SIGTERM / SIGINT received, shutting down",
- version: VERSION.replace("v", ""),
- });
- }
await prisma.$queryRaw`SELECT 1;`;
if (failIfNoRecentEvents) {
diff --git a/web/src/pages/api/public/ready.ts b/web/src/pages/api/public/ready.ts
new file mode 100644
index 0000000000..ec817b5752
--- /dev/null
+++ b/web/src/pages/api/public/ready.ts
@@ -0,0 +1,37 @@
+import { VERSION } from "@/src/constants";
+import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
+import { telemetry } from "@/src/features/telemetry";
+import { isSigtermReceived } from "@/src/utils/shutdown";
+import { traceException } from "@langfuse/shared/src/server";
+import { type NextApiRequest, type NextApiResponse } from "next";
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ try {
+ await runMiddleware(req, res, cors);
+ await telemetry();
+
+ if (isSigtermReceived()) {
+ console.log(
+ "Readiness check failed: SIGTERM / SIGINT received, shutting down.",
+ );
+ return res.status(500).json({
+ status: "SIGTERM / SIGINT received, shutting down",
+ version: VERSION.replace("v", ""),
+ });
+ }
+ } catch (e) {
+ traceException(e);
+ console.log("Readiness check failed: ", e);
+ return res.status(503).json({
+ status: "Readiness check failed",
+ version: VERSION.replace("v", ""),
+ });
+ }
+ return res.status(200).json({
+ status: "OK",
+ version: VERSION.replace("v", ""),
+ });
+}
diff --git a/worker/src/api/index.ts b/worker/src/api/index.ts
index 767b037a2f..3138d35d6c 100644
--- a/worker/src/api/index.ts
+++ b/worker/src/api/index.ts
@@ -23,7 +23,7 @@ type EventsResponse = {
router.get<{}, { status: string }>("/health", async (_req, res) => {
try {
- await checkContainerHealth(res);
+ await checkContainerHealth(res, false);
} catch (e) {
traceException(e);
logger.error(e, "Health check failed");
@@ -33,6 +33,18 @@ router.get<{}, { status: string }>("/health", async (_req, res) => {
}
});
+router.get<{}, { status: string }>("/ready", async (_req, res) => {
+ try {
+ await checkContainerHealth(res, true);
+ } catch (e) {
+ traceException(e);
+ logger.error(e, "Readiness check failed");
+ res.status(500).json({
+ status: "error",
+ });
+ }
+});
+
router
.use(basicAuth({ users: { admin: env.LANGFUSE_WORKER_PASSWORD } }))
.get<
@@ -48,7 +60,7 @@ router
});
logger.info(
- `Clickhouse health check response: ${JSON.stringify(await response.text())}`
+ `Clickhouse health check response: ${JSON.stringify(await response.text())}`,
);
res.json({ status: "success" });
@@ -66,7 +78,7 @@ router
.use(
basicAuth({
users: { admin: env.LANGFUSE_WORKER_PASSWORD },
- })
+ }),
)
.post<{}, EventsResponse>("/events", async (req, res) => {
try {
@@ -93,7 +105,7 @@ router
if (traceUpsertQueue) {
logger.info(
`Added ${jobs.length} trace upsert jobs to the queue`,
- jobs
+ jobs,
);
}
@@ -129,7 +141,7 @@ router
.use(
basicAuth({
users: { admin: env.LANGFUSE_WORKER_PASSWORD },
- })
+ }),
)
.post("/ingestion", async (req, res) => {
return res.status(200).send(); // Not implemented, Send 200 to acknowledge the request for web containers to not throw
diff --git a/worker/src/features/health/index.ts b/worker/src/features/health/index.ts
index a74f17c648..241911ce51 100644
--- a/worker/src/features/health/index.ts
+++ b/worker/src/features/health/index.ts
@@ -3,10 +3,17 @@ import { redis } from "@langfuse/shared/src/server";
import { Response } from "express";
import logger from "../../logger";
-export const checkContainerHealth = async (res: Response) => {
- if (isSigtermReceived()) {
+/**
+ * Check the health of the container.
+ * If failOnSigterm is true, the health check will fail if a SIGTERM signal has been received.
+ */
+export const checkContainerHealth = async (
+ res: Response,
+ failOnSigterm: boolean,
+) => {
+ if (failOnSigterm && isSigtermReceived()) {
logger.info(
- "Health check failed: SIGTERM / SIGINT received, shutting down."
+ "Health check failed: SIGTERM / SIGINT received, shutting down.",
);
return res.status(500).json({
status: "SIGTERM / SIGINT received, shutting down",
@@ -25,8 +32,8 @@ export const checkContainerHealth = async (res: Response) => {
new Promise((_, reject) =>
setTimeout(
() => reject(new Error("Redis ping timeout after 2 seconds")),
- 2000
- )
+ 2000,
+ ),
),
]);