feat: add structured JSON logging for all feature usage (Phase 2)
- Create feature_logger.py with JSON logging infrastructure - Add log_feature_usage() calls to all 9 routers after check_feature_access() - Logs written to /app/logs/feature-usage.log - Tracks all usage (not just violations) for future analysis - Phase 2: Non-blocking monitoring complete Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddcd2f4350
commit
1298bd235f
10 files changed
+115
-1
No files matched your search
@@ -16,6 +16,7 @@ from fastapi import APIRouter, HTTPException, Header, Depends
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth, require_admin, check_feature_access, increment_feature_usage
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["insights"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -256,6 +257,8 @@ async def analyze_with_prompt(slug: str, x_profile_id: Optional[str]=Header(defa
|
||||
|
||||
# Phase 2: Check feature access (non-blocking, log only)
|
||||
access = check_feature_access(pid, 'ai_calls')
|
||||
log_feature_usage(pid, 'ai_calls', access, 'analyze')
|
||||
|
||||
if not access['allowed']:
|
||||
logger.warning(
|
||||
f"[FEATURE-LIMIT] User {pid} would be blocked: "
|
||||
@@ -329,6 +332,8 @@ async def analyze_pipeline(x_profile_id: Optional[str]=Header(default=None), ses
|
||||
|
||||
# Phase 2: Check pipeline feature access (boolean - enabled/disabled)
|
||||
access_pipeline = check_feature_access(pid, 'ai_pipeline')
|
||||
log_feature_usage(pid, 'ai_pipeline', access_pipeline, 'pipeline')
|
||||
|
||||
if not access_pipeline['allowed']:
|
||||
logger.warning(
|
||||
f"[FEATURE-LIMIT] User {pid} would be blocked: "
|
||||
@@ -338,10 +343,12 @@ async def analyze_pipeline(x_profile_id: Optional[str]=Header(default=None), ses
|
||||
|
||||
# Also check ai_calls (pipeline uses API calls too)
|
||||
access_calls = check_feature_access(pid, 'ai_calls')
|
||||
log_feature_usage(pid, 'ai_calls', access_calls, 'pipeline_calls')
|
||||
|
||||
if not access_calls['allowed']:
|
||||
logger.warning(
|
||||
f"[FEATURE-LIMIT] User {pid} would be blocked: "
|
||||
f"ai_calls {access_calls['reason']} (used: {access_calls['used']}, limit: {access_calls['limit']})"
|
||||
f"ai_calls {access_calls['reason']} (used: {access_calls['used']}, limit: {access['limit']})"
|
||||
)
|
||||
|
||||
# Old check (keep for now)
|
||||
|
||||
Reference in new issue
Block a user