Frontend System Design · Lesson 54 of 86 · 5 min
Error Reporting: Sentry and LogRocket
This is how you explain a production-grade monitoring strategy, not just name-drop tools.
After this, you'll be able to articulate the difference between error aggregation and session replay, explain the trade-offs of using them, and describe a robust implementation that interviewers expect for a large-scale application.
Why console.log fails at scale
In production, you are blind. You can't reproduce user environments, and you can't ask them to open the dev tools. console.log is a fire-and-forget signal into a void. You have no idea if an error is a one-off fluke or a critical failure hitting 10% of your users.
This is the problem that centralized error reporting solves. It moves you from reactive debugging ("a user reported a bug") to proactive monitoring ("we were alerted that a new error is spiking after the last deploy").
Two categories of tools dominate this space:
- Error Aggregators (like Sentry): These tools capture, group, and count errors. They answer: What broke? How many users are affected? Is this a new error from the latest release?
- Session Replay Tools (like LogRocket): These tools record the user's session. They answer: How did it break? What sequence of clicks, network requests, and state changes led to the error?
You need both. One tells you the scope of the fire, the other gives you the video footage to find the cause.
The error reporting lifecycle
When an error occurs in a properly instrumented app, it's not just the stack trace that gets sent. The SDK bundles it with a wealth of context: the user's ID, browser version, OS, the sequence of events leading up to the crash (breadcrumbs), and any custom tags you've set.
This payload is sent to a service like Sentry, which aggregates it. If it's a new error or an old one that's suddenly spiking, it triggers an alert.
The essential production setup
Your goal is to initialize these tools as early as possible in your application's lifecycle to avoid missing startup errors. The most critical piece of the integration is linking the two services, so an error in Sentry provides a direct link to the exact moment it happened in a LogRocket session replay.
This is done with Sentry's beforeSend hook. You grab the current session URL from LogRocket and attach it to the Sentry event before it goes out.
Here is a minimal, production-ready setup for a React app.
// src/main.tsx (or your app's entry point)
import React from 'react';
import ReactDOM from 'react-dom/client';
import * as Sentry from '@sentry/react';
import LogRocket from 'logrocket';
import App from './App';
// 1. Initialize LogRocket first.
LogRocket.init('YOUR_LOGROCKET_APP_ID');
// 2. Initialize Sentry.
Sentry.init({
dsn: "YOUR_SENTRY_DSN",
integrations: [
new Sentry.BrowserTracing(),
// Add Sentry.Replay for their own session replay if you use it.
],
environment: import.meta.env.MODE, // e.g., 'development', 'production'
release: `my-app@${import.meta.env.VITE_APP_VERSION}`, // Assumes version is in env
tracesSampleRate: 0.1, // Performance monitoring. Adjust in production.
// 3. Link Sentry errors to LogRocket sessions.
beforeSend(event, hint) {
// LogRocket.sessionURL will be null if the session is not being recorded.
if (LogRocket.sessionURL) {
event.extra = {
...event.extra,
logRocketSessionURL: LogRocket.sessionURL,
};
}
return event;
},
});
// 4. Identify the user in both services after they log in.
// This would typically be called from an auth hook or callback.
function identifyUser(user: { id: string; email: string; name: string; }) {
Sentry.setUser({ id: user.id, email: user.email });
LogRocket.identify(user.id, { name: user.name, email: user.email });
}
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
How to configure this without breaking production
Integrating third-party monitoring scripts is not free. They add to your bundle size, consume network bandwidth, and can impact main-thread performance. You must be deliberate in your configuration.
The trap: Leaking personally identifiable information (PII). Session replay, by default, records everything. This can include names, addresses, and credit card numbers. Accidentally logging this is a major security and privacy violation. Both Sentry and LogRocket have redaction features. Use them. Configure the SDKs to block sensitive form inputs and HTML elements by default.
The trap: Killing performance. Session replay is more expensive than error aggregation. Recording every DOM mutation and network request for every user can impact performance, especially on low-end devices. Use sampling. For LogRocket, you might record 100% of sessions for authenticated users on a paid plan but only 10% for anonymous visitors. For Sentry performance tracing (tracesSampleRate), start with a low number like 0.1 (10%) and adjust based on the value you get and the cost.
The trap: Creating useless noise. The default configuration will report every minor JavaScript warning and network blip. This leads to alert fatigue, where your team starts ignoring notifications. Be aggressive with filtering. Use beforeSend to ignore benign errors from browser extensions or old, unsupported browsers. Use custom tags (Sentry.setTag) to categorize errors by feature area ('checkout', 'user-profile') so you can route alerts to the right team.
In an interview, discussing these trade-offs (privacy vs. debuggability, performance vs. visibility, signal vs. noise) is more important than just naming the tools. It shows you've dealt with these systems in a real-world environment.
How to answer "Design an error monitoring system"
When asked to design a monitoring strategy, structure your answer around the problems you need to solve.
- Detection: "First, we need to know when things go wrong. I'd use an error aggregation tool like Sentry. We'd initialize its SDK at the application entry point to capture all unhandled exceptions and promise rejections."
- Context & Triage: "A stack trace isn't enough. We need context. I'd configure the SDK to report the release version to correlate errors with deploys. We'd also set user context upon login. This lets us answer questions like 'Is this error only affecting free-tier users?' or 'Did this start with the release that went out this morning?'"
- Reproducibility: "For bugs we can't reproduce, we need more detail. I'd integrate a session replay tool like LogRocket. The key is to link the two. I'd use Sentry's
beforeSendhook to inject the LogRocket session URL into every Sentry error report. This gives us a one-click path from an aggregated error to a full video replay of the session that caused it." - Mitigation & Trade-offs: "Finally, we have to manage the risks. To protect user privacy, we'd configure both tools to redact all PII from inputs and network payloads. To manage performance and cost, we'd use sampling, especially for session replay, perhaps recording a higher percentage of sessions for high-value user flows."
This answer demonstrates a mature, multi-layered approach that covers the full lifecycle of an error, from detection to resolution, while acknowledging the real-world constraints of privacy, performance, and cost.
Try it
Sign up for a free Sentry account and get a DSN key. Instrument a small local project. Intentionally throw an error inside a button's onClick handler. Use Sentry.captureException inside a try/catch block, and add a custom tag and some extra data. Find the error event in your Sentry dashboard and verify the context is there. This should take about 20 minutes.
Next: You'll learn what to show the user while these errors are happening in the lesson on Graceful Degradation and Fallback UIs.
Lock it in — apply this lesson under interview conditions.