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
# - Best effort print lines that contain illegal chars (map to some default char, etc.)
PYTHONIOENCODING: "utf8"
TELEMETRY_ENABLED: false
jobs:
example-counter:

View File

@ -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

View File

@ -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"

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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)