From e1d00152484d216e6b7af86b3cb1632ce1d10793 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 11 Nov 2024 19:15:30 -0800 Subject: [PATCH] Allow disabling Khoj telemetry via KHOJ_TELEMETRY_DISABLE env var --- docker-compose.yml | 4 ++++ documentation/docs/miscellaneous/telemetry.md | 9 +-------- src/khoj/configure.py | 8 ++++++-- src/khoj/routers/helpers.py | 7 ++++++- src/khoj/utils/cli.py | 4 ++-- src/khoj/utils/helpers.py | 9 ++++++--- src/khoj/utils/state.py | 3 ++- 7 files changed, 27 insertions(+), 17 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3333c066..ff2594f4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -61,6 +61,10 @@ services: # - KHOJ_NO_HTTPS=True # - KHOJ_DOMAIN=192.168.0.104 # - KHOJ_DOMAIN=khoj.example.com + # Uncomment the line below to disable telemetry. + # Telemetry helps us prioritize feature development and understand how people are using Khoj + # Read more at https://docs.khoj.dev/miscellaneous/telemetry + # - KHOJ_TELEMETRY_DISABLE=True command: --host="0.0.0.0" --port=42110 -vv --anonymous-mode --non-interactive diff --git a/documentation/docs/miscellaneous/telemetry.md b/documentation/docs/miscellaneous/telemetry.md index 2d1bd2af..7abe9d8d 100644 --- a/documentation/docs/miscellaneous/telemetry.md +++ b/documentation/docs/miscellaneous/telemetry.md @@ -14,13 +14,6 @@ We don't send any personal information or any information from/about your conten ## Disable Telemetry -If you're self-hosting Khoj, you can opt out of telemetry at any time. To do so, -1. Open `~/.khoj/khoj.yml` -2. Add the following configuration: -``` -app: - should-log-telemetry: false -``` -3. Save the file and restart Khoj +If you're self-hosting Khoj, you can opt out of telemetry at any time by setting the `KHOJ_TELEMETRY_DISABLE` environment variable to `True` via shell or `docker-compose.yml` If you have any questions or concerns, please reach out to us on [Discord](https://discord.gg/BDgyabRM6e). diff --git a/src/khoj/configure.py b/src/khoj/configure.py index 002413b8..56e5f209 100644 --- a/src/khoj/configure.py +++ b/src/khoj/configure.py @@ -249,7 +249,11 @@ def configure_server( state.search_models = configure_search(state.search_models, state.config.search_type) setup_default_agent(user) - message = "📡 Telemetry disabled" if telemetry_disabled(state.config.app) else "📡 Telemetry enabled" + message = ( + "📡 Telemetry disabled" + if telemetry_disabled(state.config.app, state.telemetry_disabled) + else "📡 Telemetry enabled" + ) logger.info(message) if not init: @@ -361,7 +365,7 @@ def configure_search_types(): @schedule.repeat(schedule.every(2).minutes) def upload_telemetry(): - if telemetry_disabled(state.config.app) or not state.telemetry: + if telemetry_disabled(state.config.app, state.telemetry_disabled) or not state.telemetry: return try: diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 3a2cb5cf..6275061a 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -204,7 +204,12 @@ def update_telemetry_state( state.telemetry += [ log_telemetry( - telemetry_type=telemetry_type, api=api, client=client, app_config=state.config.app, properties=user_state + telemetry_type=telemetry_type, + api=api, + client=client, + app_config=state.config.app, + disable_telemetry_env=state.telemetry_disabled, + properties=user_state, ) ] diff --git a/src/khoj/utils/cli.py b/src/khoj/utils/cli.py index 2e2bb77d..55ee5fbf 100644 --- a/src/khoj/utils/cli.py +++ b/src/khoj/utils/cli.py @@ -16,7 +16,7 @@ from khoj.migrations.migrate_processor_config_openai import ( ) from khoj.migrations.migrate_server_pg import migrate_server_pg from khoj.migrations.migrate_version import migrate_config_to_version -from khoj.utils.helpers import in_debug_mode, resolve_absolute_path +from khoj.utils.helpers import in_debug_mode, is_env_var_true, resolve_absolute_path from khoj.utils.yaml import parse_config_from_file @@ -79,7 +79,7 @@ def cli(args=None): else: args = run_migrations(args) args.config = parse_config_from_file(args.config_file) - if in_debug_mode(): + if is_env_var_true("KHOJ_TELEMETRY_DISABLE") or in_debug_mode(): args.config.app.should_log_telemetry = False return args diff --git a/src/khoj/utils/helpers.py b/src/khoj/utils/helpers.py index 173a1a73..78b2ff35 100644 --- a/src/khoj/utils/helpers.py +++ b/src/khoj/utils/helpers.py @@ -254,8 +254,10 @@ def get_server_id(): return server_id -def telemetry_disabled(app_config: AppConfig): - return not app_config or not app_config.should_log_telemetry +def telemetry_disabled(app_config: AppConfig, telemetry_disable_env) -> bool: + return ( + not app_config.should_log_telemetry if app_config and app_config.should_log_telemetry else telemetry_disable_env + ) def log_telemetry( @@ -263,11 +265,12 @@ def log_telemetry( api: str = None, client: Optional[str] = None, app_config: Optional[AppConfig] = None, + disable_telemetry_env: bool = False, properties: dict = None, ): """Log basic app usage telemetry like client, os, api called""" # Do not log usage telemetry, if telemetry is disabled via app config - if telemetry_disabled(app_config): + if telemetry_disabled(app_config, disable_telemetry_env): return [] if properties.get("server_id") is None: diff --git a/src/khoj/utils/state.py b/src/khoj/utils/state.py index 4a8b36b5..166f50f1 100644 --- a/src/khoj/utils/state.py +++ b/src/khoj/utils/state.py @@ -12,7 +12,7 @@ from khoj.database.models import ProcessLock from khoj.processor.embeddings import CrossEncoderModel, EmbeddingsModel from khoj.utils import config as utils_config from khoj.utils.config import OfflineChatProcessorModel, SearchModels -from khoj.utils.helpers import LRU, get_device +from khoj.utils.helpers import LRU, get_device, is_env_var_true from khoj.utils.rawconfig import FullConfig # Application Global State @@ -34,6 +34,7 @@ SearchType = utils_config.SearchType scheduler: BackgroundScheduler = None schedule_leader_process_lock: ProcessLock = None telemetry: List[Dict[str, str]] = [] +telemetry_disabled: bool = is_env_var_true("KHOJ_TELEMETRY_DISABLE") khoj_version: str = None device = get_device() chat_on_gpu: bool = True