Push telemetry to Posthog to grok Khoj usage

This commit is contained in:
Debanjum Singh Solanky 2023-06-05 13:19:27 +05:30
parent d13db2e666
commit 8617cf1389
4 changed files with 17 additions and 4 deletions

View file

@ -43,3 +43,5 @@ jobs:
file: src/telemetry/Dockerfile file: src/telemetry/Dockerfile
push: true push: true
tags: ghcr.io/${{ github.repository }}-telemetry:${{ env.DOCKER_IMAGE_TAG }} tags: ghcr.io/${{ github.repository }}-telemetry:${{ env.DOCKER_IMAGE_TAG }}
secrets: |
"POSTHOG_API_KEY=${{ secrets.POSTHOG_API_KEY }}"

View file

@ -6,5 +6,10 @@ LABEL org.opencontainers.image.source https://github.com/debanjum/khoj
COPY requirements.txt /tmp/requirements.txt COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /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 Application
COPY telemetry.py /app/main.py COPY telemetry.py /app/main.py

View file

@ -1,3 +1,4 @@
uvicorn uvicorn
fastapi fastapi
requests posthog
python-dotenv

View file

@ -1,13 +1,15 @@
# Standard Packages # Standard Packages
import argparse import argparse
import logging import logging
import os
from typing import Dict, List from typing import Dict, List
# External Packages # External Packages
from fastapi import FastAPI from fastapi import FastAPI
from fastapi import HTTPException from fastapi import HTTPException
from posthog import Posthog
from dotenv import load_dotenv
import sqlite3 import sqlite3
import requests
import uvicorn import uvicorn
@ -16,6 +18,8 @@ app = FastAPI()
sqlfile = "data/khoj.sqlite" sqlfile = "data/khoj.sqlite"
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(logging.DEBUG) 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") @app.post("/v1/telemetry")
@ -26,9 +30,10 @@ def v1_telemetry(telemetry_data: List[Dict[str, str]]):
logger.error(error_message) logger.error(error_message)
raise HTTPException(status_code=500, detail=error_message) raise HTTPException(status_code=500, detail=error_message)
# POST request to new khoj telemetry server # POST request to khoj posthog server
try: 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: except Exception as e:
raise HTTPException( raise HTTPException(
status_code=500, status_code=500,