Batch upload telemetry data at regular interval instead of while querying

This commit is contained in:
Debanjum Singh Solanky 2023-05-16 23:32:18 +05:30
parent 3ede919c66
commit 134cce9d32
4 changed files with 23 additions and 11 deletions

View file

@ -3,6 +3,7 @@ import sys
import logging import logging
import json import json
from enum import Enum from enum import Enum
import requests
# External Packages # External Packages
import schedule import schedule
@ -223,3 +224,18 @@ def save_chat_session():
state.processor_config.conversation.chat_session = None state.processor_config.conversation.chat_session = None
logger.info("📩 Saved current chat session to conversation logs") logger.info("📩 Saved current chat session to conversation logs")
@schedule.repeat(schedule.every(1).minutes)
def upload_telemetry():
if not state.config.app.should_log_telemetry or not state.telemetry:
print("No telemetry to upload") if not state.telemetry else print("Telemetry logging disabled")
return
try:
logger.debug(f"📡 Upload usage telemetry to {constants.telemetry_server}: {state.telemetry}")
requests.post(constants.telemetry_server, json=state.telemetry)
except Exception as e:
logger.error(f"Error uploading telemetry: {e}")
else:
state.telemetry = None

View file

@ -168,7 +168,7 @@ def search(
# Cache results # Cache results
state.query_cache[query_cache_key] = results state.query_cache[query_cache_key] = results
log_telemetry(telemetry_type="api", api="search", app_config=state.config.app) state.telemetry += [log_telemetry(telemetry_type="api", api="search", app_config=state.config.app)]
return results return results
@ -193,7 +193,7 @@ def update(t: Optional[SearchType] = None, force: Optional[bool] = False):
else: else:
logger.info("📬 Processor reconfigured via API") logger.info("📬 Processor reconfigured via API")
log_telemetry(telemetry_type="api", api="update", app_config=state.config.app) state.telemetry += [log_telemetry(telemetry_type="api", api="update", app_config=state.config.app)]
return {"status": "ok", "message": "khoj reloaded"} return {"status": "ok", "message": "khoj reloaded"}
@ -255,6 +255,6 @@ def chat(q: Optional[str] = None):
conversation_log=meta_log.get("chat", []), conversation_log=meta_log.get("chat", []),
) )
log_telemetry(telemetry_type="api", api="chat", app_config=state.config.app) state.telemetry += [log_telemetry(telemetry_type="api", api="chat", app_config=state.config.app)]
return {"status": status, "response": gpt_response, "context": compiled_references} return {"status": status, "response": gpt_response, "context": compiled_references}

View file

@ -172,7 +172,7 @@ def log_telemetry(telemetry_type: str, api: str = None, client: str = None, app_
"""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 not app_config or not app_config.should_log_telemetry: if not app_config or not app_config.should_log_telemetry:
return return []
# Populate telemetry data to log # Populate telemetry data to log
request_body = { request_body = {
@ -189,9 +189,4 @@ def log_telemetry(telemetry_type: str, api: str = None, client: str = None, app_
request_body["client"] = client request_body["client"] = client
# Log telemetry data to telemetry endpoint # Log telemetry data to telemetry endpoint
logger = logging.getLogger(__name__) return request_body
try:
logger.debug(f"Log usage telemetry to {constants.telemetry_server}: {request_body}")
requests.post(constants.telemetry_server, json=request_body)
except:
pass

View file

@ -1,6 +1,6 @@
# Standard Packages # Standard Packages
import threading import threading
from typing import List from typing import List, Dict
from packaging import version from packaging import version
# External Packages # External Packages
@ -25,6 +25,7 @@ cli_args: List[str] = None
query_cache = LRU() query_cache = LRU()
search_index_lock = threading.Lock() search_index_lock = threading.Lock()
SearchType = utils_config.SearchType SearchType = utils_config.SearchType
telemetry: List[Dict[str, str]] = []
if torch.cuda.is_available(): if torch.cuda.is_available():
# Use CUDA GPU # Use CUDA GPU