Integração Sentry
Diretrizes para usar o Sentry para monitoramento de erros e rastreamento de performance.
Captura de Exceções
Use Sentry.captureException(error) em blocos try/catch:
try {
await operacaoArriscada();
} catch (error) {
Sentry.captureException(error);
throw error;
}
Rastreamento de Performance
Crie spans para ações significativas como cliques em botões, chamadas de API e chamadas de função.
Ações de UI
function handleClick() {
Sentry.startSpan(
{ op: "ui.click", name: "Enviar Formulário" },
(span) => {
span.setAttribute("formId", formId);
enviarFormulario();
}
);
}
Chamadas de API
async function fetchData(id) {
return Sentry.startSpan(
{ op: "http.client", name: `GET /api/items/${id}` },
async () => {
const response = await fetch(`/api/items/${id}`);
return response.json();
}
);
}
Configuração (Next.js)
Arquivos de inicialização do Sentry:
sentry.client.config.ts- Lado do clientesentry.server.config.ts- Lado do servidorsentry.edge.config.ts- Runtime Edge
Importe com import * as Sentry from "@sentry/nextjs" - não é necessário inicializar em outros arquivos.
Configuração Básica
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
enableLogs: true,
});
Com Logging no Console
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
integrations: [
Sentry.consoleLoggingIntegration({ levels: ["log", "warn", "error"] }),
],
});
Logs Estruturados
Use logger.fmt para templates literais com variáveis:
const { logger } = Sentry;
logger.trace("Iniciando conexão", { database: "users" });
logger.debug(logger.fmt`Cache miss para: ${userId}`);
logger.info("Perfil atualizado", { profileId: 345 });
logger.warn("Limite de taxa atingido", { endpoint: "/api/data" });
logger.error("Pagamento falhou", { orderId: "order_123" });
logger.fatal("Pool de conexões esgotado", { activeConnections: 100 });