Disable metrics in CI (#1822)

This commit is contained in:
Nikhil Rao 2023-09-15 18:15:25 -07:00 committed by GitHub
parent 541e311617
commit 264c44e630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 26 deletions

View File

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

View File

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

View File

@ -11,4 +11,5 @@ echo "Installing reflex from local repo code"
cd /reflex-repo cd /reflex-repo
poetry install poetry install
echo "Running reflex init in test project dir" 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" poetry run /bin/bash -c "cd ~/hello && reflex init && rm -rf ~/.reflex .web && reflex export --backend-only"

View File

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

View File

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

View File

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

View File

@ -35,3 +35,8 @@ def test_telemetry():
assert tel_json["memory"] == tel.memory assert tel_json["memory"] == tel.memory
assert tel_json["reflex_version"] == tel.reflex_version assert tel_json["reflex_version"] == tel.reflex_version
assert tel_json["python_version"] == tel.python_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)