-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #451 from SciPhi-AI/Nolan/Telemetry
Add basic telemetry
- Loading branch information
Showing
9 changed files
with
250 additions
and
13 deletions.
There are no files selected for viewing
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,27 @@ | ||
## Telemetry | ||
|
||
R2R utilizes telemetry that collects **anonymous** usage information. | ||
|
||
Why? We use this information to help us understand how R2R is used so that we can prioritize work on new features and bug fixes, allowing us to improve R2R's performance and stability. | ||
|
||
### Disabling Telemetry | ||
|
||
To disable telemetry, set the environment variable `TELEMETRY_ENABLED` to `false`, `0`, or `f`. You can do this in your terminal before running your application: | ||
|
||
```sh copy | ||
export TELEMETRY_ENABLED=false | ||
``` | ||
|
||
With this setup, telemetry events will not be captured if telemetry is disabled, providing a way for users to control the logging of events. | ||
|
||
### What do you collect? | ||
|
||
We collect basic information such as: | ||
|
||
- **Feature Usage**: Which features are being used and how often. | ||
|
||
|
||
### Where is telemetry information stored? | ||
We use [Posthog](https://posthog.com/) for our telemetry data. | ||
|
||
Posthog is an open source platform for product analytics. Learn more about Posthog on [posthog.com](https://posthog.com/) or [github.com/posthog](https://github.com/posthog). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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,59 @@ | ||
from typing import Any, Dict | ||
import uuid | ||
|
||
|
||
class BaseTelemetryEvent: | ||
def __init__(self, event_type: str, properties: Dict[str, Any]): | ||
self.event_type = event_type | ||
self.properties = properties | ||
self.event_id = str(uuid.uuid4()) | ||
|
||
|
||
class DailyActiveUserEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str): | ||
super().__init__("DailyActiveUser", {"user_id": user_id}) | ||
|
||
|
||
class FeatureUsageEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str, feature: str): | ||
super().__init__( | ||
"FeatureUsage", {"user_id": user_id, "feature": feature} | ||
) | ||
|
||
|
||
class ErrorEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str, endpoint: str, error_message: str): | ||
super().__init__( | ||
"Error", | ||
{ | ||
"user_id": user_id, | ||
"endpoint": endpoint, | ||
"error_message": error_message, | ||
}, | ||
) | ||
|
||
|
||
class RequestLatencyEvent(BaseTelemetryEvent): | ||
def __init__(self, endpoint: str, latency: float): | ||
super().__init__( | ||
"RequestLatency", {"endpoint": endpoint, "latency": latency} | ||
) | ||
|
||
|
||
class GeographicDistributionEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str, country: str): | ||
super().__init__( | ||
"GeographicDistribution", {"user_id": user_id, "country": country} | ||
) | ||
|
||
|
||
class SessionDurationEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str, duration: float): | ||
super().__init__( | ||
"SessionDuration", {"user_id": user_id, "duration": duration} | ||
) | ||
|
||
|
||
class UserPathEvent(BaseTelemetryEvent): | ||
def __init__(self, user_id: str, path: str): | ||
super().__init__("UserPath", {"user_id": user_id, "path": path}) |
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,42 @@ | ||
import logging | ||
import os | ||
import posthog | ||
from r2r.telemetry.events import BaseTelemetryEvent | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PosthogClient: | ||
""" | ||
This is a write-only project API key, so it can only create new events. It can't | ||
read events or any of your other data stored with PostHog, so it's safe to use in public apps. | ||
""" | ||
|
||
def __init__(self, api_key: str, enabled: bool = True): | ||
self.enabled = enabled | ||
if self.enabled: | ||
logger.info( | ||
"Initializing anonymized telemetry. To disable, set TELEMETRY_ENABLED=false in your environment." | ||
) | ||
posthog.project_api_key = api_key | ||
else: | ||
posthog.disabled = True | ||
logger.info( | ||
f"Posthog telemetry {'enabled' if self.enabled else 'disabled'}" | ||
) | ||
|
||
def capture(self, event: BaseTelemetryEvent): | ||
if self.enabled: | ||
posthog.capture(event.event_id, event.event_type, event.properties) | ||
|
||
|
||
# Initialize the telemetry client with a flag to enable or disable telemetry | ||
telemetry_enabled = os.getenv("TELEMETRY_ENABLED", "true").lower() in ( | ||
"true", | ||
"1", | ||
"t", | ||
) | ||
telemetry_client = PosthogClient( | ||
api_key="phc_OPBbibOIErCGc4NDLQsOrMuYFTKDmRwXX6qxnTr6zpU", | ||
enabled=telemetry_enabled, | ||
) |
Oops, something went wrong.