Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type IOPacket, packetRequiresOffloading, tryCatch } from "@trigger.dev/
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { env } from "~/env.server";
import { uploadPacketToObjectStore } from "~/v3/objectStore.server";
import { logger } from "~/services/logger.server";
import { ServiceValidationError } from "~/v3/services/common.server";

function packetExtensionForDataType(dataType: string): string {
Expand Down Expand Up @@ -53,6 +54,11 @@ export async function processWaitpointCompletionPacket(
);

if (uploadError) {
logger.error("Failed to upload large waitpoint to object store", {
error: uploadError,
filename,
environmentId: environment.id,
});
throw new ServiceValidationError("Failed to upload large waitpoint to object store", 500);
}

Expand Down
94 changes: 51 additions & 43 deletions apps/webapp/app/services/platform.v3.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { newProjectPath, organizationBillingPath } from "~/utils/pathBuilder";
import { singleton } from "~/utils/singleton";
import { RedisCacheStore } from "./unkey/redisCacheStore.server";
import { $replica } from "~/db.server";
import { metrics } from "@opentelemetry/api";

function initializeClient() {
if (isCloud() && process.env.BILLING_API_URL && process.env.BILLING_API_KEY) {
Expand All @@ -43,6 +44,23 @@ function initializeClient() {
}

const client = singleton("billingClient", initializeClient);
// Failures from @trigger.dev/platform billing client calls are tracked via
// this metric (with low-cardinality {function, kind} labels) rather than
// logged. Every task invocation hits these paths, so per-call logs were too
// noisy; dashboard the counter for visibility instead.
const platformClientMeter = metrics.getMeter("trigger.dev/platform-client");
const platformClientFailuresCounter = platformClientMeter.createCounter(
"platform_client.failures_total",
{
description:
"Failures returned or thrown by @trigger.dev/platform billing client calls",
}
);

function recordPlatformFailure(fn: string, kind: "caught" | "no_success") {
platformClientFailuresCounter.add(1, { function: fn, kind });
}
Comment thread
ericallam marked this conversation as resolved.
Comment thread
nicktrn marked this conversation as resolved.


function initializePlatformCache() {
const ctx = new DefaultStatefulContext();
Expand Down Expand Up @@ -206,7 +224,7 @@ export async function getCurrentPlan(orgId: string) {
firstDayOfNextMonth.setUTCHours(0, 0, 0, 0);

if (!result.success) {
logger.error("Error getting current plan - no success", { orgId, error: result.error });
recordPlatformFailure("getCurrentPlan", "no_success");
return undefined;
}

Expand All @@ -222,7 +240,7 @@ export async function getCurrentPlan(orgId: string) {

return { ...result, usage };
} catch (e) {
logger.error("Error getting current plan - caught error", { orgId, error: e });
recordPlatformFailure("getCurrentPlan", "caught");
return undefined;
}
}
Expand All @@ -233,13 +251,13 @@ export async function getLimits(orgId: string) {
try {
const result = await client.currentPlan(orgId);
if (!result.success) {
logger.error("Error getting limits - no success", { orgId, error: result.error });
recordPlatformFailure("getLimits", "no_success");
return undefined;
}

return result.v3Subscription?.plan?.limits;
} catch (e) {
logger.error("Error getting limits - caught error", { orgId, error: e });
recordPlatformFailure("getLimits", "caught");
return undefined;
}
}
Expand Down Expand Up @@ -315,7 +333,7 @@ export async function customerPortalUrl(orgId: string, orgSlug: string) {
returnUrl: `${env.APP_ORIGIN}${organizationBillingPath({ slug: orgSlug })}`,
});
} catch (e) {
logger.error("Error getting customer portal Url", { orgId, error: e });
recordPlatformFailure("customerPortalUrl", "caught");
return undefined;
}
}
Expand All @@ -326,12 +344,12 @@ export async function getPlans() {
try {
const result = await client.plans();
if (!result.success) {
logger.error("Error getting plans - no success", { error: result.error });
recordPlatformFailure("getPlans", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error getting plans - caught error", { error: e });
recordPlatformFailure("getPlans", "caught");
return undefined;
}
}
Expand Down Expand Up @@ -408,12 +426,12 @@ export async function setConcurrencyAddOn(organizationId: string, amount: number
try {
const result = await client.setAddOn(organizationId, { type: "concurrency", amount });
if (!result.success) {
logger.error("Error setting concurrency add on - no success", { error: result.error });
recordPlatformFailure("setConcurrencyAddOn", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error setting concurrency add on - caught error", { error: e });
recordPlatformFailure("setConcurrencyAddOn", "caught");
return undefined;
}
}
Expand All @@ -424,12 +442,12 @@ export async function setSeatsAddOn(organizationId: string, amount: number) {
try {
const result = await client.setAddOn(organizationId, { type: "seats", amount });
if (!result.success) {
logger.error("Error setting seats add on - no success", { error: result.error });
recordPlatformFailure("setSeatsAddOn", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error setting seats add on - caught error", { error: e });
recordPlatformFailure("setSeatsAddOn", "caught");
return undefined;
}
}
Expand All @@ -440,12 +458,12 @@ export async function setBranchesAddOn(organizationId: string, amount: number) {
try {
const result = await client.setAddOn(organizationId, { type: "branches", amount });
if (!result.success) {
logger.error("Error setting branches add on - no success", { error: result.error });
recordPlatformFailure("setBranchesAddOn", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error setting branches add on - caught error", { error: e });
recordPlatformFailure("setBranchesAddOn", "caught");
return undefined;
}
}
Expand All @@ -456,12 +474,12 @@ export async function getUsage(organizationId: string, { from, to }: { from: Dat
try {
const result = await client.usage(organizationId, { from, to });
if (!result.success) {
logger.error("Error getting usage - no success", { error: result.error });
recordPlatformFailure("getUsage", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error getting usage - caught error", { error: e });
recordPlatformFailure("getUsage", "caught");
return undefined;
}
}
Expand Down Expand Up @@ -490,12 +508,12 @@ export async function getUsageSeries(organizationId: string, params: UsageSeries
try {
const result = await client.usageSeries(organizationId, params);
if (!result.success) {
logger.error("Error getting usage series - no success", { error: result.error });
recordPlatformFailure("getUsageSeries", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error getting usage series - caught error", { error: e });
recordPlatformFailure("getUsageSeries", "caught");
return undefined;
}
}
Expand All @@ -514,12 +532,12 @@ export async function reportInvocationUsage(
additionalData,
});
if (!result.success) {
logger.error("Error reporting invocation - no success", { error: result.error });
recordPlatformFailure("reportInvocationUsage", "no_success");
return undefined;
}
return result;
} catch (e) {
logger.error("Error reporting invocation - caught error", { error: e });
recordPlatformFailure("reportInvocationUsage", "caught");
return undefined;
}
}
Expand Down Expand Up @@ -550,12 +568,12 @@ export async function getEntitlement(
try {
const response = await client.getEntitlement(organizationId);
if (!response.success) {
logger.error("Error getting entitlement - no success", { error: response.error });
recordPlatformFailure("getEntitlement", "no_success");
return undefined;
}
return response;
} catch (e) {
logger.error("Error getting entitlement - caught error", { error: e });
recordPlatformFailure("getEntitlement", "caught");
return undefined;
}
});
Expand Down Expand Up @@ -602,7 +620,7 @@ export async function getBillingAlerts(
if (!client) return undefined;
const result = await client.getBillingAlerts(organizationId);
if (!result.success) {
logger.error("Error getting billing alert", { error: result.error, organizationId });
recordPlatformFailure("getBillingAlert", "no_success");
throw new Error("Error getting billing alert");
}
return result;
Expand All @@ -615,7 +633,7 @@ export async function setBillingAlert(
if (!client) return undefined;
const result = await client.updateBillingAlerts(organizationId, alert);
if (!result.success) {
logger.error("Error setting billing alert", { error: result.error, organizationId });
recordPlatformFailure("setBillingAlert", "no_success");
throw new Error("Error setting billing alert");
}
return result;
Expand All @@ -628,11 +646,7 @@ export async function generateRegistryCredentials(
if (!client) return undefined;
const result = await client.generateRegistryCredentials(projectId, region);
if (!result.success) {
logger.error("Error generating registry credentials", {
error: result.error,
projectId,
region,
});
recordPlatformFailure("generateRegistryCredentials", "no_success");
throw new Error("Failed to generate registry credentials");
}

Expand All @@ -651,13 +665,7 @@ export async function enqueueBuild(
if (!client) return undefined;
const result = await client.enqueueBuild(projectId, { deploymentId, artifactKey, options });
if (!result.success) {
logger.error("Error enqueuing build", {
error: result.error,
projectId,
deploymentId,
artifactKey,
options,
});
recordPlatformFailure("enqueueBuild", "no_success");
throw new Error("Failed to enqueue build");
}

Expand All @@ -672,12 +680,12 @@ export async function getPrivateLinks(
const [error, result] = await tryCatch(client.getPrivateLinks(organizationId));

if (error) {
logger.error("Error getting private links", { organizationId, error });
recordPlatformFailure("getPrivateLinks", "caught");
return undefined;
}

if (!result.success) {
logger.error("Error getting private links - no success", { organizationId, error: result.error });
recordPlatformFailure("getPrivateLinks", "no_success");
return undefined;
}

Expand All @@ -693,12 +701,12 @@ export async function createPrivateLink(
const [error, result] = await tryCatch(client.createPrivateLink(organizationId, body));

if (error) {
logger.error("Error creating private link", { organizationId, error });
recordPlatformFailure("createPrivateLink", "caught");
throw error;
}

if (!result.success) {
logger.error("Error creating private link - no success", { organizationId, error: result.error });
recordPlatformFailure("createPrivateLink", "no_success");
throw new Error(result.error ?? "Failed to create private link");
}

Expand All @@ -714,12 +722,12 @@ export async function deletePrivateLink(
const [error, result] = await tryCatch(client.deletePrivateLink(organizationId, connectionId));

if (error) {
logger.error("Error deleting private link", { organizationId, connectionId, error });
recordPlatformFailure("deletePrivateLink", "caught");
throw error;
}

if (!result.success) {
logger.error("Error deleting private link - no success", { organizationId, connectionId, error: result.error });
recordPlatformFailure("deletePrivateLink", "no_success");
throw new Error(result.error ?? "Failed to delete private link");
}
}
Expand All @@ -732,12 +740,12 @@ export async function getPrivateLinkRegions(
const [error, result] = await tryCatch(client.getPrivateLinkRegions(organizationId));

if (error) {
logger.error("Error getting private link regions", { organizationId, error });
recordPlatformFailure("getPrivateLinkRegions", "caught");
return undefined;
}

if (!result.success) {
logger.error("Error getting private link regions - no success", { organizationId, error: result.error });
recordPlatformFailure("getPrivateLinkRegions", "no_success");
return undefined;
}

Expand Down
Loading
Loading