This document provides an overview of the Sentry.io integration in the Lifehacking App for error tracking, performance monitoring, and logging.
Sentry is configured to provide:
- Error Tracking: Automatic capture of unhandled exceptions
- Performance Monitoring: Transaction tracing for page loads and API calls
- Logging: Console log integration for all log levels
- Session Replay: Visual reproduction of user sessions (client-side only)
-
sentry.client.config.ts - Client-side initialization
- Enables session replay
- Configures console logging integration
- Sets up error and performance monitoring
-
sentry.server.config.ts - Server-side initialization
- Configures server-side error tracking
- Enables console logging integration
-
sentry.edge.config.ts - Edge runtime initialization
- Configures edge function error tracking
- Enables console logging integration
-
instrumentation.ts - Next.js instrumentation hook
- Loads appropriate Sentry configuration based on runtime
-
next.config.ts - Sentry webpack plugin configuration
- Configures source map upload
- Sets up monitoring tunnel route
- Enables automatic instrumentation
Add the following to your .env.local file:
NEXT_PUBLIC_SENTRY_DSN=your_sentry_dsn_hereThe DSN is publicly accessible and safe to expose in client-side code.
All error boundaries automatically capture exceptions:
'use client';
import { useEffect } from 'react';
import * as Sentry from '@sentry/nextjs';
export default function Error({ error }: { error: Error }) {
useEffect(() => {
Sentry.captureException(error);
}, [error]);
// ... error UI
}Use the utility function for manual error capture:
import { captureException } from '@/lib/utils/sentry';
try {
// ... code that might throw
} catch (error) {
captureException(error as Error, {
userId: '123',
action: 'data-fetch',
});
}Wrap API calls with spans to track performance:
import * as Sentry from '@sentry/nextjs';
export async function fetchData(id: string) {
return Sentry.startSpan(
{
op: 'http.client',
name: `GET /api/data/${id}`,
},
async (span) => {
span.setAttribute('data.id', id);
const response = await fetch(`/api/data/${id}`);
const data = await response.json();
span.setAttribute('data.found', true);
return data;
}
);
}Track user interactions with custom spans:
import * as Sentry from '@sentry/nextjs';
const handleButtonClick = async () => {
await Sentry.startSpan(
{
op: 'ui.action',
name: 'Submit Form',
},
async (span) => {
span.setAttribute('form.type', 'category');
// ... form submission logic
span.setAttribute('form.success', true);
}
);
};import { withSpan } from '@/lib/utils/sentry';
const result = await withSpan(
'http.client',
'GET /api/users',
async (span) => {
const users = await fetchUsers();
return users;
},
{ endpoint: '/api/users', method: 'GET' }
);Use Sentry's logger for structured logs:
import { logger } from '@/lib/utils/sentry';
// Basic logging
logger.info('User logged in', { userId: '123' });
logger.warn('Rate limit approaching', { remaining: 10 });
logger.error('Payment failed', { orderId: 'order_123' });
// Template literals with logger.fmt
logger.debug(logger.fmt`Cache miss for user: ${userId}`);All console.log, console.warn, and console.error calls are automatically sent to Sentry as logs.
Always provide context when capturing errors:
captureException(error, {
userId: user.id,
action: 'checkout',
cartValue: cart.total,
});Add meaningful attributes to spans:
span.setAttribute('user.id', userId);
span.setAttribute('request.size', data.length);
span.setAttribute('cache.hit', true);Never log sensitive information:
- Passwords
- API keys
- Credit card numbers
- Personal identification numbers
- Use appropriate sample rates in production
- Limit span creation to meaningful operations
- Avoid creating spans in tight loops
Access your Sentry dashboard at: https://sentry.io/organizations/arielbvergara/
Key sections:
- Issues: View and triage errors
- Performance: Analyze transaction performance
- Releases: Track deployments
- Alerts: Configure notifications
Requests to Sentry are routed through /monitoring to bypass ad-blockers and improve reliability.
- Check that
NEXT_PUBLIC_SENTRY_DSNis set - Verify Sentry is initialized (check browser console)
- Ensure error boundaries are in place
- Check Sentry project settings
- Verify
tracesSampleRateis > 0 - Check that spans are being created
- Ensure instrumentation.ts is loaded
- Verify Sentry auth token is configured
- Check build logs for upload errors
- Ensure
organdprojectare correct in next.config.ts
Adjust in configuration files:
Sentry.init({
tracesSampleRate: 1.0, // 100% in development
replaysSessionSampleRate: 0.1, // 10% of sessions
replaysOnErrorSampleRate: 1.0, // 100% of error sessions
});Sentry automatically detects the environment from NODE_ENV:
development: Full logging and tracingproduction: Optimized for performance