-
Notifications
You must be signed in to change notification settings - Fork 174
/
analytics.py
223 lines (185 loc) · 6.57 KB
/
analytics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import annotations
import atexit
import base64
import dataclasses
import datetime
import functools
import json
import logging
import os
import platform
import time
import urllib.error
import urllib.request
import uuid
from typing import Any, Callable
from daft import context
_ANALYTICS_CLIENT = None
_WRITE_KEY = "ebFETjqH70OOvtDvrlBC902iljBZGvPU"
_SEGMENT_BATCH_ENDPOINT = "https://api.segment.io/v1/batch"
logger = logging.getLogger(__name__)
@dataclasses.dataclass(frozen=True)
class AnalyticsEvent:
session_id: str
event_name: str
event_time: datetime.datetime
data: dict[str, Any]
def _build_segment_batch_payload(
events: list[AnalyticsEvent], daft_version: str, daft_build_type: str
) -> dict[str, Any]:
return {
"batch": [
{
"type": "track",
"anonymousId": event.session_id,
"event": event.event_name,
"properties": event.data,
"timestamp": event.event_time.isoformat(),
"context": {
"app": {
"name": "getdaft",
"version": daft_version,
"build": daft_build_type,
},
},
}
for event in events
],
}
def _post_segment_track_endpoint(payload: dict[str, Any]) -> None:
"""Posts a batch of JSON data to Segment"""
req = urllib.request.Request(
_SEGMENT_BATCH_ENDPOINT,
method="POST",
headers={
"Content-Type": "application/json",
"User-Agent": "daft-analytics",
"Authorization": f"Basic {base64.b64encode(f'{_WRITE_KEY}:'.encode()).decode('utf-8')}",
},
data=json.dumps(payload).encode("utf-8"),
)
resp = urllib.request.urlopen(req)
if resp.status != 200:
raise RuntimeError(f"HTTP request to segment returned status code: {resp.status}")
class AnalyticsClient:
"""Non-threadsafe client for sending analytics events, which is a singleton for each Python process"""
def __init__(
self,
daft_version: str,
daft_build_type: str,
publish_payload_function: Callable[[dict[str, Any]], None] = _post_segment_track_endpoint,
buffer_capacity: int = 100,
) -> None:
self._daft_version = daft_version
self._daft_build_type = daft_build_type
self._session_key = str(uuid.uuid4())
# Function to publish a payload to Segment
self._publish = publish_payload_function
# Buffer for events to be sent to Segment
self._buffer_capacity = buffer_capacity
self._buffer: list[AnalyticsEvent] = []
def _append_to_log(self, event_name: str, data: dict[str, Any]) -> None:
self._buffer.append(
AnalyticsEvent(
session_id=self._session_key,
event_name=event_name,
event_time=datetime.datetime.utcnow(),
data=data,
)
)
if len(self._buffer) >= self._buffer_capacity:
self._flush()
def _flush(self) -> None:
try:
payload = _build_segment_batch_payload(self._buffer, self._daft_version, self._daft_build_type)
self._publish(payload)
except Exception as e:
# No-op on failure to avoid crashing the program - TODO: add retries for more robust logging
logger.debug("Error in analytics publisher thread: %s", e)
finally:
self._buffer = []
def track_import(self) -> None:
self._append_to_log(
"Imported Daft",
{
"runner": context.get_context().runner_config.name,
"platform": platform.platform(),
"python_version": platform.python_version(),
"DAFT_ANALYTICS_ENABLED": os.getenv("DAFT_ANALYTICS_ENABLED"),
},
)
def track_df_method_call(self, method_name: str, duration_seconds: float, error: str | None = None) -> None:
optionals = {}
if error is not None:
optionals["error"] = error
self._append_to_log(
"DataFrame Method Call",
{
"method_name": method_name,
"duration_seconds": duration_seconds,
**optionals,
},
)
def track_fn_call(self, fn_name: str, duration_seconds: float, error: str | None = None) -> None:
optionals = {}
if error is not None:
optionals["error"] = error
self._append_to_log(
"daft API Call",
{
"fn_name": fn_name,
"duration_seconds": duration_seconds,
**optionals,
},
)
def init_analytics(daft_version: str, daft_build_type: str) -> AnalyticsClient:
"""Initialize the analytics module
Returns:
AnalyticsClient: initialized singleton AnalyticsClient
"""
global _ANALYTICS_CLIENT
if _ANALYTICS_CLIENT is not None:
return _ANALYTICS_CLIENT
_ANALYTICS_CLIENT = AnalyticsClient(daft_version, daft_build_type)
atexit.register(_ANALYTICS_CLIENT._flush)
return _ANALYTICS_CLIENT
def time_df_method(method):
"""Decorator to track metrics about Dataframe method calls"""
@functools.wraps(method)
def tracked_method(*args, **kwargs):
if _ANALYTICS_CLIENT is None:
return method(*args, **kwargs)
start = time.time()
try:
result = method(*args, **kwargs)
except Exception as e:
_ANALYTICS_CLIENT.track_df_method_call(
method_name=method.__name__, duration_seconds=time.time() - start, error=str(type(e).__name__)
)
raise
_ANALYTICS_CLIENT.track_df_method_call(
method_name=method.__name__,
duration_seconds=time.time() - start,
)
return result
return tracked_method
def time_func(fn):
"""Decorator to track metrics for daft API calls"""
@functools.wraps(fn)
def tracked_fn(*args, **kwargs):
if _ANALYTICS_CLIENT is None:
return fn(*args, **kwargs)
start = time.time()
try:
result = fn(*args, **kwargs)
except Exception as e:
_ANALYTICS_CLIENT.track_fn_call(
fn_name=fn.__name__, duration_seconds=time.time() - start, error=str(type(e).__name__)
)
raise
_ANALYTICS_CLIENT.track_fn_call(
fn_name=fn.__name__,
duration_seconds=time.time() - start,
)
return result
return tracked_fn