Allow disabling Khoj telemetry via KHOJ_TELEMETRY_DISABLE env var

This commit is contained in:
Debanjum 2024-11-11 19:15:30 -08:00
parent dc109559d4
commit e1d0015248
7 changed files with 27 additions and 17 deletions

View file

@ -61,6 +61,10 @@ services:
# - KHOJ_NO_HTTPS=True # - KHOJ_NO_HTTPS=True
# - KHOJ_DOMAIN=192.168.0.104 # - KHOJ_DOMAIN=192.168.0.104
# - KHOJ_DOMAIN=khoj.example.com # - 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 command: --host="0.0.0.0" --port=42110 -vv --anonymous-mode --non-interactive

View file

@ -14,13 +14,6 @@ We don't send any personal information or any information from/about your conten
## Disable Telemetry ## Disable Telemetry
If you're self-hosting Khoj, you can opt out of telemetry at any time. To do so, 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`
1. Open `~/.khoj/khoj.yml`
2. Add the following configuration:
```
app:
should-log-telemetry: false
```
3. Save the file and restart Khoj
If you have any questions or concerns, please reach out to us on [Discord](https://discord.gg/BDgyabRM6e). If you have any questions or concerns, please reach out to us on [Discord](https://discord.gg/BDgyabRM6e).

View file

@ -249,7 +249,11 @@ def configure_server(
state.search_models = configure_search(state.search_models, state.config.search_type) state.search_models = configure_search(state.search_models, state.config.search_type)
setup_default_agent(user) 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) logger.info(message)
if not init: if not init:
@ -361,7 +365,7 @@ def configure_search_types():
@schedule.repeat(schedule.every(2).minutes) @schedule.repeat(schedule.every(2).minutes)
def upload_telemetry(): 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 return
try: try:

View file

@ -204,7 +204,12 @@ def update_telemetry_state(
state.telemetry += [ state.telemetry += [
log_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,
) )
] ]

View file

@ -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_server_pg import migrate_server_pg
from khoj.migrations.migrate_version import migrate_config_to_version 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 from khoj.utils.yaml import parse_config_from_file
@ -79,7 +79,7 @@ def cli(args=None):
else: else:
args = run_migrations(args) args = run_migrations(args)
args.config = parse_config_from_file(args.config_file) 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 args.config.app.should_log_telemetry = False
return args return args

View file

@ -254,8 +254,10 @@ def get_server_id():
return server_id return server_id
def telemetry_disabled(app_config: AppConfig): def telemetry_disabled(app_config: AppConfig, telemetry_disable_env) -> bool:
return not app_config or not app_config.should_log_telemetry return (
not app_config.should_log_telemetry if app_config and app_config.should_log_telemetry else telemetry_disable_env
)
def log_telemetry( def log_telemetry(
@ -263,11 +265,12 @@ def log_telemetry(
api: str = None, api: str = None,
client: Optional[str] = None, client: Optional[str] = None,
app_config: Optional[AppConfig] = None, app_config: Optional[AppConfig] = None,
disable_telemetry_env: bool = False,
properties: dict = None, properties: dict = None,
): ):
"""Log basic app usage telemetry like client, os, api called""" """Log basic app usage telemetry like client, os, api called"""
# Do not log usage telemetry, if telemetry is disabled via app config # 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 [] return []
if properties.get("server_id") is None: if properties.get("server_id") is None:

View file

@ -12,7 +12,7 @@ from khoj.database.models import ProcessLock
from khoj.processor.embeddings import CrossEncoderModel, EmbeddingsModel from khoj.processor.embeddings import CrossEncoderModel, EmbeddingsModel
from khoj.utils import config as utils_config from khoj.utils import config as utils_config
from khoj.utils.config import OfflineChatProcessorModel, SearchModels 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 from khoj.utils.rawconfig import FullConfig
# Application Global State # Application Global State
@ -34,6 +34,7 @@ SearchType = utils_config.SearchType
scheduler: BackgroundScheduler = None scheduler: BackgroundScheduler = None
schedule_leader_process_lock: ProcessLock = None schedule_leader_process_lock: ProcessLock = None
telemetry: List[Dict[str, str]] = [] telemetry: List[Dict[str, str]] = []
telemetry_disabled: bool = is_env_var_true("KHOJ_TELEMETRY_DISABLE")
khoj_version: str = None khoj_version: str = None
device = get_device() device = get_device()
chat_on_gpu: bool = True chat_on_gpu: bool = True