1+ from litellm import CALLBACK_TYPES
2+
3+
14import json
5+ import logging
6+ import os
27import platform
38import sys
49import urllib .request
510from pathlib import Path
611from typing import TYPE_CHECKING , Any
712from uuid import uuid4
813
14+ import litellm
15+
916from strix .config import Config
1017
1118
19+ logger = logging .getLogger (__name__ )
20+
1221if TYPE_CHECKING :
1322 from strix .telemetry .tracer import Tracer
1423
15- _POSTHOG_PUBLIC_API_KEY = "phc_7rO3XRuNT5sgSKAl6HDIrWdSGh1COzxw0vxVIAR6vVZ"
16- _POSTHOG_HOST = "https://us.i.posthog.com"
24+ _POSTHOG_PRIMARY_API_KEY = "phc_7rO3XRuNT5sgSKAl6HDIrWdSGh1COzxw0vxVIAR6vVZ"
25+ _POSTHOG_PRIMARY_HOST = "https://us.i.posthog.com"
26+
27+ _POSTHOG_LLM_API_KEY = os .environ .get ("POSTHOG_LLM_API_KEY" )
28+ _POSTHOG_LLM_HOST = os .environ .get ("POSTHOG_LLM_HOST" )
1729
1830_SESSION_ID = uuid4 ().hex [:16 ]
1931
2032
2133def _is_enabled () -> bool :
22- return (Config .get ("strix_telemetry" ) or "1" ).lower () not in ("0" , "false" , "no" , "off" )
34+ telemetry_value = Config .get ("strix_telemetry" ) or "1"
35+ return telemetry_value .lower () not in ("0" , "false" , "no" , "off" )
36+
37+
38+ def configure_litellm_posthog () -> None :
39+ """Configure LiteLLM to send LLM traces to env postHog account."""
40+
41+ should_send_trace_to_posthog = _POSTHOG_LLM_API_KEY is not None and _POSTHOG_LLM_HOST is not None
42+
43+ if not _is_enabled ():
44+ logger .info ("PostHog telemetry (traces) is disabled" )
45+ return
46+
47+ if not should_send_trace_to_posthog :
48+ logger .info ("PostHog telemetry (traces) is disabled" )
49+ return
50+
51+ os .environ ["POSTHOG_API_KEY" ] = _POSTHOG_LLM_API_KEY
52+ os .environ ["POSTHOG_API_URL" ] = _POSTHOG_LLM_HOST
53+
54+ if "posthog" not in (litellm .success_callback or []):
55+ callbacks = list [CALLBACK_TYPES ](litellm .success_callback or [])
56+ callbacks .append ("posthog" )
57+ litellm .success_callback = callbacks
58+
59+ if "posthog" not in (litellm .failure_callback or []):
60+ callbacks = list [CALLBACK_TYPES ](litellm .failure_callback or [])
61+ callbacks .append ("posthog" )
62+ litellm .failure_callback = callbacks
2363
2464
2565def _is_first_run () -> bool :
@@ -44,22 +84,24 @@ def _get_version() -> str:
4484
4585
4686def _send (event : str , properties : dict [str , Any ]) -> None :
87+ """Send custom events to Instance A (Primary) for manual tracking."""
4788 if not _is_enabled ():
4889 return
4990 try :
5091 payload = {
51- "api_key" : _POSTHOG_PUBLIC_API_KEY ,
92+ "api_key" : _POSTHOG_PRIMARY_API_KEY ,
5293 "event" : event ,
5394 "distinct_id" : _SESSION_ID ,
5495 "properties" : properties ,
5596 }
5697 req = urllib .request .Request ( # noqa: S310
57- f"{ _POSTHOG_HOST } /capture/" ,
98+ f"{ _POSTHOG_PRIMARY_HOST } /capture/" ,
5899 data = json .dumps (payload ).encode (),
59100 headers = {"Content-Type" : "application/json" },
60101 )
61102 with urllib .request .urlopen (req , timeout = 10 ): # noqa: S310 # nosec B310
62103 pass
104+ logger .error (f"Sent custom event '{ event } ' to hardcoded posthog account" )
63105 except Exception : # noqa: BLE001, S110
64106 pass # nosec B110
65107
0 commit comments