Skip to content

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

fix(worker): keep spend alert billing skips healthy (#13467)

Max Deichmann0b9c464c73e2
diff --git a/worker/src/ee/cloudSpendAlerts/__tests__/handleCloudSpendAlertJob.test.ts b/worker/src/ee/cloudSpendAlerts/__tests__/handleCloudSpendAlertJob.test.ts
new file mode 100644
index 0000000000..a9b9889d80
--- /dev/null
+++ b/worker/src/ee/cloudSpendAlerts/__tests__/handleCloudSpendAlertJob.test.ts
@@ -0,0 +1,145 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { Job } from "bullmq";
+
+const {
+ mockOrganizationFindFirst,
+ mockLoggerInfo,
+ mockLoggerWarn,
+ mockLoggerError,
+ mockLoggerDebug,
+ mockRecordIncrement,
+ mockTraceException,
+ mockSendCloudSpendAlertEmail,
+} = vi.hoisted(() => ({
+ mockOrganizationFindFirst: vi.fn(),
+ mockLoggerInfo: vi.fn(),
+ mockLoggerWarn: vi.fn(),
+ mockLoggerError: vi.fn(),
+ mockLoggerDebug: vi.fn(),
+ mockRecordIncrement: vi.fn(),
+ mockTraceException: vi.fn(),
+ mockSendCloudSpendAlertEmail: vi.fn(),
+}));
+
+vi.mock("@langfuse/shared", () => ({
+ parseDbOrg: (org: unknown) => org,
+ Role: {
+ ADMIN: "ADMIN",
+ OWNER: "OWNER",
+ },
+}));
+
+vi.mock("@langfuse/shared/src/db", () => ({
+ prisma: {
+ organization: {
+ findFirst: mockOrganizationFindFirst,
+ },
+ organizationMembership: {
+ findMany: vi.fn(),
+ },
+ cloudSpendAlert: {
+ update: vi.fn(),
+ },
+ },
+}));
+
+vi.mock("@langfuse/shared/src/server", () => ({
+ logger: {
+ info: mockLoggerInfo,
+ warn: mockLoggerWarn,
+ error: mockLoggerError,
+ debug: mockLoggerDebug,
+ },
+ recordIncrement: mockRecordIncrement,
+ traceException: mockTraceException,
+ sendCloudSpendAlertEmail: mockSendCloudSpendAlertEmail,
+}));
+
+vi.mock("../../../env", () => ({
+ env: {
+ STRIPE_SECRET_KEY: "sk_test",
+ },
+}));
+
+import { handleCloudSpendAlertJob } from "../handleCloudSpendAlertJob";
+
+const createJob = (orgId = "org-1") =>
+ ({
+ data: { orgId },
+ }) as Job<{ orgId: string }>;
+
+const createOrg = (cloudConfig: Record<string, unknown>) => ({
+ id: "org-1",
+ name: "Test Org",
+ cloudConfig,
+ cloudSpendAlerts: [
+ {
+ id: "alert-1",
+ title: "Default Spend alert ($200)",
+ threshold: { toString: () => "200" },
+ triggeredAt: null,
+ },
+ ],
+});
+
+describe("handleCloudSpendAlertJob", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("skips with a healthy span when the Stripe customer id is missing", async () => {
+ mockOrganizationFindFirst.mockResolvedValue(
+ createOrg({
+ plan: "cloud:core",
+ stripe: {},
+ }),
+ );
+
+ await expect(
+ handleCloudSpendAlertJob(createJob()),
+ ).resolves.toBeUndefined();
+
+ expect(mockLoggerWarn).toHaveBeenCalledWith(
+ "[CLOUD SPEND ALERTS] Stripe customer id not found for org org-1",
+ );
+ expect(mockTraceException).not.toHaveBeenCalled();
+ expect(mockLoggerError).not.toHaveBeenCalled();
+ expect(mockRecordIncrement).toHaveBeenCalledWith(
+ "langfuse.queue.cloud_spend_alert_queue.skipped_orgs_missing_billing_config",
+ 1,
+ {
+ unit: "organizations",
+ reason: "missing_stripe_customer_id",
+ },
+ );
+ });
+
+ it("skips with a healthy span when the Stripe subscription id is missing", async () => {
+ mockOrganizationFindFirst.mockResolvedValue(
+ createOrg({
+ plan: "cloud:core",
+ stripe: {
+ customerId: "cus_test",
+ },
+ }),
+ );
+
+ await expect(
+ handleCloudSpendAlertJob(createJob()),
+ ).resolves.toBeUndefined();
+
+ expect(mockLoggerWarn).toHaveBeenCalledWith(
+ "[CLOUD SPEND ALERTS] Stripe subscription id not found for org org-1",
+ );
+ expect(mockTraceException).not.toHaveBeenCalled();
+ expect(mockLoggerError).not.toHaveBeenCalled();
+ expect(mockRecordIncrement).toHaveBeenCalledWith(
+ "langfuse.queue.cloud_spend_alert_queue.skipped_orgs_missing_billing_config",
+ 1,
+ {
+ unit: "organizations",
+ reason: "missing_stripe_subscription_id",
+ },
+ );
+ });
+});
diff --git a/worker/src/ee/cloudSpendAlerts/handleCloudSpendAlertJob.ts b/worker/src/ee/cloudSpendAlerts/handleCloudSpendAlertJob.ts
index e3b4c40087..adc40ca824 100644
--- a/worker/src/ee/cloudSpendAlerts/handleCloudSpendAlertJob.ts
+++ b/worker/src/ee/cloudSpendAlerts/handleCloudSpendAlertJob.ts
@@ -8,6 +8,19 @@ import { Job } from "bullmq";
import { backOff } from "exponential-backoff";
import { sendCloudSpendAlertEmail } from "@langfuse/shared/src/server";
+const recordMissingBillingConfigSkip = (
+ reason: "missing_stripe_customer_id" | "missing_stripe_subscription_id",
+) => {
+ recordIncrement(
+ "langfuse.queue.cloud_spend_alert_queue.skipped_orgs_missing_billing_config",
+ 1,
+ {
+ unit: "organizations",
+ reason,
+ },
+ );
+};
+
export const handleCloudSpendAlertJob = async (job: Job<{ orgId: string }>) => {
const { orgId } = job.data;
@@ -55,23 +68,19 @@ export const handleCloudSpendAlertJob = async (job: Job<{ orgId: string }>) => {
// Get Stripe customer ID
const stripeCustomerId = org.cloudConfig?.stripe?.customerId;
if (!stripeCustomerId) {
- logger.error(
- `[CLOUD SPEND ALERTS] Stripe customer id not found for org ${orgId}`,
- );
- traceException(
+ logger.warn(
`[CLOUD SPEND ALERTS] Stripe customer id not found for org ${orgId}`,
);
+ recordMissingBillingConfigSkip("missing_stripe_customer_id");
return;
}
// Get Stripe subscription ID
const stripeSubscriptionId = org.cloudConfig?.stripe?.activeSubscriptionId;
if (!stripeSubscriptionId) {
- logger.error(
- `[CLOUD SPEND ALERTS] Stripe subscription id not found for org ${orgId}`,
- );
- traceException(
+ logger.warn(
`[CLOUD SPEND ALERTS] Stripe subscription id not found for org ${orgId}`,
);
+ recordMissingBillingConfigSkip("missing_stripe_subscription_id");
return;
}