Skip to content

Commit

Permalink
[usage] log entrypoints to posthog (#4)
Browse files Browse the repository at this point in the history
* add posthog events"

* lint
  • Loading branch information
asaiacai authored Sep 17, 2024
1 parent 5f30fcf commit 980b7ac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions sky/setup_files/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def parse_readme(readme: str) -> str:
"typing_extensions",
'filelock >= 3.6.0',
'packaging',
'posthog',
'psutil',
'pulp',
# Cython 3.0 release breaks PyYAML 5.4.* (https://github.com/yaml/pyyaml/issues/601)
Expand Down
7 changes: 7 additions & 0 deletions sky/usage/constants.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
"""Constants for usage collection."""

import os

LOG_URL = 'http://usage.skypilot.co:9090/loki/api/v1/push' # pylint: disable=line-too-long

USAGE_MESSAGE_SCHEMA_VERSION = 1

PRIVACY_POLICY_PATH = '~/.sky/privacy_policy'

POSTHOG_API_KEY = os.environ.get(
'POSTHOG_API_KEY', 'phc_4UgX80BfVNmYRZ2o3dJLyRMGkv1CxBozPAcPnD29uP4')

POSTHOG_HOST = os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com')

USAGE_POLICY_MESSAGE = (
'SkyPilot collects usage data to improve its services. '
'`setup` and `run` commands are not collected to '
Expand Down
8 changes: 8 additions & 0 deletions sky/usage/usage_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any, Callable, Dict, List, Optional, Union

import click
import posthog
import requests

import sky
Expand All @@ -27,6 +28,8 @@
from sky import task as task_lib

logger = sky_logging.init_logger(__name__)
posthog_client = posthog.Posthog(constants.POSTHOG_API_KEY,
constants.POSTHOG_HOST)


def _get_current_timestamp_ns() -> int:
Expand Down Expand Up @@ -320,6 +323,11 @@ def _send_to_loki(message_type: MessageType):
data=payload,
headers=headers,
timeout=0.5)
posthog_client.capture(common_utils.get_user_hash(), 'trainy-sky-event', {
**prom_labels,
**json.loads(str(message)), 'timestamp': str(log_timestamp)
})

if response.status_code != 204:
logger.debug(
f'Grafana Loki failed with response: {response.text}\n{payload}')
Expand Down

0 comments on commit 980b7ac

Please sign in to comment.