Ver código fonte

Disable metrics in CI (#1822)

Nikhil Rao 1 ano atrás
pai
commit
264c44e630

+ 1 - 0
.github/workflows/integration_tests.yml

@@ -24,6 +24,7 @@ env:
   # - Catch encoding errors when printing logs
   # - Best effort print lines that contain illegal chars (map to some default char, etc.)
   PYTHONIOENCODING: "utf8"
+  TELEMETRY_ENABLED: false
 
 jobs:
   example-counter:

+ 3 - 0
.github/workflows/integration_tests_wsl.yml

@@ -13,6 +13,9 @@ on:
 permissions:
   contents: read
 
+env:
+  TELEMETRY_ENABLED: false
+
 jobs:
   example-counter-wsl:
     # 2019 is more stable with WSL in GH actions

+ 1 - 0
integration/init-test/in_docker_test_script.sh

@@ -11,4 +11,5 @@ echo "Installing reflex from local repo code"
 cd /reflex-repo
 poetry install
 echo "Running reflex init in test project dir"
+export TELEMETRY_ENABLED=false
 poetry run /bin/bash -c "cd ~/hello && reflex init && rm -rf ~/.reflex .web && reflex export --backend-only"

+ 4 - 4
reflex/reflex.py

@@ -81,9 +81,9 @@ def init(
     if not os.path.exists(constants.CONFIG_FILE):
         prerequisites.create_config(app_name)
         prerequisites.initialize_app_directory(app_name, template)
-        telemetry.send("init", config.telemetry_enabled)
+        telemetry.send("init")
     else:
-        telemetry.send("reinit", config.telemetry_enabled)
+        telemetry.send("reinit")
 
     # Initialize the .gitignore.
     prerequisites.initialize_gitignore()
@@ -165,7 +165,7 @@ def run(
     assert setup_frontend and frontend_cmd and backend_cmd, "Invalid env"
 
     # Post a telemetry event.
-    telemetry.send(f"run-{env.value}", config.telemetry_enabled)
+    telemetry.send(f"run-{env.value}")
 
     # Display custom message when there is a keyboard interrupt.
     atexit.register(processes.atexit_handler)
@@ -273,7 +273,7 @@ def export(
     )
 
     # Post a telemetry event.
-    telemetry.send("export", config.telemetry_enabled)
+    telemetry.send("export")
 
 
 db_cli = typer.Typer()

+ 36 - 22
reflex/utils/telemetry.py

@@ -1,5 +1,7 @@
 """Anonymous telemetry for Reflex."""
 
+from __future__ import annotations
+
 import json
 import multiprocessing
 import platform
@@ -10,6 +12,7 @@ import psutil
 
 from reflex import constants
 from reflex.base import Base
+from reflex.config import get_config
 
 
 def get_os() -> str:
@@ -67,32 +70,43 @@ class Telemetry(Base):
     python_version: str = get_python_version()
 
 
-def send(event: str, telemetry_enabled: bool) -> None:
+def send(event: str, telemetry_enabled: bool | None = None) -> bool:
     """Send anonymous telemetry for Reflex.
 
     Args:
         event: The event name.
-        telemetry_enabled: Whether to send the telemetry.
+        telemetry_enabled: Whether to send the telemetry (If None, get from config).
+
+    Returns:
+        Whether the telemetry was sent successfully.
     """
+    # Get the telemetry_enabled from the config if it is not specified.
+    if telemetry_enabled is None:
+        telemetry_enabled = get_config().telemetry_enabled
+
+    # Return if telemetry is disabled.
+    if not telemetry_enabled:
+        return False
+
     try:
-        if telemetry_enabled:
-            telemetry = Telemetry()
-            with open(constants.REFLEX_JSON) as f:  # type: ignore
-                reflex_json = json.load(f)
-                distinct_id = reflex_json["project_hash"]
-            post_hog = {
-                "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
-                "event": event,
-                "properties": {
-                    "distinct_id": distinct_id,
-                    "user_os": telemetry.user_os,
-                    "reflex_version": telemetry.reflex_version,
-                    "python_version": telemetry.python_version,
-                    "cpu_count": telemetry.cpu_count,
-                    "memory": telemetry.memory,
-                },
-                "timestamp": datetime.utcnow().isoformat(),
-            }
-            httpx.post("https://app.posthog.com/capture/", json=post_hog)
+        telemetry = Telemetry()
+        with open(constants.REFLEX_JSON) as f:  # type: ignore
+            reflex_json = json.load(f)
+            distinct_id = reflex_json["project_hash"]
+        post_hog = {
+            "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
+            "event": event,
+            "properties": {
+                "distinct_id": distinct_id,
+                "user_os": telemetry.user_os,
+                "reflex_version": telemetry.reflex_version,
+                "python_version": telemetry.python_version,
+                "cpu_count": telemetry.cpu_count,
+                "memory": telemetry.memory,
+            },
+            "timestamp": datetime.utcnow().isoformat(),
+        }
+        httpx.post("https://app.posthog.com/capture/", json=post_hog)
+        return True
     except Exception:
-        pass
+        return False

+ 1 - 0
scripts/integration.sh

@@ -15,6 +15,7 @@ check_ports=${1:-3000 8000}
 shift
 
 # Start the server in the background
+export TELEMETRY_ENABLED=false
 reflex run --loglevel debug --env "$env_mode" "$@" & pid=$!
 
 # Within the context of this bash, $pid_in_bash is what we need to pass to "kill" on exit

+ 5 - 0
tests/test_telemetry.py

@@ -35,3 +35,8 @@ def test_telemetry():
     assert tel_json["memory"] == tel.memory
     assert tel_json["reflex_version"] == tel.reflex_version
     assert tel_json["python_version"] == tel.python_version
+
+
+def test_disable():
+    """Test that disabling telemetry works."""
+    assert not telemetry.send("test", telemetry_enabled=False)