Guides / Monitoring
Your app is fine. Stripe is having an incident. Your users are getting payment errors. You find out 20 minutes later via Twitter. Logging external API calls gives you the signal earlier.
// lib/api-monitor.ts
import { logit } from "@/lib/logit";
export async function monitoredCall<T>(
vendor: string,
operation: string,
fn: () => Promise<T>
): Promise<T> {
const start = Date.now();
try {
const result = await fn();
const duration = Date.now() - start;
if (duration > 2000) {
await logit.now("vendor-health", {
event: "Slow vendor API call",
description: `${vendor} / ${operation} took ${duration}ms`,
icon: "๐ข",
notify: false,
tags: { vendor, operation, severity: "slow" },
metadata: { vendor, operation, duration },
}).catch(console.error);
}
return result;
} catch (err) {
const duration = Date.now() - start;
await logit.now("vendor-health", {
event: "Vendor API error",
description: `${vendor} / ${operation} failed โ ${err instanceof Error ? err.message : String(err)}`,
icon: "๐ด",
notify: false,
tags: { vendor, operation, severity: "error" },
metadata: { vendor, operation, duration, error: err instanceof Error ? err.message : String(err) },
}).catch(console.error);
throw err;
}
}
const session = await monitoredCall("stripe", "checkout.session.create", () =>
stripe.checkout.sessions.create({ ... })
);
const completion = await monitoredCall("openai", "chat.completions.create", () =>
openai.chat.completions.create({ ... })
);
await monitoredCall("sendgrid", "mail.send", () =>
sgMail.send({ ... })
);
| Field | Value |
|---|---|
| Name | Vendor API errors |
| Channel | vendor-health |
| Event name | Vendor API error |
| Threshold | 5 |
| Window | 5 minutes |
| Cooldown | 15 minutes |
| Action | Push notification |
vendor as a tag so you can filter the dashboard to only Stripe errors during a Stripe incident.catch(console.error) on LogIt calls inside the monitor so logging never blocks production calls.Try LogIt free
7-day trial. No credit card required.