From 8617cf13898af9dc7f398f834fd558a5882d737a Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 5 Jun 2023 13:19:27 +0530 Subject: [PATCH] Push telemetry to Posthog to grok Khoj usage --- .github/workflows/dockerize_telemetry_server.yml | 2 ++ src/telemetry/Dockerfile | 5 +++++ src/telemetry/requirements.txt | 3 ++- src/telemetry/telemetry.py | 11 ++++++++--- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/dockerize_telemetry_server.yml b/.github/workflows/dockerize_telemetry_server.yml index ae50db4b..316667d2 100644 --- a/.github/workflows/dockerize_telemetry_server.yml +++ b/.github/workflows/dockerize_telemetry_server.yml @@ -43,3 +43,5 @@ jobs: file: src/telemetry/Dockerfile push: true tags: ghcr.io/${{ github.repository }}-telemetry:${{ env.DOCKER_IMAGE_TAG }} + secrets: | + "POSTHOG_API_KEY=${{ secrets.POSTHOG_API_KEY }}" diff --git a/src/telemetry/Dockerfile b/src/telemetry/Dockerfile index 049bd8e5..23bfe25a 100644 --- a/src/telemetry/Dockerfile +++ b/src/telemetry/Dockerfile @@ -6,5 +6,10 @@ LABEL org.opencontainers.image.source https://github.com/debanjum/khoj COPY requirements.txt /tmp/requirements.txt RUN pip install --no-cache-dir -r /tmp/requirements.txt +# Set Environment Variables +RUN --mount=type=secret,id=POSTHOG_API_KEY \ + export POSTHOG_API_KEY=$(cat /run/secrets/POSTHOG_API_KEY) && \ + echo "POSTHOG_API_KEY=$POSTHOG_API_KEY" > /app/.env + # Copy Application COPY telemetry.py /app/main.py diff --git a/src/telemetry/requirements.txt b/src/telemetry/requirements.txt index fd38fb64..9478d555 100644 --- a/src/telemetry/requirements.txt +++ b/src/telemetry/requirements.txt @@ -1,3 +1,4 @@ uvicorn fastapi -requests +posthog +python-dotenv diff --git a/src/telemetry/telemetry.py b/src/telemetry/telemetry.py index bb05bc8e..5c86587b 100644 --- a/src/telemetry/telemetry.py +++ b/src/telemetry/telemetry.py @@ -1,13 +1,15 @@ # Standard Packages import argparse import logging +import os from typing import Dict, List # External Packages from fastapi import FastAPI from fastapi import HTTPException +from posthog import Posthog +from dotenv import load_dotenv import sqlite3 -import requests import uvicorn @@ -16,6 +18,8 @@ app = FastAPI() sqlfile = "data/khoj.sqlite" logger = logging.getLogger() logger.setLevel(logging.DEBUG) +load_dotenv() +posthog = Posthog(project_api_key=os.getenv("POSTHOG_API_KEY"), host="https://app.posthog.com") @app.post("/v1/telemetry") @@ -26,9 +30,10 @@ def v1_telemetry(telemetry_data: List[Dict[str, str]]): logger.error(error_message) raise HTTPException(status_code=500, detail=error_message) - # POST request to new khoj telemetry server + # POST request to khoj posthog server try: - requests.post("https://telemetry.khoj.dev/v1/telemetry", json=telemetry_data) + for row in telemetry_data: + posthog.capture(row["server_id"], "api_request", row) except Exception as e: raise HTTPException( status_code=500,