097
LVL 04 — SENIOR-IN-TRAININGSESSION 097DAY 97

ERROR TRACKING

🎫 PIXELCRAFT-083
🔧DevOps | 🟡 Medium | Priority: 🟠 High

Errors in production are invisible. Users hit a bug, we don't know. They leave, we don't know why. Integrate Sentry for real-time error tracking on both frontend and backend. Know about bugs before users report them.
CONCEPTS.UNLOCKED
🔍
Error Tracking Services
Sentry, Datadog, LogRocket. Capture errors in production automatically: stack trace, browser info, user context, breadcrumbs (what happened before the error). Debug from your dashboard, not from user reports.
🗺️
Source Maps
Map minified code back to source. Production JS is minified: error at a.js:1:4832 is useless. Source maps translate it to FilterPicker.tsx:42 — readable stack traces from minified bundles.
🧭
Error Context
User info, breadcrumbs, device info. Not just "TypeError: undefined" — but WHO (user 42), WHAT they were doing (applying sepia filter), WHERE (Chrome on Android), and WHEN (2 minutes ago).
🔔
Alerting
Get notified when new errors occur. Slack message: "New error in production: Cannot read property 'width' of null — 23 users affected in the last hour." React before users complain.
📦
Release Tracking
Which deploy introduced which errors. Tag each error with the git commit/release version. "This error started appearing after deploy v1.4.2." Instantly know which change caused the regression.
HANDS-ON.TASKS
01
Install Sentry
npm install @sentry/react @sentry/node
02
Frontend Integration
// main.tsx — initialize FIRST import * as Sentry from '@sentry/react'; Sentry.init({ dsn: import.meta.env.VITE_SENTRY_DSN, environment: import.meta.env.MODE, integrations: [ Sentry.browserTracingIntegration(), Sentry.replayIntegration(), ], // Sample 10% of transactions // for performance monitoring tracesSampleRate: 0.1, // Record 1% of sessions // for session replay replaysSessionSampleRate: 0.01, // Record 100% of sessions // WITH errors for replay replaysOnErrorSampleRate: 1.0, }); // Add user context after login function onLogin(user: User) { Sentry.setUser({ id: user.id, email: user.email, username: user.name, }); } // Clear on logout function onLogout() { Sentry.setUser(null); }
03
Backend Integration
// server.ts — initialize FIRST const Sentry = require('@sentry/node'); Sentry.init({ dsn: process.env.SENTRY_DSN, environment: process.env.NODE_ENV, tracesSampleRate: 0.1, }); // Express: Sentry request handler // (must be FIRST middleware) app.use(Sentry.Handlers.requestHandler()); // ... all your routes here ... // Express: Sentry error handler // (must be LAST middleware, before // your own error handler) app.use(Sentry.Handlers.errorHandler()); // Your error handler after Sentry's app.use((err, req, res, next) => { res.status(err.statusCode || 500).json({ error: 'Internal server error', requestId: req.requestId, // Sentry event ID for reference sentryId: res.sentry, }); });
04
Source Maps in CI/CD
# In CI pipeline after build: npm install -D @sentry/cli npx sentry-cli releases new $VERSION npx sentry-cli releases files $VERSION \ upload-sourcemaps ./dist \ --url-prefix '~/' npx sentry-cli releases finalize $VERSION # Now: production errors show # ORIGINAL source code line numbers, # not minified bundle positions. # FilterPicker.tsx:42 # not a.js:1:4832
Source maps are uploaded to Sentry (not served publicly — no security risk). Sentry matches stack traces to source maps on their servers. You see readable code; attackers don't.
05
Verify & Set Up Alerts
// Throw a test error in production: app.get('/api/test-sentry', (req, res) => { throw new Error( 'Sentry test: this should appear' + ' in the dashboard'); }); // Visit the endpoint → // See the error in Sentry within seconds // Set up Slack alerts: // Sentry → Settings → Integrations // → Slack → Configure // Alert rule: notify on new issues // in #production-alerts channel
06
Close the Ticket
git switch -c devops/PIXELCRAFT-083-sentry git add src/ server/ git commit -m "Integrate Sentry error tracking (PIXELCRAFT-083)" git push origin devops/PIXELCRAFT-083-sentry # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Observability = Logs + Metrics + Traces.

Sentry provides error tracking — a specialized form of logging with stack traces, breadcrumbs, and session replay. At scale, you need all three pillars.

// Three pillars of observability:

Logs — WHAT happened
  "User 42 got error at 14:32"
  Discrete events with context
  Tools: Pino, ELK, CloudWatch

Metrics — HOW MUCH is happening
  "Error rate: 2.3%, P95 latency: 120ms"
  Aggregated numbers over time
  Tools: Prometheus, Grafana, Datadog

Traces — WHERE time is spent
  "API → Queue → Worker → DB → S3"
  Request flow across services
  Tools: Jaeger, OpenTelemetry

Sentry — error tracking + replay
  Stack trace + user + breadcrumbs
  + session replay video

// Systems too complex for any single
// person to hold in their head need
// all three pillars to understand.
"Observability Lab"
[A]Add custom breadcrumbs: Sentry.addBreadcrumb({ category: 'filter', message: 'Applied sepia', level: 'info' }). When an error occurs, you'll see exactly what the user did before the crash — a timeline of actions.
[B]Add performance monitoring: Sentry tracks transaction durations automatically. View the slowest API endpoints, the slowest page loads, and the P95 latency. Identify performance regressions per release.
[C]Research: what is Sentry's Session Replay? It records a video-like reconstruction of the user's session — every click, scroll, and DOM change. How does it work without recording actual video? How does it handle PII (personally identifiable information)?
REF.MATERIAL
ARTICLE
Sentry
Official guide: SDKs, source maps, breadcrumbs, alerting, performance monitoring, and session replay. The complete error tracking platform.
SENTRYOFFICIALESSENTIAL
VIDEO
Traversy Media
Practical Sentry setup: React + Node.js integration, source maps, breadcrumbs, and Slack alerts. Hands-on tutorial.
SENTRYTUTORIAL
ARTICLE
Sentry
How to upload source maps: Vite plugin, CLI, CI integration. Get readable stack traces from minified production code.
SOURCE MAPSOFFICIAL
ARTICLE
Charity Majors (Honeycomb CEO)
Why traditional logging isn't enough: structured events, high-cardinality data, and the observability philosophy. A shift in how we think about debugging.
OBSERVABILITYPHILOSOPHY
VIDEO
IBM Technology
Logs, metrics, traces — the three pillars explained. Why observability matters as systems grow more complex.
OBSERVABILITYOVERVIEW
// LEAVE EXCITED BECAUSE
Production errors show up in Sentry with full stack traces, user context, and breadcrumbs. You get Slack alerts in real-time. No more "a user reported a bug but we can't reproduce it."