🪢 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
fix: add timeouts to webhooks (#7924)
Max Deichmann0cda8e2e21b8
diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.tsindex e1f1e0b41c..b6c85ff7fc 100644--- a/packages/shared/src/server/index.ts+++ b/packages/shared/src/server/index.ts@@ -66,7 +66,5 @@ export * from "./clickhouse/measureAndReturn";export * from "./data-deletion/ingestionFileDeletion";export * from "./s3";-export * from "./automations/webhooks";-// test utilsexport * from "./test-utils";diff --git a/worker/src/__tests__/webhooks.test.ts b/worker/src/__tests__/webhooks.test.tsindex 375b7b7b61..ea23c36f51 100644--- a/worker/src/__tests__/webhooks.test.ts+++ b/worker/src/__tests__/webhooks.test.ts@@ -11,13 +11,13 @@ import { v4 } from "uuid";import {ActionExecutionStatus,JobConfigState,+ JsonNested,PromptDomainSchema,WebhookActionConfigWithSecrets,} from "@langfuse/shared";import {WebhookInput,createOrgProjectAndApiKey,- executeWebhook,} from "@langfuse/shared/src/server";import { prisma } from "@langfuse/shared/src/db";import {@@ -28,6 +28,7 @@ import {import { generateWebhookSecret } from "@langfuse/shared/encryption";import { setupServer } from "msw/node";import { http, HttpResponse } from "msw";+import { executeWebhook } from "../queues/webhooks";// Mock webhook server for testing HTTP requestsclass WebhookTestServer {diff --git a/worker/src/app.ts b/worker/src/app.tsindex ca778e25ed..45ef89c2f6 100644--- a/worker/src/app.ts+++ b/worker/src/app.ts@@ -16,7 +16,6 @@ import {} from "./queues/evalQueue";import { batchExportQueueProcessor } from "./queues/batchExportQueue";import { onShutdown } from "./utils/shutdown";-import helmet from "helmet";import { cloudUsageMeteringQueueProcessor } from "./queues/cloudUsageMeteringQueue";import { WorkerManager } from "./queues/workerManager";@@ -30,7 +29,6 @@ import {BlobStorageIntegrationQueue,DeadLetterRetryQueue,IngestionQueue,- webhookProcessor,} from "@langfuse/shared/src/server";import { env } from "./env";import { ingestionQueueProcessorBuilder } from "./queues/ingestionQueue";@@ -56,6 +54,7 @@ import { batchActionQueueProcessor } from "./queues/batchActionQueue";import { scoreDeleteProcessor } from "./queues/scoreDelete";import { DlqRetryService } from "./services/dlq/dlqRetryService";import { entityChangeQueueProcessor } from "./queues/entityChangeQueue";+import { webhookProcessor } from "./queues/webhooks";const app = express();diff --git a/worker/src/env.ts b/worker/src/env.tsindex 96bdcef001..65ec921628 100644--- a/worker/src/env.ts+++ b/worker/src/env.ts@@ -242,6 +242,7 @@ const EnvSchema = z.object({.number().positive().default(5),+ LANGFUSE_WEBHOOK_TIMEOUT_MS: z.coerce.number().positive().default(10000),LANGFUSE_ENTITY_CHANGE_QUEUE_PROCESSING_CONCURRENCY: z.coerce.number().positive()diff --git a/packages/shared/src/server/automations/webhooks.ts b/worker/src/queues/webhooks.tssimilarity index 77%rename from packages/shared/src/server/automations/webhooks.tsrename to worker/src/queues/webhooks.tsindex 08218497eb..48dfabc7a6 100644--- a/packages/shared/src/server/automations/webhooks.ts+++ b/worker/src/queues/webhooks.ts@@ -1,24 +1,25 @@-import { Job, Processor } from "bullmq";-import { backOff } from "exponential-backoff";-import {- ActionExecutionStatus,- JobConfigState,-} from "../../../prisma/generated/types";import {+ InternalServerError,PromptWebhookOutboundSchema,WebhookDefaultHeaders,-} from "../../domain";-import { prisma } from "../../db";-import { TQueueJobTypes, QueueName, WebhookInput } from "../queues";+ ActionExecutionStatus,+ LangfuseNotFoundError,+ JobConfigState,+} from "@langfuse/shared";+import { decrypt, createSignatureHeader } from "@langfuse/shared/encryption";+import { prisma } from "@langfuse/shared/src/db";import {- getActionByIdWithSecrets,+ TQueueJobTypes,+ QueueName,+ WebhookInput,getAutomationById,+ getActionByIdWithSecrets,getConsecutiveAutomationFailures,-} from "../repositories";-import { logger } from "..";-import { createSignatureHeader } from "../../encryption/signature";-import { decrypt } from "../../encryption";-import { InternalServerError, LangfuseNotFoundError } from "../../errors";+ logger,+} from "@langfuse/shared/src/server";+import { Processor, Job } from "bullmq";+import { backOff } from "exponential-backoff";+import { env } from "../env";export const webhookProcessor: Processor = async (job: Job<TQueueJobTypes[QueueName.WebhookQueue]>,@@ -132,22 +133,44 @@ export const executeWebhook = async (input: WebhookInput) => {webhookPayload,)} and headers ${JSON.stringify(requestHeaders)}`,);- const res = await fetch(webhookConfig.url, {- method: "POST",- body: webhookPayload,- headers: requestHeaders,- });-- httpStatus = res.status;- responseBody = await res.text();- if (res.status !== 200) {- logger.warn(- `Webhook does not return 200: failed with status ${res.status} for url ${webhookConfig.url} and project ${projectId}. Body: ${responseBody}`,- );- throw new Error(- `Webhook does not return 200: failed with status ${res.status} for url ${webhookConfig.url} and project ${projectId}`,- );+ // Create AbortController for timeout+ const abortController = new AbortController();+ const timeoutId = setTimeout(() => {+ abortController.abort();+ }, env.LANGFUSE_WEBHOOK_TIMEOUT_MS);++ try {+ const res = await fetch(webhookConfig.url, {+ method: "POST",+ body: webhookPayload,+ headers: requestHeaders,+ signal: abortController.signal,+ });++ httpStatus = res.status;+ responseBody = await res.text();++ if (res.status !== 200) {+ logger.warn(+ `Webhook does not return 200: failed with status ${res.status} for url ${webhookConfig.url} and project ${projectId}. Body: ${responseBody}`,+ );+ throw new Error(+ `Webhook does not return 200: failed with status ${res.status} for url ${webhookConfig.url} and project ${projectId}`,+ );+ }+ } catch (error) {+ if (error instanceof Error && error.name === "AbortError") {+ logger.warn(+ `Webhook timeout after ${env.LANGFUSE_WEBHOOK_TIMEOUT_MS}ms for url ${webhookConfig.url} and project ${projectId}`,+ );+ throw new Error(+ `Webhook timeout after ${env.LANGFUSE_WEBHOOK_TIMEOUT_MS}ms for url ${webhookConfig.url} and project ${projectId}`,+ );+ }+ throw error;+ } finally {+ clearTimeout(timeoutId);}},{