forked from metavoiceio/metavoice-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: anonymised telemetry to track usage patterns (metavoiceio#131)
* feat: anonymised telemetry to track usage patterns * add: PR suggestions --------- Co-authored-by: Siddharth Sharma <sid@7732c6f0-01.cloud.together.ai>
- Loading branch information
1 parent
0345ea8
commit eebdcc6
Showing
10 changed files
with
1,085 additions
and
644 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Telemetry | ||
|
||
This directory holds all the telemetry for MetaVoice. We, MetaVoice, capture anonymized telemetry to understand usage patterns. | ||
|
||
If you prefer to opt out of telemetry, set `ANONYMIZED_TELEMETRY=False` in an .env file at the root level of this repo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import abc | ||
import os | ||
import uuid | ||
from abc import abstractmethod | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TelemetryEvent: | ||
name: str | ||
properties: dict | ||
|
||
|
||
class TelemetryClient(abc.ABC): | ||
USER_ID_PATH = str(Path.home() / ".cache" / "metavoice" / "telemetry_user_id") | ||
UNKNOWN_USER_ID = "UNKNOWN" | ||
_curr_user_id = None | ||
|
||
@abstractmethod | ||
def capture(self, event: TelemetryEvent) -> None: | ||
pass | ||
|
||
@property | ||
def user_id(self) -> str: | ||
if self._curr_user_id: | ||
return self._curr_user_id | ||
|
||
# File access may fail due to permissions or other reasons. We don't want to | ||
# crash so we catch all exceptions. | ||
try: | ||
if not os.path.exists(self.USER_ID_PATH): | ||
os.makedirs(os.path.dirname(self.USER_ID_PATH), exist_ok=True) | ||
with open(self.USER_ID_PATH, "w") as f: | ||
new_user_id = str(uuid.uuid4()) | ||
f.write(new_user_id) | ||
self._curr_user_id = new_user_id | ||
else: | ||
with open(self.USER_ID_PATH, "r") as f: | ||
self._curr_user_id = f.read() | ||
except Exception: | ||
self._curr_user_id = self.UNKNOWN_USER_ID | ||
return self._curr_user_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
import os | ||
import sys | ||
|
||
from dotenv import load_dotenv | ||
from posthog import Posthog | ||
|
||
from fam.telemetry import TelemetryClient, TelemetryEvent | ||
|
||
load_dotenv() | ||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout), logging.StreamHandler(sys.stderr)]) | ||
|
||
|
||
class PosthogClient(TelemetryClient): | ||
def __init__(self): | ||
self._posthog = Posthog( | ||
project_api_key="phc_tk7IUlV7Q7lEa9LNbXxyC1sMWlCqiW6DkHyhJrbWMCS", host="https://eu.posthog.com" | ||
) | ||
|
||
if not os.getenv("ANONYMIZED_TELEMETRY", True) or "pytest" in sys.modules: | ||
self._posthog.disabled = True | ||
logger.info("Anonymized telemetry disabled. See fam/telemetry/README.md for more information.") | ||
else: | ||
logger.info("Anonymized telemetry enabled. See fam/telemetry/README.md for more information.") | ||
|
||
posthog_logger = logging.getLogger("posthog") | ||
posthog_logger.disabled = True # Silence posthog's logging | ||
|
||
super().__init__() | ||
|
||
def capture(self, event: TelemetryEvent) -> None: | ||
try: | ||
self._posthog.capture( | ||
self.user_id, | ||
event.name, | ||
{**event.properties}, | ||
) | ||
except Exception as e: | ||
logger.error(f"Failed to send telemetry event {event.name}: {e}") |
Oops, something went wrong.