|
| 1 | +# Sentry Integration for Sable |
| 2 | + |
| 3 | +This document describes the Sentry error tracking and monitoring integration added to Sable. |
| 4 | +For a detailed breakdown of what data is collected and how it is protected, see [SENTRY_PRIVACY.md](./SENTRY_PRIVACY.md). |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +Sentry is integrated with Sable to provide: |
| 9 | + |
| 10 | +- **Error tracking**: Automatic capture and reporting of errors and exceptions |
| 11 | +- **Performance monitoring**: Track application performance and identify bottlenecks |
| 12 | +- **User feedback**: Collect bug reports with context from users |
| 13 | +- **Session replay**: Record user sessions (with privacy controls) for debugging |
| 14 | +- **Breadcrumbs**: Track user actions leading up to errors |
| 15 | +- **Debug log integration**: Attach internal debug logs to error reports |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +### 1. Automatic Error Tracking |
| 20 | + |
| 21 | +All errors are automatically captured and sent to Sentry with: |
| 22 | + |
| 23 | +- Stack traces |
| 24 | +- User context (anonymized) |
| 25 | +- Device and browser information |
| 26 | +- Recent breadcrumbs (user actions) |
| 27 | +- Debug logs (when enabled) |
| 28 | + |
| 29 | +### 2. Debug Logger Integration |
| 30 | + |
| 31 | +The internal debug logger now integrates with Sentry: |
| 32 | + |
| 33 | +- **Breadcrumbs**: All debug logs are added as breadcrumbs for context |
| 34 | +- **Error capture**: Errors logged to the debug logger are automatically sent to Sentry |
| 35 | +- **Warning sampling**: 10% of warnings are sent to Sentry to avoid overwhelming the system |
| 36 | +- **Log attachment**: Recent logs can be attached to bug reports for additional context |
| 37 | + |
| 38 | +Key integration points: |
| 39 | + |
| 40 | +- `src/app/utils/debugLogger.ts` - Enhanced with Sentry breadcrumb and error capture |
| 41 | +- Automatic breadcrumb creation for all log entries |
| 42 | +- Error objects in log data are captured as exceptions |
| 43 | +- 10% sampling rate for warnings to control volume |
| 44 | + |
| 45 | +### 3. Bug Report Modal Integration |
| 46 | + |
| 47 | +The bug report modal (`/bugreport` command or "Bug Report" button) now includes: |
| 48 | + |
| 49 | +- **Optional Sentry reporting**: Checkbox to send anonymous reports to Sentry |
| 50 | +- **Debug log attachment**: Option to include recent debug logs (last 100 entries) |
| 51 | +- **User feedback API**: Bug reports are sent as Sentry user feedback for better visibility |
| 52 | +- **Privacy controls**: Users can opt-out of Sentry reporting |
| 53 | + |
| 54 | +Integration points: |
| 55 | + |
| 56 | +- `src/app/features/bug-report/BugReportModal.tsx` - Added Sentry options and submission logic |
| 57 | +- Automatically attaches platform info, version, and user agent |
| 58 | +- Links bug reports to Sentry events for tracking |
| 59 | + |
| 60 | +### 4. Privacy & Security |
| 61 | + |
| 62 | +Comprehensive data scrubbing (full details in [SENTRY_PRIVACY.md](./SENTRY_PRIVACY.md)): |
| 63 | + |
| 64 | +- **Token masking**: All access tokens, passwords, and authentication data are redacted |
| 65 | +- **Matrix ID anonymization**: User IDs, room IDs, and event IDs are masked |
| 66 | +- **Session replay privacy**: All text, media, and form inputs are masked when replay is enabled |
| 67 | +- **request header sanitization**: Authorization headers are removed |
| 68 | +- **User opt-out**: Users can disable Sentry entirely via settings |
| 69 | + |
| 70 | +Sensitive patterns automatically redacted: |
| 71 | + |
| 72 | +- `access_token`, `password`, `token`, `refresh_token` |
| 73 | +- `session_id`, `sync_token`, `next_batch` |
| 74 | +- Matrix user IDs (`@user:server`) |
| 75 | +- Matrix room IDs (`!room:server`) |
| 76 | +- Matrix event IDs (`$event_id`) |
| 77 | + |
| 78 | +### 5. Settings UI |
| 79 | + |
| 80 | +New Sentry settings panel in Developer Tools: |
| 81 | + |
| 82 | +- **Enable/disable Sentry**: Toggle error tracking on/off |
| 83 | +- **Session replay control**: Enable/disable session recording |
| 84 | +- **Test error reporting**: Send test errors to verify configuration |
| 85 | +- **Test feedback**: Send test feedback messages |
| 86 | +- **Attach debug logs**: Manually attach recent logs to next error |
| 87 | + |
| 88 | +Access via: Settings → Developer Tools → Error Tracking (Sentry) |
| 89 | + |
| 90 | +## Configuration |
| 91 | + |
| 92 | +### Environment Variables |
| 93 | + |
| 94 | +Configure Sentry via environment variables: |
| 95 | + |
| 96 | +```env |
| 97 | +# Required: Your Sentry DSN (if not set, Sentry is disabled) |
| 98 | +VITE_SENTRY_DSN=https://your-sentry-dsn@sentry.io/project-id |
| 99 | +
|
| 100 | +# Required: Environment name - controls sampling rates |
| 101 | +# - "production" = 10% trace/replay sampling (cost-effective for production) |
| 102 | +# - "preview" = 100% trace/replay sampling (full debugging for PR previews) |
| 103 | +# - "development" = 100% trace/replay sampling (full debugging for local dev) |
| 104 | +VITE_SENTRY_ENVIRONMENT=production |
| 105 | +
|
| 106 | +# Optional: Release version for tracking (defaults to VITE_APP_VERSION) |
| 107 | +VITE_SENTRY_RELEASE=1.7.0 |
| 108 | +
|
| 109 | +# Optional: For uploading source maps to Sentry (CI/CD only) |
| 110 | +SENTRY_AUTH_TOKEN=your-sentry-auth-token |
| 111 | +SENTRY_ORG=your-org-slug |
| 112 | +SENTRY_PROJECT=your-project-slug |
| 113 | +``` |
| 114 | + |
| 115 | +### Deployment Configuration |
| 116 | + |
| 117 | +**Production deployment (from `dev` branch):** |
| 118 | + |
| 119 | +- Set `VITE_SENTRY_ENVIRONMENT=production` |
| 120 | +- Gets 10% sampling for traces and session replay |
| 121 | +- Cost-effective for production usage |
| 122 | +- Configured in `.github/workflows/cloudflare-web-deploy.yml` |
| 123 | + |
| 124 | +**Preview deployments (PR previews, Cloudflare Pages):** |
| 125 | + |
| 126 | +- Set `VITE_SENTRY_ENVIRONMENT=preview` |
| 127 | +- Gets 100% sampling for traces and session replay |
| 128 | +- Full debugging capabilities for testing |
| 129 | +- Configured in `.github/workflows/cloudflare-web-preview.yml` |
| 130 | + |
| 131 | +**Local development:** |
| 132 | + |
| 133 | +- `VITE_SENTRY_ENVIRONMENT` not set (defaults to `development` via Vite MODE) |
| 134 | +- Gets 100% sampling for traces and session replay |
| 135 | +- Full debugging capabilities |
| 136 | + |
| 137 | +**Sampling rates by environment:** |
| 138 | + |
| 139 | +``` |
| 140 | +Environment | Traces | Session Replay | Error Replay |
| 141 | +---------------|--------|----------------|------------- |
| 142 | +production | 10% | 10% | 100% |
| 143 | +preview | 100% | 100% | 100% |
| 144 | +development | 100% | 100% | 100% |
| 145 | +``` |
| 146 | + |
| 147 | +### User Preferences |
| 148 | + |
| 149 | +Users can control Sentry via localStorage: |
| 150 | + |
| 151 | +```javascript |
| 152 | +// Disable Sentry entirely (requires page refresh) |
| 153 | +localStorage.setItem('sable_sentry_enabled', 'false'); |
| 154 | + |
| 155 | +// Disable session replay only (requires page refresh) |
| 156 | +localStorage.setItem('sable_sentry_replay_enabled', 'false'); |
| 157 | +``` |
| 158 | + |
| 159 | +Or use the UI in Settings → Developer Tools → Error Tracking (Sentry). |
| 160 | + |
| 161 | +## Implementation Details |
| 162 | + |
| 163 | +### Files Modified |
| 164 | + |
| 165 | +1. **`src/instrument.ts`** |
| 166 | + - Enhanced Sentry initialization with privacy controls |
| 167 | + - Added user preference checks |
| 168 | + - Improved data scrubbing for Matrix-specific data |
| 169 | + - Conditional session replay based on user settings |
| 170 | + |
| 171 | +2. **`src/app/utils/debugLogger.ts`** |
| 172 | + - Added Sentry import |
| 173 | + - New `sendToSentry()` method for breadcrumbs and error capture |
| 174 | + - New `exportLogsForSentry()` method |
| 175 | + - New `attachLogsToSentry()` method |
| 176 | + - Integrated into main `log()` method |
| 177 | + |
| 178 | +3. **`src/app/features/bug-report/BugReportModal.tsx`** |
| 179 | + - Added Sentry and debug logger imports |
| 180 | + - New state for Sentry options (`sendToSentry`, `includeDebugLogs`) |
| 181 | + - Enhanced `handleSubmit()` with Sentry user feedback |
| 182 | + - New UI checkboxes for Sentry options |
| 183 | + |
| 184 | +4. **`src/app/features/settings/developer-tools/SentrySettings.tsx`** _(new file)_ |
| 185 | + - New settings panel component |
| 186 | + - Controls for Sentry and session replay |
| 187 | + - Manual log attachment |
| 188 | + |
| 189 | +5. **`src/app/features/settings/developer-tools/DevelopTools.tsx`** |
| 190 | + - Added SentrySettings import and component |
| 191 | + |
| 192 | +### Sentry Configuration |
| 193 | + |
| 194 | +- **Tracing sample rate**: 100% in development, 10% in production |
| 195 | +- **Session replay sample rate**: 10% of all sessions, 100% of error sessions |
| 196 | +- **Warning capture rate**: 10% to avoid overwhelming Sentry |
| 197 | +- **Breadcrumb retention**: All breadcrumbs retained for context |
| 198 | +- **Log attachment limit**: Last 100 debug log entries |
| 199 | + |
| 200 | +### Performance Considerations |
| 201 | + |
| 202 | +- Breadcrumbs are added synchronously but are low-overhead |
| 203 | +- Error capture is asynchronous and non-blocking |
| 204 | +- Warning sampling (10%) prevents excessive Sentry usage |
| 205 | +- Session replay only captures when enabled by user |
| 206 | +- Debug log attachment limited to most recent entries |
| 207 | + |
| 208 | +## Usage Examples |
| 209 | + |
| 210 | +### For Developers |
| 211 | + |
| 212 | +```typescript |
| 213 | +import { getDebugLogger } from '$utils/debugLogger'; |
| 214 | + |
| 215 | +// Errors are automatically sent to Sentry |
| 216 | +const logger = createDebugLogger('myNamespace'); |
| 217 | +logger.error('sync', 'Sync failed', error); // Sent to Sentry |
| 218 | + |
| 219 | +// Manually attach logs before capturing an error |
| 220 | +const debugLogger = getDebugLogger(); |
| 221 | +debugLogger.attachLogsToSentry(100); |
| 222 | +Sentry.captureException(error); |
| 223 | +``` |
| 224 | + |
| 225 | +### For Users |
| 226 | + |
| 227 | +1. **Report a bug with Sentry**: |
| 228 | + - Type `/bugreport` or click "Bug Report" button |
| 229 | + - Fill in the form |
| 230 | + - Check "Send anonymous report to Sentry" |
| 231 | + - Check "Include recent debug logs" for more context |
| 232 | + - Submit |
| 233 | + |
| 234 | +2. **Disable Sentry**: |
| 235 | + - Go to Settings → Developer Tools |
| 236 | + - Enable Developer Tools |
| 237 | + - Scroll to "Error Tracking (Sentry)" |
| 238 | + - Toggle off "Enable Sentry Error Tracking" |
| 239 | + - Refresh the page |
| 240 | + |
| 241 | +## Benefits |
| 242 | + |
| 243 | +### For Users |
| 244 | + |
| 245 | +- Better bug tracking and faster fixes |
| 246 | +- Optional participation with privacy controls |
| 247 | +- Transparent data usage |
| 248 | + |
| 249 | +### For Developers |
| 250 | + |
| 251 | +- Real-time error notifications |
| 252 | +- Rich context with breadcrumbs and logs |
| 253 | +- Performance monitoring |
| 254 | +- User feedback integrated with errors |
| 255 | +- Replay sessions to reproduce bugs |
| 256 | + |
| 257 | +## Privacy Commitment |
| 258 | + |
| 259 | +See [SENTRY_PRIVACY.md](./SENTRY_PRIVACY.md) for a complete, code-linked breakdown of what is collected, what is masked, and how user controls work. |
| 260 | + |
| 261 | +In summary, all data sent to Sentry is: |
| 262 | + |
| 263 | +- **Opt-in by default** but can be disabled |
| 264 | +- **Anonymized**: No personal data or message content |
| 265 | +- **Filtered**: Tokens, passwords, and IDs are redacted |
| 266 | +- **Minimal**: Only error context and debug info |
| 267 | +- **Transparent**: Users can see what's being sent |
| 268 | + |
| 269 | +No message content, room conversations, or personal information is ever sent to Sentry. |
| 270 | + |
| 271 | +## Future Enhancements |
| 272 | + |
| 273 | +Potential improvements: |
| 274 | + |
| 275 | +- [ ] Add performance metrics to Sentry settings |
| 276 | +- [ ] More granular control over breadcrumb categories |
| 277 | +- [ ] Export Sentry reports as JSON for offline analysis |
| 278 | +- [ ] Integration with internal metrics dashboard |
| 279 | +- [ ] Automatic source map upload for better stack traces |
| 280 | +- [ ] Custom Sentry tags from user preferences |
| 281 | +- [ ] Rate limiting per user/session |
| 282 | + |
| 283 | +## Testing |
| 284 | + |
| 285 | +To test the integration: |
| 286 | + |
| 287 | +1. **Test error reporting**: |
| 288 | + - Go to Settings → Developer Tools → Error Tracking |
| 289 | + - Click "Send Test Error" |
| 290 | + - Check Sentry dashboard for the error |
| 291 | + |
| 292 | +2. **Test feedback**: |
| 293 | + - Click "Send Test Feedback" |
| 294 | + - Check Sentry feedback section |
| 295 | + |
| 296 | +3. **Test bug report integration**: |
| 297 | + - Type `/bugreport` |
| 298 | + - Fill in form with test data |
| 299 | + - Enable "Send anonymous report to Sentry" |
| 300 | + - Submit and check Sentry |
| 301 | + |
| 302 | +4. **Test privacy controls**: |
| 303 | + - Disable Sentry in settings |
| 304 | + - Refresh page |
| 305 | + - Trigger an error (should not appear in Sentry) |
| 306 | + - Re-enable and verify errors are captured again |
| 307 | + |
| 308 | +## Troubleshooting |
| 309 | + |
| 310 | +### Sentry not capturing errors |
| 311 | + |
| 312 | +1. Check that `VITE_SENTRY_DSN` is set |
| 313 | +2. Check that Sentry is enabled in settings |
| 314 | +3. Check browser console for Sentry initialization message |
| 315 | +4. Verify network requests to Sentry are not blocked |
| 316 | + |
| 317 | +### Sensitive data in reports |
| 318 | + |
| 319 | +1. Check `beforeSend` hook in `instrument.ts` |
| 320 | +2. Add new patterns to the scrubbing regex |
| 321 | +3. Test with actual data to verify masking |
| 322 | + |
| 323 | +### Performance impact |
| 324 | + |
| 325 | +1. Reduce tracing sample rate in production |
| 326 | +2. Disable session replay if not needed |
| 327 | +3. Monitor Sentry quota usage |
| 328 | +4. Adjust warning sampling rate |
| 329 | + |
| 330 | +## Resources |
| 331 | + |
| 332 | +- [Sentry React Documentation](https://docs.sentry.io/platforms/javascript/guides/react/) |
| 333 | +- [Sentry Error Monitoring Best Practices](https://docs.sentry.io/product/error-monitoring/) |
| 334 | +- [Sentry Session Replay](https://docs.sentry.io/product/session-replay/) |
| 335 | +- [Sentry User Feedback](https://docs.sentry.io/product/user-feedback/) |
0 commit comments