From 264c44e630d93fd9511e8dcfa35a7e3d002d7bc4 Mon Sep 17 00:00:00 2001 From: Nikhil Rao Date: Fri, 15 Sep 2023 18:15:25 -0700 Subject: [PATCH] Disable metrics in CI (#1822) --- .github/workflows/integration_tests.yml | 1 + .github/workflows/integration_tests_wsl.yml | 3 + .../init-test/in_docker_test_script.sh | 1 + reflex/reflex.py | 8 +-- reflex/utils/telemetry.py | 58 ++++++++++++------- scripts/integration.sh | 1 + tests/test_telemetry.py | 5 ++ 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/.github/workflows/integration_tests.yml b/.github/workflows/integration_tests.yml index beecee811..1849d95f9 100644 --- a/.github/workflows/integration_tests.yml +++ b/.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: diff --git a/.github/workflows/integration_tests_wsl.yml b/.github/workflows/integration_tests_wsl.yml index 88a2dadca..b6366dc16 100644 --- a/.github/workflows/integration_tests_wsl.yml +++ b/.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 diff --git a/integration/init-test/in_docker_test_script.sh b/integration/init-test/in_docker_test_script.sh index 1424d7222..6ac3af815 100755 --- a/integration/init-test/in_docker_test_script.sh +++ b/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" \ No newline at end of file diff --git a/reflex/reflex.py b/reflex/reflex.py index a797dbe88..e90d8b1ad 100644 --- a/reflex/reflex.py +++ b/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() diff --git a/reflex/utils/telemetry.py b/reflex/utils/telemetry.py index d98a8e3bb..0d6dfe897 100644 --- a/reflex/utils/telemetry.py +++ b/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 diff --git a/scripts/integration.sh b/scripts/integration.sh index f01f73817..ef56fd165 100755 --- a/scripts/integration.sh +++ b/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 diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py index 3019aa432..f4d35ec4c 100644 --- a/tests/test_telemetry.py +++ b/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)