insightfy-bms-ms-ums / DEBUGGING_GUIDE.md
MukeshKapoor25's picture
fix(nosql,settings): resolve Redis URL configuration and clean up logging
4904af8

Store Connectivity Debugging Guide

Overview

Enhanced logging has been added to the app/nosql.py file to help debug MongoDB and Redis connectivity issues during application startup. The Redis URL configuration issue has been resolved.

Issue Resolution

The original error was caused by Redis expecting a proper URL scheme (redis://) but receiving a host:port format. This has been fixed by automatically constructing a proper Redis URL from the CACHE_URI configuration.

Changes Made

1. Enhanced Logging in connect_stores()

  • Added initial connectivity check logging with configuration details
  • Individual logging for MongoDB and Redis ping results
  • Detailed error logging when connectivity fails
  • Clear indication of which store (MongoDB or Redis) failed

2. Enhanced Logging in ping_mongo()

  • Debug logs before attempting MongoDB ping
  • Success logs with ping result details
  • Error logs with error type, message, and database name
  • Full exception traceback for debugging

3. Enhanced Logging in ping_redis()

  • Debug logs before attempting Redis ping
  • Success logs with pong result
  • Error logs with error type, message, and cache URI
  • Full exception traceback for debugging

4. Enhanced Logging in Client Initialization

  • MongoDB client initialization logs with masked URI
  • Redis client initialization logs with configuration details
  • Detailed error logging for initialization failures

5. Enhanced _redis_from_settings()

  • Debug logs showing which connection method is used (CACHE_URL vs CACHE_URI)
  • Parsed connection details (host, port, password presence, db)
  • Better error messages for invalid CACHE_URI format

6. Environment Configuration

  • Added CACHE_DB=0 to .env file
  • Changed default log level to DEBUG for development environment

7. Redis URL Construction (settings.py)

  • Automatically constructs proper Redis URL from CACHE_URI when CACHE_URL is not set
  • Handles authentication with password from CACHE_K
  • Format: redis://:password@host:port/db or redis://host:port/db

Log Output Examples

Successful Startup

{"message": "Starting store connectivity check", "mongo_uri_set": true, "mongo_db_name": "insightfy-bloom", "cache_url_set": false, "cache_uri_set": true}
{"message": "MongoDB connectivity check result", "mongo_ok": true, "mongo_db_name": "insightfy-bloom"}
{"message": "Redis connectivity check result", "redis_ok": true, "cache_uri": "redis-11382..."}
{"message": "Store connectivity OK (Mongo & Redis)"}

Failed Connectivity

{"message": "Store connectivity check failed", "mongo_ok": false, "redis_ok": true, "mongo_uri_set": true, "cache_configured": true}
RuntimeError: Store connectivity check failed - MongoDB: False, Redis: True

Debugging Steps

  1. Check the logs - Look for the connectivity check messages
  2. Identify which store failed - MongoDB or Redis
  3. Review error details - Error type, message, and traceback
  4. Verify configuration - Check if URIs are set correctly
  5. Test connectivity - Ensure network access to MongoDB and Redis

Common Issues

MongoDB Connection Issues

  • Invalid MONGO_URI format
  • Network connectivity to MongoDB cluster
  • Authentication credentials
  • SSL/TLS certificate issues

Redis Connection Issues

  • Invalid CACHE_URI format (should be host:port)
  • Network connectivity to Redis server
  • Authentication password (CACHE_K)
  • Wrong database number (CACHE_DB)

Configuration Requirements

MongoDB

MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/dbname?ssl=true
MONGO_DB_NAME=insightfy-bloom

Redis

CACHE_URI=redis-host.com:6379
CACHE_K=your_redis_password
CACHE_DB=0

Reverting to Production Log Level

For production, change the log level back to INFO or WARNING:

# In app/app.py
_log_level = os.getenv("LOG_LEVEL") or ("INFO" if _env == "development" else "INFO")

Or set the environment variable:

LOG_LEVEL=INFO

Final Solution Summary

The Redis connectivity issue was resolved by:

  1. Root Cause: Redis client expected a URL with proper scheme (redis://) but was receiving host:port format
  2. Solution: Modified settings.py to automatically construct proper Redis URL from CACHE_URI
  3. Implementation:
    • If CACHE_URL is not set but CACHE_URI exists, construct: redis://:password@host:port/db
    • Added proper error handling and logging for Redis client creation
  4. Result: Both MongoDB and Redis connectivity checks now pass successfully

Current Working Configuration

# Redis Configuration (in .env)
CACHE_URI=redis-11382.c305.ap-south-1-1.ec2.redns.redis-cloud.com:11382
CACHE_K=your_redis_password
CACHE_DB=0

# MongoDB Configuration
MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/dbname
MONGO_DB_NAME=insightfy-bloom

The application now starts successfully with both stores connected.