Prompt
How do I get stack traces and breadcrumbs for production bugs?
Latest observation
To get useful stack traces and breadcrumbs for production bugs, you generally need to instrument your app with an error monitoring tool and make sure it can capture context from the places users interact with the app.
What you want to capture
- Stack traces: where the error happened in code
- Breadcrumbs: a trail of events leading up to the error, like:
- clicks
- navigation changes
- API requests
- console logs/warnings
- route transitions
- form submissions
Typical solution
Use an error monitoring platform such as:
- Sentry
- Datadog RUM
- Bugsnag
- Rollbar
- New Relic
These tools usually provide:
- automatic exception capture
- source map support for minified frontend code
- breadcrumb collection
- release tracking
- user/session context
For frontend apps
1. Install an SDK
Example with Sentry:
npm install @sentry/browser
2. Initialize early
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "YOUR_DSN",
tracesSampleRate: 0.1,
integrations: [
Sentry.browserTracingIntegration(),
Sentry.replayIntegration(), // optional, if you want session replay
],
});
3. Add breadcrumbs manually when needed
Sentry.addBreadcrumb({
category: "ui.click",
message: "User clicked Save",
level: "info",
});
4. Capture exceptions
try {
doSomething();
} catch (err) {
Sentry.captureException(err);
}
5. Upload source maps
If your production JS is minified, source maps are critical to get readable stack traces.
For backend apps
1. Add exception logging
Wrap request handling so unhandled exceptions are reported.
2. Attach request context
Include:
- user ID
- request ID / trace ID
- route
- query params
- relevant business identifiers
3. Capture breadcrumbs / event trail
Record notable events before the error:
- DB queries
- external API calls
- retries
- state transitions
- warnings/errors
4. Use structured logging
Even if your monitoring tool captures stack traces, logs help reconstruct what happened.
Example fields:
{
"level": "error",
"message": "Payment failed",
"userId": "123",
"orderId": "abc",
"requestId": "req-789"
}
Best practices
- Initialize monitoring as early as possible
- Don’t log sensitive data like passwords, tokens, or full payment details
- Use source maps in production builds
- Set release/version info so errors map to the correct deploy
- Attach user/session IDs for correlation
- Sample carefully if volume is high
- Send breadcrumbs for important user actions and network requests
- Test in staging to make sure errors are actually reported
If you want to build it yourself
You can:
- capture global unhandled exceptions and promise rejections
- collect a ring buffer of recent events as breadcrumbs
- send error + breadcrumbs to your backend
- store the last N logs per session/request
But in most cases, using a dedicated tool is much faster and more reliable.
Practical recommendation
If you’re starting from scratch:
- Use Sentry
- Enable:
- exception capture
- breadcrumbs
- source maps
- release tagging
- Add a few manual breadcrumbs around critical user flows
- Correlate frontend errors with backend logs using a request/session ID
If you want, I can give you a copy-paste setup for React, Next.js, Node, Python, or mobile.