KAHIBARO
Discord Login Register

20.12. Error Tracking

Why Error Tracking Matters

When you run a backend in development, you see errors directly in your terminal or logs. In production, you usually cannot watch logs all day, and errors may happen rarely or only for certain users.

Error tracking solves this by:

Without error tracking, many bugs stay hidden until users complain. With it, you can find and fix problems much faster.

Important: Error tracking is not a replacement for logging.
Use logging to understand system behavior and business events.
Use error tracking to detect and analyze failures.

Errors vs Exceptions vs Failures

In backend applications, several related terms are common:

Not every exception is a failure:

Error tracking usually focuses on unhandled exceptions or handled exceptions that you decide to report, such as:

What Error Tracking Tools Do

An error tracking system generally provides:

Automatic Error Capture

Libraries for your language and framework:

For example, in a Python web app, an error tracker might catch:

python
def get_user_age(user):
    return 2024 - user.birth_year  # raises AttributeError if user is None

If user is None, the error tracker records:

Grouping and De-duplication

If the same error happens 1,000 times, you do not want 1,000 separate issues. Error trackers group errors by a fingerprint, often based on:

The dashboard might show:

Error groupEventsUsersLast seen
AttributeError in user_service.py:421,2035345 minutes ago
TimeoutError calling payment provider129822 minutes ago
KeyError "cart_id" in cart_controller.py646013 minutes ago

Context and Breadcrumbs

Error trackers add context:

They may also add breadcrumbs, which are recent events leading up to the error, such as:

This helps you reproduce the problem.

Alerts and Notifications

Error trackers can send alerts when:

Common alert channels:

Popular Error Tracking Tools

There are many error tracking tools. Here are some common ones in backend development:

ToolTypeMain features
SentryHosted / selfRich UI, SDKs for many languages, performance data
RollbarHostedReal-time grouping, deployments tracking
HoneybadgerHostedError tracking plus uptime & check-ins
BugsnagHostedError stability scores, rich dashboards
Open-sourceSelf-hostedExamples: Sentry (self-hosted), GlitchTip

You do not need to choose a specific tool for this course, but the concepts are very similar across products.

How Error Tracking Integrates with Your Backend

Error tracking usually integrates at two main levels:

  1. Framework-level integration
    A middleware or plugin that wraps your web framework and captures unhandled exceptions.
  2. Manual reporting in code
    Explicit calls to report important handled exceptions or custom events.

Framework-Level Example (Conceptual)

Imagine a typical Python web framework with middleware support. An error tracking middleware might look like this:

python
def error_tracking_middleware(app, tracker):
    async def wrapper(request):
        try:
            return await app(request)
        except Exception as exc:
            tracker.capture_exception(exc, request=request)
            # Re-raise so your normal error handlers still run
            raise
    return wrapper

Once this wrapper is installed:

A FastAPI, Django, or Flask integration does something similar, but the code is hidden inside the tool’s SDK.

Manual Reporting Example

Sometimes you intentionally catch exceptions but still want to know about them:

python
def charge_card(user_id, amount):
    try:
        return payment_provider.charge(user_id, amount)
    except PaymentTimeoutError as exc:
        error_tracker.capture_exception(exc, extra={"user_id": user_id, "amount": amount})
        # Fallback behavior that user might see as "please try again later"
        raise ServiceUnavailableError("Payment service unavailable")

This pattern is important for:

Error Tracking vs Traditional Logging

Both error tracking and logging are important. They serve different but complementary purposes.

Comparison

AspectLoggingError tracking
FocusAll events, behavior, debug infoFailures and exceptions
VolumePotentially very highMuch lower, filtered to errors
Typical storageLog files, ELK stack, cloud loggingSpecialized dashboard
Query styleSearch by text, fields, time windowBrowse error groups, issues, stack traces
AlertsOften on log patterns or metricsOn new or frequent error groups
User contextOptionalUsually built-in (user, request, environment)

You might log something like:

python
logger.error("Payment provider timeout for user %s", user_id)

While also sending it to error tracking:

python
error_tracker.capture_exception(exc, extra={"user_id": user_id})

Use logs for detailed behavior and debugging. Use error tracking for high-level overview of failures.

Configuring Error Tracking for Different Environments

Backends usually run in multiple environments:

You do not want to treat all environments equally.

Typical Configuration

EnvironmentShould capture errors?Should send alerts?Notes
DevelopmentOptionalNoYou see errors locally anyway
StagingYesMaybeUseful for release testing
ProductionYesYesCritical to detect real issues

In code, you might check an environment variable:

python
import os
ENV = os.getenv("APP_ENV", "development")
if ENV == "production":
    error_tracker.init(dsn=os.getenv("ERROR_TRACKING_DSN"), send_pii=False)
else:
    # In dev, maybe log to console instead or use a different project
    error_tracker.init(dsn=None)  # disabled

This makes sure you do not mix development errors with real production errors in the dashboard.

Error Grouping and Prioritization

As your system grows, you will have many different errors. You need to decide what to fix first.

Grouping Errors

Error tracking tools usually group by:

You might see groups like:

Prioritizing What to Fix

Common ways to prioritize:

A simple prioritization scheme:

PriorityDescriptionExample
P0System unusable for many usersAll API requests return 500
P1Critical feature broken for some usersCheckout fails for some payment methods
P2Non-critical feature brokenProfile picture upload failing
P3Cosmetic or minor errorsRare admin-page errors, typo exceptions

Error tracking dashboards often support:

Example Workflow: From Error to Fix

Here is a typical lifecycle of an error with an error tracking tool:

  1. Error occurs in production.
    A user requests POST /orders, and the backend raises IntegrityError due to a missing foreign key.
  2. Tool captures the error.
    The exception is caught by the framework integration and sent to the tracking service with full context.
  3. Error appears in dashboard.
    You see a new issue:
    • Type: IntegrityError
    • Message: insert or update on table "orders" violates foreign key constraint "orders_user_id_fkey"
    • Affected users: 27
    • First seen: 10 minutes ago
    • Last seen: 1 minute ago
  4. Alert is sent.
    A Slack notification posts in #backend-alerts with a link to the issue.
  5. Developer investigates the stack trace.
    The stack shows the error originates in order_service.create_order.
    They inspect the code and realize user ids are not checked properly after a recent refactor.
  6. Developer reproduces locally.
    Using the request details from the error report, they reproduce the problem with a test or manually.
  7. Fix is implemented and deployed.
    The developer adds proper validation and tests, then deploys a new version.
  8. Error tracking detects that the issue is gone.
    After deployment, the issue stops appearing.
    The developer marks it as resolved in the error tracking dashboard.
  9. Regression monitoring.
    If the same error happens again in a new version, it will automatically be reopened.

Privacy and Security Considerations

Error tracking deals with sensitive information. You must be careful not to leak:

Redacting Sensitive Data

Error tracking tools usually support:

Conceptually, you might configure:

python
error_tracker.init(
    dsn="...",
    scrub_fields=["password", "token", "authorization", "credit_card"]
)

Then, if a request body contains:

json
{
  "email": "user@example.com",
  "password": "supersecret"
}

The error tracker might only store:

json
{
  "email": "user@example.com",
  "password": "[Filtered]"
}

Rule: Never send secrets (passwords, private keys, full credit card numbers) to any third-party service, including error tracking tools.

Minimizing Personal Data

Often you only need:

You do not need:

Design your error tracking integration to attach only the minimum personal data needed to debug issues.

Using Error Tracking with Metrics and Monitoring

Error tracking fits into a larger monitoring and observability setup.

You will often combine:

Example relationships:

In production-ready systems, all three work together.

Integrating Error Tracking Into Your Development Process

To get the most out of error tracking, it should become part of everyday development, not just a tool you check during emergencies.

During Development

During Code Review

Reviewers can ask:

For example, instead of:

python
except Exception:
    pass

Use:

python
except Exception as exc:
    error_tracker.capture_exception(exc)
    raise

Or handle specific exception types.

During On-Call and Incident Response

When something breaks in production:

  1. Check metrics (uptime, error rate).
  2. Open the error tracking dashboard to see:
    • New errors.
    • Spikes in existing errors.
  3. Use stack traces and context to identify the root cause.
  4. After the fix, mark the error as resolved and monitor for regressions.

This becomes a repeatable process that helps stabilize your backend over time.

Summary

Using a good error tracking setup will help you discover bugs earlier, fix them faster, and maintain a more reliable backend system.

Views: 18

Comments

Please login to add a comment.

Don't have an account? Register now!