Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions playgrounds/fastapi-exception-capture/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# FastAPI Exception Capture Usage

Minimal reproduction of PostHog exception capture issue.

## Quick Start

```bash
cd posthog-python/playgrounds/fastapi-exception-capture
export POSTHOG_API_KEY="your_key"
uv sync
uv run python app.py
```

## Test

```bash
curl http://localhost:8000/test-exception
```
66 changes: 66 additions & 0 deletions playgrounds/fastapi-exception-capture/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3

import logging
import os

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from posthog import Posthog

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()

POSTHOG_API_KEY = os.getenv("POSTHOG_API_KEY", "phc_PLACEHOLDER_KEY")
POSTHOG_HOST = os.getenv("POSTHOG_HOST", "https://us.i.posthog.com")


@app.exception_handler(Exception)
async def global_exception_handler(request, exception):
client = Posthog(
POSTHOG_API_KEY,
host=POSTHOG_HOST,
log_captured_exceptions=True,
debug=True,
)

user_id = "anonymous"
properties = {"url": str(request.url)}

capture_id = client.capture_exception(
exception,
distinct_id=user_id,
properties=properties,
)

logger.info(
f"Captured exception: {exception.__class__.__name__} ({str(exception)[:100]}), id: {capture_id}"
)

return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"capture_id": capture_id,
},
)


@app.get("/")
async def root():
return {"message": "PostHog Exception Capture Playground"}


@app.get("/test-exception")
async def test_exception():
raise Exception("TEST BACKEND EXCEPTION")


if __name__ == "__main__":
import uvicorn

print("Starting server...")
print(f"PostHog Host: {POSTHOG_HOST}")
print("\nTest: curl http://localhost:8000/test-exception")
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="info")
8 changes: 8 additions & 0 deletions playgrounds/fastapi-exception-capture/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "fastapi-exception-capture-playground"
version = "0.1.0"
requires-python = ">=3.9"
dependencies = ["fastapi>=0.104.0", "uvicorn[standard]>=0.24.0", "posthog"]

[tool.uv.sources]
posthog = { path = "../..", editable = true }
Loading
Loading