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

Cog 597 refactor analytics #231

Merged
merged 3 commits into from
Nov 16, 2024
Merged

Cog 597 refactor analytics #231

merged 3 commits into from
Nov 16, 2024

Conversation

dexters1
Copy link
Collaborator

@dexters1 dexters1 commented Nov 15, 2024

Refactor telemetry analytics

Summary by CodeRabbit

  • New Features

    • Introduced a new analytics proxy for improved telemetry data handling.
  • Bug Fixes

    • Enhanced error handling for telemetry data submissions.

Added proxy usage with vercel hosting for telemetry and analytics

Feature COG-597
Adding user_id to event properties allows tracking of which user started the event

Refactor COG-597
Copy link
Contributor

coderabbitai bot commented Nov 15, 2024

Walkthrough

The pull request introduces modifications to the cognee/shared/utils.py module, focusing on telemetry data handling and the integration of an analytics proxy. Key changes include the removal of the Posthog library, the introduction of a new global variable for the analytics proxy URL, and significant updates to the send_telemetry function, which now constructs a payload and sends telemetry data via HTTP POST requests. Error handling has also been improved to check the response from the analytics proxy.

Changes

File Change Summary
cognee/shared/utils.py - Removed Posthog library integration.
- Added vercel_url for analytics proxy.
- Updated send_telemetry to construct a payload and send it via POST to vercel_url.
- Improved error handling for response status code.

Possibly related PRs

  • Cog 492 posthog unique users #187: The changes in this PR modify the send_telemetry function in cognee/shared/utils.py to utilize a new get_anonymous_id() function, which is directly related to the telemetry data handling changes in the main PR.

Poem

In the code where bunnies play,
New paths for data found their way.
With proxies now, we send our cheer,
Telemetry hops, loud and clear!
No more Posthog, we’ve made a shift,
To better ways, our code will lift! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

gitguardian bot commented Nov 15, 2024

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
13947935 Triggered Generic High Entropy Secret d90f5fe cognee/shared/utils.py View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
cognee/shared/utils.py (3)

20-20: Consider making vercel_url configurable

To enhance flexibility and security, it's recommended to avoid hardcoding the vercel_url. Instead, retrieve it from an environment variable or a configuration file. This allows for easier updates and environment-specific configurations.

Apply this diff to make the URL configurable:

- vercel_url = "https://proxyanalytics.vercel.app"
+ vercel_url = os.getenv("ANALYTICS_PROXY_URL", "https://proxyanalytics.vercel.app")

This change uses the ANALYTICS_PROXY_URL environment variable if it's set, with a default fallback to the current URL.


51-51: Avoid duplication of user_id in payload

The user_id is included in both user_properties and properties. To prevent redundancy and potential confusion, consider removing the duplicate entry unless both are required for the analytics proxy.

Apply this diff to remove the duplicate:

payload = {
    "anonymous_id": str(get_anonymous_id()),
    "event_name": event_name,
    "user_properties": {
-       "user_id": str(user_id),
    },
    "properties": {
        "time": current_time.strftime("%m/%d/%Y"),
        "user_id": str(user_id),
        **additional_properties
    },
}

Alternatively, if user_id is required in user_properties, remove it from properties:

payload = {
    "anonymous_id": str(get_anonymous_id()),
    "event_name": event_name,
    "user_properties": {
        "user_id": str(user_id),
    },
    "properties": {
        "time": current_time.strftime("%m/%d/%Y"),
-       "user_id": str(user_id),
        **additional_properties
    },
}

Ensure consistency based on how the analytics proxy expects the data.

Also applies to: 55-55


54-54: Use ISO 8601 format for time field

Consider formatting the time field using the ISO 8601 standard for better interoperability and parsing consistency across different systems.

Apply this diff to update the time format:

- "time": current_time.strftime("%m/%d/%Y"),
+ "time": current_time.isoformat(),

The isoformat() method provides a standardized timestamp, such as '2023-10-21T15:03:00+00:00'.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8c0c0b9 and 2703215.

📒 Files selected for processing (1)
  • cognee/shared/utils.py (3 hunks)

Comment on lines +60 to +63
response = requests.post(vercel_url, json=payload)

try:
posthog.capture(get_anonymous_id(), event_name, properties)
except Exception as e:
print("ERROR sending telemetric data to Posthog. See exception: %s", e)
if response.status_code != 200:
print(f"Error sending telemetry through proxy: {response.status_code}")
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add exception handling and use logging instead of print

Wrap the HTTP request in a try-except block to handle potential exceptions like connection errors. Additionally, replace print statements with the logging module for better control over logging levels and outputs.

Apply this diff to improve error handling and logging:

+import logging

+logging.basicConfig(level=logging.INFO)

 def send_telemetry(event_name: str, user_id, additional_properties: dict = {}):
     if os.getenv("TELEMETRY_DISABLED"):
         return

     env = os.getenv("ENV")
     if env in ["test", "dev"]:
         return

     current_time = datetime.now(timezone.utc)
     payload = {
         "anonymous_id": str(get_anonymous_id()),
         "event_name": event_name,
         "user_properties": {
             "user_id": str(user_id),
         },
         "properties": {
             "time": current_time.strftime("%m/%d/%Y"),
             "user_id": str(user_id),
             **additional_properties
         },
     }

-    response = requests.post(vercel_url, json=payload)

-    if response.status_code != 200:
-        print(f"Error sending telemetry through proxy: {response.status_code}")
+    try:
+        response = requests.post(vercel_url, json=payload)
+        response.raise_for_status()
+    except requests.exceptions.RequestException as e:
+        logging.error(f"Error sending telemetry: {e}")
+        return

This change includes:

  • Importing the logging module and configuring it.
  • Wrapping the requests.post call in a try-except block to catch exceptions.
  • Using response.raise_for_status() to check for HTTP errors.
  • Logging errors using logging.error().

This approach ensures that exceptions are properly handled, and error messages are logged appropriately.

Committable suggestion skipped: line range outside the PR's diff.

@Vasilije1990 Vasilije1990 merged commit c2e265f into main Nov 16, 2024
24 of 26 checks passed
@Vasilije1990 Vasilije1990 deleted the COG-597-refactor-analytics branch November 16, 2024 12:56
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.

2 participants