Skip to content

[CI] (8dccaac) fastapi/fastapi3-ai-saas - #2354

Closed
wizard-ci-bot[bot] wants to merge 1 commit into
mainfrom
wizard-ci-8dccaac-fastapi-fastapi3-ai-saas
Closed

wizard-ci-bot[bot] wants to merge 1 commit into
mainfrom
wizard-ci-8dccaac-fastapi-fastapi3-ai-saas

Conversation

@wizard-ci-bot

@wizard-ci-bot wizard-ci-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

Automated wizard CI run

Source: wizard-pr
Trigger ID: 8dccaac
App: fastapi/fastapi3-ai-saas
App directory: apps/fastapi/fastapi3-ai-saas
Workbench branch: wizard-ci-8dccaac-fastapi-fastapi3-ai-saas
Wizard branch: release-please--branches--main--components--wizard
Context Mill branch: main
PostHog (MCP) branch: master
Timestamp: 2026-07-03T00:33:33.106Z
Duration: 759.1s

YARA Scanner

✓ 63 tool calls scanned, 0 violations detected

No violations: ✓ 63 clean scans

@wizard-ci-bot

wizard-ci-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown
Author

PR Evaluation Report

Summary

This PR integrates PostHog analytics into a FastAPI-based AI SaaS application. It adds a PostHog client module with instance-based initialization and exception autocapture, a custom ASGI middleware that wraps requests in PostHog contexts, and 11 meaningful capture events across auth, content generation, API key management, settings, and usage routes. Environment variables are properly configured via Pydantic Settings.

Files changed Lines added Lines removed
13 +230 -1

Confidence score: 5/5 🧙

  • Email used as distinct_id: The middleware and auth routes use identify_context(user.email) instead of str(user.id). Using raw email as distinct_id is a data quality anti-pattern — it exposes PII in every event and can cause fragmented user data if a user changes their email. Should use str(user.id) and set email via `` person properties. [MEDIUM]
  • Middleware creates a DB session per request: _get_user_from_scope opens a new SessionLocal() to look up the user from the cookie, even though FastAPI's dependency injection (via RequiredUser) already resolves the user downstream. This adds unnecessary DB overhead on every HTTP request. [MEDIUM]

File changes

Filename Score Description
app/posthog_client.py 4/5 New module initializing PostHog with Posthog() constructor, enable_exception_autocapture=True, and atexit.register(shutdown)
app/middleware.py 3/5 ASGI middleware wrapping requests in PostHog context; uses email as distinct_id and creates redundant DB sessions
app/main.py 4/5 Registers middleware, adds flush() in lifespan shutdown
app/config.py 5/5 Adds PostHog settings to Pydantic Settings model
app/routers/auth.py 3/5 Auth events with `` person properties; uses email as distinct_id
app/routers/generate.py 5/5 Well-instrumented generation events with rich properties
app/routers/api_keys.py 4/5 API key lifecycle events
app/routers/pages.py 4/5 Dashboard view event with context properties
app/routers/settings.py 4/5 Settings and password change events
app/routers/usage.py 4/5 Usage stats viewed event with aggregate properties
.env.example 5/5 PostHog env var placeholders documented
requirements.txt 5/5 posthog>=3.0.0 added
posthog-setup-report.md 5/5 Comprehensive setup report

App sanity check ✅

Criteria Result Description
App builds and runs Yes No syntax errors; all imports resolve correctly
Preserves existing env vars & configs Yes Existing settings, routes, and configs untouched
No syntax or type errors Yes All Python syntax is valid; types are consistent
Correct imports/exports Yes posthog module imports and internal app imports are correct
Minimal, focused changes Yes All changes directly relate to PostHog integration
Pre-existing issues None N/A

Issues

  • Redundant DB session in middleware: The middleware manually parses cookies and opens a SessionLocal() to resolve the user on every request, duplicating work that FastAPI's RequiredUser dependency already does. This adds a DB query per request even for unauthenticated endpoints. Consider using Starlette's scope["state"] or reading the user from the dependency injection system. [MEDIUM]

Other completed criteria

  • Environment variables documented in .env.example with PostHog token, host, and disabled flag
  • Build configuration valid — requirements.txt has correct package specifier
  • atexit.register(posthog_client.shutdown) ensures graceful flush on process exit

PostHog implementation ⚠️

Criteria Result Description
PostHog SDKs installed Yes posthog>=3.0.0 in requirements.txt
PostHog client initialized Yes Instance-based Posthog() constructor with enable_exception_autocapture=True, host from env, atexit.register(shutdown)
capture() Yes 11 meaningful capture calls across all route modules
identify() N/A Server-only app
Error tracking Yes enable_exception_autocapture=True set on client; middleware wraps requests in new_context() which auto-captures exceptions
Reverse proxy N/A Server-only app

Issues

  • Email used as distinct_id throughout: identify_context(user.email) is called in middleware.py (line 37), auth.py login (line 40), and auth.py signup (line 84). Email is PII and should not be used as distinct_id — it exposes the email on every event and causes data fragmentation if the user changes their email. Use str(user.id) instead, and set email via `` person properties (which the signup event already partially does with is_active). [MEDIUM]

Other completed criteria

  • API key loaded from POSTHOG_PROJECT_TOKEN environment variable via Pydantic Settings — not hardcoded
  • Host correctly configured, defaults to https://us.i.posthog.com, configurable via POSTHOG_HOST
  • posthog_client.flush() called in FastAPI lifespan shutdown
  • posthog_disabled flag allows disabling PostHog without code changes
  • Middleware correctly wraps requests in new_context() so route captures inherit context

PostHog insights and events ✅

Filename PostHog events Description
app/routers/auth.py user_signed_up, user_logged_in, user_logged_out Auth lifecycle events; signup includes `` person properties, signup_method, and `credits_granted`
app/routers/generate.py content_generated, content_generation_failed Core product events with generation type, credits used/remaining, prompt length, and failure reason
app/routers/api_keys.py api_key_created, api_key_revoked API key lifecycle with active key count
app/routers/pages.py dashboard_viewed Page view with total generations and API key count
app/routers/settings.py settings_updated, password_changed Account management events with field changed
app/routers/usage.py usage_stats_viewed Usage analytics with period, generation count, and credits used
app/middleware.py capturedException (via autocapture) Middleware context enables automatic exception capture for all requests

Issues

No issues — event naming is consistent (snake_case), events map to real user actions, properties are enriched with contextual metadata, and no PII appears in event properties.

Other completed criteria

  • Events enable clear product funnels: user_signed_up → dashboard_viewed → content_generated
  • Failure events (content_generation_failed) with reason properties enable churn analysis
  • Properties include actionable metadata (credits, generation types, counts) without PII
  • Event naming follows consistent object_verb snake_case convention

Reviewed by wizard workbench PR evaluator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants