Skip to content

Commit da24d51

Browse files
committed
fix: initialize PostHog client once at app startup
- Move client creation to module level instead of per-exception - Remove unnecessary shutdown() call on long-lived client - Remove playground deps (fastapi, uvicorn) from root pyproject.toml - Bump test deps for Python 3.14 support (pydantic, langgraph-checkpoint, tiktoken)
1 parent 336e3c3 commit da24d51

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

  • playgrounds/fastapi-exception-capture

‎playgrounds/fastapi-exception-capture/app.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,24 @@
1515
POSTHOG_API_KEY = os.getenv("POSTHOG_API_KEY", "phc_PLACEHOLDER_KEY")
1616
POSTHOG_HOST = os.getenv("POSTHOG_HOST", "https://us.i.posthog.com")
1717

18+
posthog_client = Posthog(
19+
POSTHOG_API_KEY,
20+
host=POSTHOG_HOST,
21+
log_captured_exceptions=True,
22+
debug=True,
23+
)
24+
1825

1926
@app.exception_handler(Exception)
2027
async def global_exception_handler(request, exception):
21-
client = Posthog(
22-
POSTHOG_API_KEY,
23-
host=POSTHOG_HOST,
24-
log_captured_exceptions=True,
25-
debug=True,
26-
)
27-
2828
user_id = "anonymous"
2929
properties = {"url": str(request.url)}
3030

31-
capture_id = client.capture_exception(
31+
capture_id = posthog_client.capture_exception(
3232
exception,
3333
distinct_id=user_id,
3434
properties=properties,
3535
)
36-
client.shutdown()
3736

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

0 commit comments

Comments
 (0)