Skip to content
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

fix: Ensure Sentry is initialized for non logged in users too #1183

Merged
merged 5 commits into from
Jan 15, 2025

Conversation

tushar-composio
Copy link
Contributor

@tushar-composio tushar-composio commented Jan 13, 2025

Important

Ensure Sentry is initialized for users without user_data.json by updating get_sentry_config() and caching fetch_dsn().

  • Behavior:
    • Ensure Sentry is initialized for users without user_data.json by calling update_dsn() in get_sentry_config().
    • update_dsn() now creates directory structure if user_data.json is absent.
  • Performance:
    • Cache fetch_dsn() to optimize repeated calls.

This description was created by Ellipsis for 01a5d91. It will automatically update as commits are pushed.

Copy link

vercel bot commented Jan 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
composio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 8:47am

Comment on lines +39 to +41
if not user_file.exists():
update_dsn()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary update_dsn() call. The user_data.json file is created in update_dsn if it doesn't exist, so calling update_dsn before checking if the file exists is redundant.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if not user_file.exists():
update_dsn()
if not user_file.exists():

Comment on lines 111 to 122
@atexit.register
def update_dsn() -> None:
user_file = Path.home() / ".composio" / "user_data.json"
if not user_file.exists():
return

try:
data = json.loads(user_file.read_text(encoding="utf-8"))
except json.JSONDecodeError:
return

if data.get("api_key") is None:
return

if data.get("sentry") is not None and data.get("sentry").get("dsn") is not None:
if user_file.exists():
try:
data = json.loads(user_file.read_text(encoding="utf-8"))
except json.JSONDecodeError:
return
else:
data: t.Dict[str, t.Any] = {}

if data.get("sentry", {}).get("dsn") is not None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update_dsn function now unnecessarily creates an empty dictionary if the user file doesn't exist, even if the DSN is already available. This could lead to unintended data overwrites if the file is later created.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Reviewed everything up to 24d8ba1 in 47 seconds

More details
  • Looked at 58 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 drafted comments based on config settings.
1. python/composio/utils/sentry.py:26
  • Draft comment:
    Using @cache on fetch_dsn may lead to stale DSN values if they change on the server. Consider removing the cache if DSN updates are expected.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable:
    The DSN is only fetched in very specific circumstances: 1) when the program exits and there's no DSN stored, or 2) when the user file doesn't exist. Once fetched, it's stored in a file. The cache doesn't matter because the value is persisted to disk and subsequent runs will use the file value. Within a single run, caching is fine because we don't need real-time DSN updates.
    Maybe there are scenarios where the program needs to refresh the DSN mid-execution that I'm not seeing. The caching could prevent that.
    Looking at the code flow, the DSN is only used for initialization and stored persistently. There's no mechanism or apparent need for mid-execution DSN updates.
    The comment should be deleted because the caching behavior is appropriate for this use case. The DSN is meant to be stable within a program execution and is properly persisted between runs.
2. python/composio/utils/sentry.py:111
  • Draft comment:
    Ensure the .composio directory exists before writing to user_data.json to avoid FileNotFoundError.
  • Reason this comment was not posted:
    Comment did not seem useful.

Workflow ID: wflow_YGIbAIKfqlkBwQFT


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@@ -34,6 +36,9 @@ def fetch_dsn() -> t.Optional[str]:

def get_sentry_config() -> t.Optional[t.Dict]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_sentry_config() function's return type hint t.Optional[t.Dict] should be more specific. Consider using t.Optional[t.Dict[str, t.Any]] to better document the expected structure of the configuration dictionary.

@@ -106,18 +111,15 @@ def init():
@atexit.register
def update_dsn() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a docstring to the update_dsn() function explaining its purpose, behavior, and the fact that it's registered as an atexit handler. This would help future maintainers understand when and how this function is called.

@@ -22,6 +23,7 @@
import sentry_sdk.types


@cache
def fetch_dsn() -> t.Optional[str]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fetch_dsn() function is now cached using @cache decorator. Consider adding a comment or updating the docstring to indicate this caching behavior, as it affects the function's performance characteristics and potential memory usage.

@shreysingla11
Copy link
Collaborator

Code Review Summary

Overall, this is a well-structured PR that improves Sentry initialization for non-logged-in users. Here's my assessment:

Strengths:

✅ Addition of caching for fetch_dsn() improves performance
✅ Better handling of non-logged-in users
✅ Simplified and more robust error handling
✅ Improved dictionary access patterns using get() with defaults

Suggestions for Improvement:

  • Add more comprehensive documentation for functions
  • Make type hints more specific
  • Consider documenting caching behavior

Code Quality Rating: 8/10

The changes are solid, improve system reliability, and fix important edge cases. The code is well-structured and maintainable.

@@ -34,6 +36,9 @@ def fetch_dsn() -> t.Optional[str]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @cache decorator on fetch_dsn could lead to stale DSNs being used. If the DSN changes on the backend, the cached value will prevent the updated DSN from being retrieved until the process restarts.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
@cache_with_ttl(ttl_seconds=300)
def fetch_dsn() -> t.Optional[str]:

Comment on lines 114 to 127
if user_file.exists():
try:
data = json.loads(user_file.read_text(encoding="utf-8"))
except json.JSONDecodeError:
return
else:
data: t.Dict[str, t.Any] = {}

if data.get("sentry", {}).get("dsn") is not None:
return

dsn = fetch_dsn()
if dsn is None:
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for updating the DSN is unnecessarily complex and can be simplified. The nested if statements and redundant checks can be removed.

Comment on lines +39 to +41
if not user_file.exists():
update_dsn()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling update_dsn() when the user file doesn't exist in get_sentry_config() might lead to unnecessary network requests. This function's primary purpose seems to be configuration retrieval, not updating.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if not user_file.exists():
update_dsn()
if not user_file.exists():
return None

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Incremental review on 46fe3c8 in 25 seconds

More details
  • Looked at 10 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/composio/utils/sentry.py:130
  • Draft comment:
    Consider moving the directory creation line user_file.parent.mkdir(parents=True, exist_ok=True) before any file operations to ensure the directory exists before attempting to read or write the file.
  • Reason this comment was not posted:
    Comment looked like it was already resolved.

Workflow ID: wflow_wpOZJjoiGL7jV0sL


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@@ -22,6 +23,7 @@
import sentry_sdk.types


@cache

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @cache decorator on fetch_dsn could lead to stale DSNs being used. If the DSN changes on the backend, the client will not pick up the change until the process restarts.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
@cache
@cache(ttl=300)

Comment on lines +39 to +41
if not user_file.exists():
update_dsn()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary call to update_dsn() if the user file doesn't exist. The subsequent if not user_file.exists(): return None already handles this case.

return

dsn = fetch_dsn()
if dsn is None:
return

data["sentry"] = {"dsn": dsn}
user_file.parent.mkdir(parents=True, exist_ok=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in update_dsn to create the user data directory is now performed even if the DSN is already present, which is unnecessary.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
user_file.parent.mkdir(parents=True, exist_ok=True)
if not user_file.exists():
user_file.parent.mkdir(parents=True, exist_ok=True)

Comment on lines 6 to +9
import traceback
import types
import typing as t
from functools import cache

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-importing 'cache' from functools can lead to confusion and potential import errors. Remove the duplicate import statement.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Incremental review on 2621fee in 11 seconds

More details
  • Looked at 18 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/composio/utils/sentry.py:9
  • Draft comment:
    Unnecessary change in import order. Consider reverting to maintain consistency.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The import statement for 'cache' from 'functools' was moved, but this change is unnecessary and doesn't affect functionality. It should be reverted for consistency.

Workflow ID: wflow_jUN2eodN0D11ExX6


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

except json.JSONDecodeError:
return
else:
data = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the type hint reduces code maintainability and can lead to type-related bugs. Reintroduce the type hint for clarity and error prevention. The type hint for 'data' was removed, which may lead to type-related issues if 'data' is used inconsistently elsewhere.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Incremental review on 01a5d91 in 14 seconds

More details
  • Looked at 13 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. python/composio/utils/sentry.py:120
  • Draft comment:
    Consider keeping the type hint for 'data' for clarity and type safety.
data: t.Dict[str, t.Any] = {}
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The type hint for 'data' was removed, which can lead to less clarity and potential type-related issues in the future.

Workflow ID: wflow_UXk53iTAdwAONPWg


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@tushar-composio tushar-composio merged commit 2b172f1 into master Jan 15, 2025
21 of 23 checks passed
@tushar-composio tushar-composio deleted the ENG-2513 branch January 15, 2025 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants