Skip to content

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

fix(cloud): fallback to clikchouse when usage meter cannot be fetched from stripe (#5232)

Marc Klingene27f0bdc1d4f
diff --git a/web/src/ee/features/billing/server/cloudBillingRouter.ts b/web/src/ee/features/billing/server/cloudBillingRouter.ts
index 88c0910153..c2e8c469c6 100644
--- a/web/src/ee/features/billing/server/cloudBillingRouter.ts
+++ b/web/src/ee/features/billing/server/cloudBillingRouter.ts
@@ -12,7 +12,10 @@ import { TRPCError } from "@trpc/server";
import * as z from "zod";
import { throwIfNoOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { auditLog } from "@/src/features/audit-logs/auditLog";
-import { getObservationCountOfProjectsSinceCreationDate } from "@langfuse/shared/src/server";
+import {
+ getObservationCountOfProjectsSinceCreationDate,
+ logger,
+} from "@langfuse/shared/src/server";
export const cloudBillingRouter = createTRPCRouter({
createStripeCheckoutSession: protectedOrganizationProcedure
@@ -377,34 +380,43 @@ export const cloudBillingRouter = createTRPCRouter({
end: new Date(subscription.current_period_end * 1000),
};
- const stripeInvoice = await stripeClient.invoices.retrieveUpcoming({
- subscription: parsedOrg.cloudConfig.stripe.activeSubscriptionId,
- });
- const upcomingInvoice = {
- usdAmount: stripeInvoice.amount_due / 100,
- date: new Date(stripeInvoice.period_end * 1000),
- };
- const usageInvoiceLines = stripeInvoice.lines.data.filter((line) =>
- Boolean(line.plan?.meter),
- );
- const usage = usageInvoiceLines.reduce((acc, line) => {
- if (line.quantity) {
- return acc + line.quantity;
- }
- return acc;
- }, 0);
- // get meter for usage type (events or observations)
- const meterId = usageInvoiceLines[0]?.plan?.meter;
- const meter = meterId
- ? await stripeClient.billing.meters.retrieve(meterId)
- : undefined;
-
- return {
- usageCount: usage,
- usageType: meter?.display_name.toLowerCase() ?? "events",
- billingPeriod,
- upcomingInvoice,
- };
+ try {
+ const stripeInvoice = await stripeClient.invoices.retrieveUpcoming({
+ subscription: parsedOrg.cloudConfig.stripe.activeSubscriptionId,
+ });
+ const upcomingInvoice = {
+ usdAmount: stripeInvoice.amount_due / 100,
+ date: new Date(stripeInvoice.period_end * 1000),
+ };
+ const usageInvoiceLines = stripeInvoice.lines.data.filter((line) =>
+ Boolean(line.plan?.meter),
+ );
+ const usage = usageInvoiceLines.reduce((acc, line) => {
+ if (line.quantity) {
+ return acc + line.quantity;
+ }
+ return acc;
+ }, 0);
+ // get meter for usage type (events or observations)
+ const meterId = usageInvoiceLines[0]?.plan?.meter;
+ const meter = meterId
+ ? await stripeClient.billing.meters.retrieve(meterId)
+ : undefined;
+
+ return {
+ usageCount: usage,
+ usageType: meter?.display_name.toLowerCase() ?? "events",
+ billingPeriod,
+ upcomingInvoice,
+ };
+ } catch (e) {
+ logger.error(
+ "Failed to get usage from Stripe, using usage from Clickhouse",
+ {
+ error: e,
+ },
+ );
+ }
}
}
@@ -420,39 +432,6 @@ export const cloudBillingRouter = createTRPCRouter({
start: thirtyDaysAgo,
});
- // const usageArr = await Promise.all([
- // ctx.prisma.observation.count({
- // where: {
- // project: {
- // orgId: input.orgId,
- // },
- // createdAt: {
- // gte: thirtyDaysAgo,
- // },
- // },
- // }),
- // ctx.prisma.trace.count({
- // where: {
- // project: {
- // orgId: input.orgId,
- // },
- // createdAt: {
- // gte: thirtyDaysAgo,
- // },
- // },
- // }),
- // ctx.prisma.score.count({
- // where: {
- // project: {
- // orgId: input.orgId,
- // },
- // createdAt: {
- // gte: thirtyDaysAgo,
- // },
- // },
- // }),
- // ]);
-
return {
usageCount: countObservations,
usageType: "observations",