Skip to content

feat(backend): Add Sentry error tracking integration#91

Merged
DevanshuNEU merged 11 commits into
OpenCodeIntel:mainfrom
DevanshuNEU:feat/issue-54-sentry-integration
Dec 12, 2025
Merged

feat(backend): Add Sentry error tracking integration#91
DevanshuNEU merged 11 commits into
OpenCodeIntel:mainfrom
DevanshuNEU:feat/issue-54-sentry-integration

Conversation

@DevanshuNEU

Copy link
Copy Markdown
Collaborator

Summary

Adds Sentry error tracking for production visibility. No more flying blind when things break.

Problem

Currently zero visibility into production errors:

  • Users report "it's broken" - we don't know what happened
  • No stack traces, no context, no user info
  • Can't proactively fix issues

Solution

Integrated Sentry SDK with FastAPI for comprehensive error tracking.

Features

Feature Description
Error Capture Automatic exception tracking with full stack traces
User Context Errors tagged with user_id and email
Performance Monitoring Transaction tracing (10% sample in prod)
Smart Filtering Ignores health checks and bot spam
Graceful Degradation App works fine without SENTRY_DSN

Files Changed

File Change
backend/services/sentry.py New - Sentry initialization and helpers
backend/main.py Init Sentry at startup
backend/middleware/auth.py Set user context after auth
backend/requirements.txt Added sentry-sdk[fastapi]
.env.example Added SENTRY_DSN, ENVIRONMENT
backend/.env.example Added SENTRY_DSN, ENVIRONMENT

Configuration

# Get DSN from https://sentry.io → Project Settings → Client Keys
SENTRY_DSN=https://xxx@sentry.io/xxx
ENVIRONMENT=production  # or development, staging

Sentry is optional - if SENTRY_DSN is not set, error tracking is simply disabled.

Usage

Errors are captured automatically. For manual capture:

from services.sentry import capture_exception, capture_message

# Capture exception with extra context
try:
    risky_operation()
except Exception as e:
    capture_exception(e, repo_id="abc", user_action="indexing")

# Capture warning message
capture_message("Unusual pattern detected", level="warning", details="...")

Testing

cd backend && python3 -m pytest tests/test_api.py -v
# 13 passed ✅

What You'll See in Sentry

  • Full stack traces with local variables
  • User ID and email attached to errors
  • Request details (URL, headers, body)
  • Environment tags (production/staging/development)
  • Performance traces for slow requests

Closes #54

FEATURES:
- Sentry SDK integration with FastAPI
- Automatic error capture with full stack traces
- User context attached to errors (user_id, email)
- Performance monitoring (10% sample in production)
- Smart filtering (ignores health checks, bot paths)

IMPLEMENTATION:
- services/sentry.py: init_sentry(), set_user_context(), capture_exception()
- main.py: Initialize Sentry before other imports
- middleware/auth.py: Set user context after authentication

CONFIGURATION:
- SENTRY_DSN: Your Sentry project DSN
- ENVIRONMENT: development/staging/production

Sentry is optional - app works fine without SENTRY_DSN set.

Closes OpenCodeIntel#54
@vercel

vercel Bot commented Dec 12, 2025

Copy link
Copy Markdown

@DevanshuNEU is attempting to deploy a commit to the Dev's projects Team on Vercel.

A member of the Team first needs to authorize it.

…andler

ENHANCEMENTS:
- Generic 500 exception handler captures all unhandled errors
- Operation context tagging (indexing, search)
- Search errors now captured with query context
- track_background_task decorator for async jobs
- sentry_operation context manager for scoped tracking

NEW HELPERS:
- set_operation_context(): Tag current operation
- capture_http_exception(): For custom exception handlers
- track_background_task(): Decorator for async functions

All 49 tests passing.
FEATURES:
- Structured logging (JSON in prod, pretty in dev)
- OperationContext manager for scoped context
- trace_operation() for Sentry spans
- track_performance() decorator
- Simple metrics counters

USAGE:
  from services.observability import get_logger, OperationContext, metrics

  logger = get_logger('indexer')

  with OperationContext(operation='indexing', repo_id='abc'):
      logger.info('Processing file', context={'file': 'main.py'})
      metrics.increment('files_processed')

Part of OpenCodeIntel#54 - comprehensive observability
…Intel#54)

NEW: backend/services/observability.py
- StructuredLogger: JSON logs (prod) / Pretty logs (dev)
- operation_context: Context manager for operation tracking
- track_performance: Decorator for Sentry spans
- Metrics: Simple counters/gauges/histograms
- Breadcrumbs: Debug trail for errors
- capture_exception/capture_message: Manual error capture

UPDATED: backend/services/sentry.py
- Kept init_sentry() and set_user_context()
- Re-exports observability functions for convenience
- Cleaner separation of concerns

Usage:
  from services.observability import get_logger, operation_context

  logger = get_logger('indexer')
  with operation_context('indexing', repo_id='abc') as ctx:
      logger.info('Starting', files=100)
      ctx.set_extra('functions', 340)
- Cleaner StructuredLogger with JSON/Pretty formatters
- Simplified trace_operation context manager
- Added track_performance decorator for async/sync functions
- Simple Metrics class for counters and timings
- Better Sentry integration with filtering

Updates OpenCodeIntel#54
- Cleaner StructuredLogger with JSON (prod) / pretty (dev) output
- Simplified Sentry helpers: set_operation_context, add_breadcrumb, capture_exception
- track_time context manager for performance spans
- trace_operation decorator for function-level tracing
- Simple in-memory Metrics class for counters and timings
- Better documentation with usage examples
…CodeIntel#54)

- Replace all print() with structured logger calls
- Add trace_operation and track_time for performance spans
- Add breadcrumbs for debugging flow
- Capture exceptions with context (repo_id, operation)
- Add metrics: indexing_completed, search_requests, search_latency_ms
…er (OpenCodeIntel#54)

- search_enhancer: Add structured logging for query expansion errors
- dependency_analyzer: Replace all print() with structured logger
- Add metrics for dependency_graphs_built
- Capture exceptions with context
…l#54)

- cache.py: Structured logging + cache hit/miss/error metrics
- repo_manager.py: Logging for repo sync and clone operations
- supabase_service.py: DB operation logging

Added metrics:
- cache_hits, cache_misses, cache_errors
- repos_cloned
- playground.py: Structured logging for demo repo loading
- analysis.py: Logging for dependency graph and style analysis
- repos.py: Logging for indexing operations + WebSocket error capture

All print() statements replaced with structured logger calls.
…deIntel#54)

- style_analyzer.py: Logging for style analysis operations
- performance_metrics.py: Logging for metrics initialization
- indexer_optimized.py: Full logging for incremental indexing + error capture

All print() statements replaced with structured logger calls.
Total: ~50 print statements converted to structured logs.
@vercel

vercel Bot commented Dec 12, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
opencodeintel Ignored Ignored Preview Dec 12, 2025 5:37am

@DevanshuNEU DevanshuNEU merged commit bf40c59 into OpenCodeIntel:main Dec 12, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(backend): Add Sentry error tracking

1 participant