-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/error tracking #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kdh29
wants to merge
7
commits into
main
Choose a base branch
from
feat/error-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/error tracking #352
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da921e1
feat: add optional PostHog error tracking for frontend and backend
claude c9b2224
chore: add dist/ to frontend gitignore
claude 005a878
Wire PostHog env vars through Docker build and runtime configs
01869db
removed test of smoke test
3ee23fe
put thoughtful host to recieve errors
e485180
fixed a url i missed oops
6274082
last missed url
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """ | ||
| PostHog client initialization for error tracking. | ||
|
|
||
| This module provides optional PostHog integration. If POSTHOG_API_KEY | ||
| is not set, all tracking functions become no-ops and the application | ||
| continues to work normally. | ||
|
|
||
| Usage: | ||
| from posthog_client import capture_exception, capture_event, posthog_client | ||
|
|
||
| # In exception handlers: | ||
| capture_exception(exc, {"path": "/api/foo", "user_id": "123"}) | ||
|
|
||
| # For custom events: | ||
| capture_event("backend-server", "api_error", {"status_code": 500}) | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
| import sys | ||
| import traceback | ||
| from typing import Any, Optional | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Environment configuration | ||
| POSTHOG_API_KEY = (os.getenv("POSTHOG_API_KEY") or "").strip() | ||
| POSTHOG_HOST = (os.getenv("POSTHOG_HOST") or "https://us.i.posthog.com").strip() | ||
|
|
||
| # Initialize PostHog client (None if not configured) | ||
| posthog_client: Optional[Any] = None | ||
|
|
||
| if POSTHOG_API_KEY: | ||
| try: | ||
| from posthog import Posthog | ||
|
|
||
| posthog_client = Posthog( | ||
| project_api_key=POSTHOG_API_KEY, | ||
| host=POSTHOG_HOST, | ||
| ) | ||
| logger.info(f"PostHog error tracking initialized (host: {POSTHOG_HOST})") | ||
| except Exception as e: | ||
| logger.warning(f"Failed to initialize PostHog client: {e}") | ||
| posthog_client = None | ||
| else: | ||
| logger.info("POSTHOG_API_KEY not set - error tracking disabled") | ||
|
|
||
|
|
||
| def capture_exception( | ||
| exception: BaseException, | ||
| properties: Optional[dict[str, Any]] = None, | ||
| distinct_id: str = "backend-server", | ||
| ) -> None: | ||
| """ | ||
| Capture an exception to PostHog for error tracking. | ||
|
|
||
| Args: | ||
| exception: The exception to capture | ||
| properties: Additional properties to include with the error | ||
| distinct_id: The user/system identifier (defaults to "backend-server") | ||
| """ | ||
| if posthog_client is None: | ||
| return | ||
|
|
||
| try: | ||
| # Build exception properties | ||
| exc_type = type(exception).__name__ | ||
| exc_message = str(exception) | ||
| exc_traceback = "".join( | ||
| traceback.format_exception(type(exception), exception, exception.__traceback__) | ||
| ) | ||
|
|
||
| error_properties = { | ||
| "$exception_type": exc_type, | ||
| "$exception_message": exc_message, | ||
| "$exception_stack_trace_raw": exc_traceback, | ||
| "exception_type": exc_type, | ||
| "exception_message": exc_message, | ||
| } | ||
|
|
||
| if properties: | ||
| error_properties.update(properties) | ||
|
|
||
| posthog_client.capture( | ||
| distinct_id=distinct_id, | ||
| event="$exception", | ||
| properties=error_properties, | ||
| ) | ||
| except Exception as e: | ||
| # Never let PostHog errors break the application | ||
| logger.warning(f"Failed to capture exception to PostHog: {e}") | ||
|
|
||
|
|
||
| def capture_event( | ||
| distinct_id: str, | ||
| event: str, | ||
| properties: Optional[dict[str, Any]] = None, | ||
| ) -> None: | ||
| """ | ||
| Capture a custom event to PostHog. | ||
|
|
||
| Args: | ||
| distinct_id: The user/system identifier | ||
| event: The event name | ||
| properties: Additional properties to include with the event | ||
| """ | ||
| if posthog_client is None: | ||
| return | ||
|
|
||
| try: | ||
| posthog_client.capture( | ||
| distinct_id=distinct_id, | ||
| event=event, | ||
| properties=properties or {}, | ||
| ) | ||
| except Exception as e: | ||
| # Never let PostHog errors break the application | ||
| logger.warning(f"Failed to capture event to PostHog: {e}") | ||
|
|
||
|
|
||
| def shutdown() -> None: | ||
| """Flush and shutdown the PostHog client gracefully.""" | ||
| if posthog_client is not None: | ||
| try: | ||
| posthog_client.shutdown() | ||
| except Exception as e: | ||
| logger.warning(f"Error shutting down PostHog client: {e}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
|
|
||
| # Build output | ||
| dist/ | ||
|
|
||
| # Playwright | ||
| node_modules/ | ||
| /test-results/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we have to do all this? isn't this stuff builtin to Posthog? ("exception autocapture"?)