diff --git a/apps/api/src/services/billing/auto_charge.ts b/apps/api/src/services/billing/auto_charge.ts index 60c0107e6..934d537e9 100644 --- a/apps/api/src/services/billing/auto_charge.ts +++ b/apps/api/src/services/billing/auto_charge.ts @@ -10,7 +10,7 @@ import { NotificationType } from "../../types"; import { deleteKey, getValue, setValue } from "../redis"; import { redisRateLimitClient } from "../rate-limiter"; import { sendSlackWebhook } from "../alerts/slack"; -import { logger } from "../../lib/logger"; +import { logger as _logger } from "../../lib/logger"; // Define the number of credits to be added during auto-recharge const AUTO_RECHARGE_CREDITS = 1000; @@ -32,6 +32,13 @@ export async function autoCharge( remainingCredits: number; chunk: AuthCreditUsageChunk; }> { + const logger = _logger.child({ + module: "auto_charge", + method: "autoCharge", + team_id: chunk.team_id, + teamId: chunk.team_id, + }); + const resource = `auto-recharge:${chunk.team_id}`; const cooldownKey = `auto-recharge-cooldown:${chunk.team_id}`; const hourlyCounterKey = `auto-recharge-hourly:${chunk.team_id}`; @@ -52,7 +59,7 @@ export async function autoCharge( if (hourlyCharges >= MAX_CHARGES_PER_HOUR) { logger.warn( - `Auto-recharge for team ${chunk.team_id} exceeded hourly limit of ${MAX_CHARGES_PER_HOUR}`, + `Auto-recharge exceeded hourly limit of ${MAX_CHARGES_PER_HOUR}`, ); return { success: false, @@ -66,7 +73,7 @@ export async function autoCharge( const cooldownValue = await getValue(cooldownKey); if (cooldownValue) { logger.info( - `Auto-recharge for team ${chunk.team_id} is in cooldown period`, + `Auto-recharge is in cooldown period`, ); return { success: false, @@ -95,7 +102,7 @@ export async function autoCharge( const cooldownValue = await getValue(cooldownKey); if (cooldownValue) { logger.info( - `Auto-recharge for team ${chunk.team_id} is in cooldown period`, + `Auto-recharge is in cooldown period`, ); return { success: false, @@ -131,7 +138,7 @@ export async function autoCharge( .single(); if (customersError) { - logger.error(`Error fetching customer data: ${customersError}`); + logger.error(`Error fetching customer data`, { error: customersError }); return { success: false, message: "Error fetching customer data", @@ -197,7 +204,7 @@ export async function autoCharge( ); } } catch (error) { - logger.error(`Error sending frequent auto-recharge notification: ${error}`); + logger.error(`Error sending frequent auto-recharge notification`, { error }); } await sendNotification( @@ -213,6 +220,11 @@ export async function autoCharge( await clearACUC(chunk.api_key); await clearACUCTeam(chunk.team_id); + logger.info(`Auto-recharge successful`, { + credits: AUTO_RECHARGE_CREDITS, + paymentStatus: paymentStatus.return_status, + }); + if (process.env.SLACK_ADMIN_WEBHOOK_URL) { const webhookCooldownKey = `webhook_cooldown_${chunk.team_id}`; const isInCooldown = await getValue(webhookCooldownKey); @@ -280,7 +292,7 @@ export async function autoCharge( }, ); } catch (error) { - logger.error(`Failed to acquire lock for auto-recharge: ${error}`); + logger.error(`Failed to acquire lock for auto-recharge`, { error }); return { success: false, message: "Failed to acquire lock for auto-recharge", diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts index 564cbd4b7..c6f424317 100644 --- a/apps/api/src/services/billing/credit_billing.ts +++ b/apps/api/src/services/billing/credit_billing.ts @@ -132,6 +132,12 @@ export async function supaCheckTeamCredits( chunk.remaining_credits < autoRechargeThreshold && !chunk.is_extract ) { + logger.info("Auto-recharge triggered", { + team_id, + teamId: team_id, + autoRechargeThreshold, + remainingCredits: chunk.remaining_credits, + }); const autoChargeResult = await autoCharge(chunk, autoRechargeThreshold); if (autoChargeResult.success) { return {