This document describes the Sentry error tracking implementation for the Snapshots Service.
We've implemented comprehensive error tracking using Sentry, which provides:
- Automatic error capture and reporting
- Performance monitoring
- User context tracking
- Session replay
- Custom error handling utilities
npm install @sentry/nextjs- sentry.client.config.ts: Client-side configuration
- sentry.server.config.ts: Server-side configuration
- sentry.edge.config.ts: Edge runtime configuration
- next.config.ts: Wrapped with Sentry webpack plugin
- .sentryclirc: Sentry CLI configuration
Required environment variables:
# Public DSN for client-side
NEXT_PUBLIC_SENTRY_DSN=https://[email protected]/PROJECT_ID
# Build-time variables
SENTRY_ORG=your-org
SENTRY_PROJECT=your-project
SENTRY_AUTH_TOKEN=your-auth-tokenAll unhandled errors are automatically captured:
- Client-side JavaScript errors
- Server-side Node.js errors
- API route errors
- Edge runtime errors
- Traces sample rate: 10% in production, 100% in development
- Automatic instrumentation for:
- API routes
- Database queries (Prisma)
- Page loads
- Navigation
Records user sessions when errors occur:
- 100% replay on errors
- 10% sample rate for all sessions
- Masks sensitive text
- Blocks media content
Automatically tracks user information:
- User ID, email, and tier
- Set on login, cleared on logout
- Component:
SentryUserContext
Utility functions in lib/sentry.ts:
// Capture exception with context
captureException(error, {
user: { id: userId },
request: { url, method }
});
// Track user actions
trackUserAction('download_snapshot', 'user_interaction', {
chainId: 'noble-1',
size: '1.5GB'
});
// Monitor async operations
await monitorAsync(
async () => await fetchData(),
'fetch_snapshots'
);
// Wrap API handlers
export const GET = withSentry(handler);- Global Error Boundary:
app/global-error.tsx - Page Error Boundary:
app/error.tsx - Component Error Boundary:
components/error/ErrorBoundary.tsx
import { withSentry, captureApiError } from '@/lib/sentry';
async function handler(req: NextRequest) {
try {
// Your API logic
} catch (error) {
captureApiError(error, {
method: req.method,
url: req.url,
});
throw error;
}
}
export const GET = withSentry(handler);import { ErrorBoundary } from '@/components/error/ErrorBoundary';
export function MyComponent() {
return (
<ErrorBoundary>
<YourComponent />
</ErrorBoundary>
);
}import { captureException, captureMessage } from '@/lib/sentry';
// Capture an error
try {
await riskyOperation();
} catch (error) {
captureException(error, {
operation: 'snapshot_download',
chainId,
});
}
// Log a message
captureMessage('Unusual condition detected', {
condition: 'high_memory_usage',
value: memoryUsage,
}, 'warning');import { startTransaction } from '@/lib/sentry';
const transaction = startTransaction('process_snapshot', 'task');
try {
await processSnapshot();
transaction.setStatus('ok');
} catch (error) {
transaction.setStatus('internal_error');
throw error;
} finally {
transaction.finish();
}The following errors are filtered out to reduce noise:
- Browser extension errors
- ResizeObserver loop errors
- Network errors
- Next.js navigation errors (NEXT_NOT_FOUND, NEXT_REDIRECT)
Test endpoint available at /api/test-error:
# Test error capture
curl http://localhost:3000/api/test-error?type=error
# Test warning
curl http://localhost:3000/api/test-error?type=warning
# Test custom context
curl http://localhost:3000/api/test-error?type=custom- Issues: View all errors grouped by similarity
- Performance: Monitor transaction times
- Releases: Track errors by deployment
- User Feedback: See user-submitted error reports
Configure alerts for:
- Error rate spikes
- New error types
- Performance degradation
- Specific error conditions
- Add Context: Always include relevant context when capturing errors
- Use Breadcrumbs: Track user actions leading to errors
- Set User Context: Ensure user info is set for better debugging
- Filter Noise: Configure ignoreErrors for known issues
- Monitor Performance: Use transactions for critical operations
-
Sensitive Data:
- Session replay masks all text by default
- Don't log sensitive information in error context
- Review what data is sent to Sentry
-
Source Maps:
- Hidden from production bundles
- Only uploaded to Sentry during build
-
Tunneling:
- Configured to route through
/monitoringto bypass ad blockers - Increases server load but improves data collection
- Configured to route through
-
Errors not appearing in Sentry:
- Check NEXT_PUBLIC_SENTRY_DSN is set
- Verify error isn't filtered by ignoreErrors
- Check browser console for Sentry initialization
-
Source maps not working:
- Ensure SENTRY_AUTH_TOKEN is set during build
- Check SENTRY_ORG and SENTRY_PROJECT match
-
Performance issues:
- Reduce tracesSampleRate in production
- Disable session replay if not needed
- Custom Dashboards: Build error dashboards in the app
- Slack Integration: Send critical errors to Slack
- Error Analytics: Track error trends over time
- User Feedback: Allow users to submit error reports
- Proactive Monitoring: Alert before errors impact users