mcpField
S
sentry-best-practices

sentry-best-practices

github.com/andrelandgrafDevelopment2398 installsUpdated 20w ago
Repository

About sentry-best-practices

sentry-best-practices is a development skill for instrumenting applications with Sentry. It covers exception capture, user and event context, performance spans, breadcrumbs, and structured logs, with examples using the Sentry Next.js SDK.

Key features

  1. 01Capture handled exceptions
  2. 02Attach user, tag, and extra context
  3. 03Trace operations with spans
  4. 04Add custom breadcrumbs
  5. 05Write structured logs

Use cases

  1. 01Track handled errors with relevant context
  2. 02Instrument API requests and UI actions
  3. 03Record custom events and structured logs

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/andrelandgraf/fullstackrecipes --skill sentry-best-practices

After installing, type/sentry-best-practicesin Claude Code to confirm it loaded.

Details

From the project README

Sentry Best Practices

Capture exceptions, add context, trace performance, and log with Sentry.

Prerequisites

Complete these setup recipes first:

  • Sentry Setup
Capturing Exceptions

Capture errors that are handled but still worth tracking.

import * as Sentry from "@sentry/nextjs";

try {
  await riskyOperation();
} catch (err) {
  Sentry.captureException(err);
}
Adding Context

setUser persists for the session. Attach tags (indexed, filterable) and extra (arbitrary detail) per exception.

import * as Sentry from "@sentry/nextjs";

Sentry.setUser({ id: session.user.id, email: session.user.email });

Sentry.captureException(err, {
  tags: { feature: "checkout", plan: "pro" },
  extra: { orderId: "order_123", items: cart.items },
});

Clear the user on sign out.

Sentry.setUser(null);
Performance Tracing

Wrap meaningful operations in startSpan. The sync form receives the span for attributes.

import * as Sentry from "@sentry/nextjs";

const users = await Sentry.startSpan(
  { op: "http.client", name: "GET /api/users" },
  async () => (await fetch("/api/users")).json(),
);

Sentry.startSpan({ op: "ui.click", name: "Submit Button Click" }, (span) => {
  span.setAttribute("form", "checkout");
  processSubmit();
});
Breadcrumbs

Console logs, fetches, and UI clicks are captured automatically. Add manual breadcrumbs for custom events.

import * as Sentry from "@sentry/nextjs";

Sentry.addBreadcrumb({
  category: "auth",
  message: "User signed in",
  level: "info",
});
Sentry Logger

Structured logs surface in the Logs tab.

import * as Sentry from "@sentry/nextjs";

const { logger } = Sentry;

logger.info("Payment processed", { orderId: "123", amount: 99.99 });
logger.warn("Rate limit approaching", { current: 90, max: 100 });
logger.error("Payment failed", { orderId: "123", reason: "declined" });

References

Comments

Markdown supported