forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.py
More file actions
178 lines (150 loc) · 5.8 KB
/
Copy pathtelemetry.py
File metadata and controls
178 lines (150 loc) · 5.8 KB
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
# Copyright 2019 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import uuid
from datetime import datetime
from functools import wraps
from os.path import expanduser, join
from pathlib import Path
from typing import List, Tuple
import requests
from feast.version import get_version
TELEMETRY_ENDPOINT = (
"https://us-central1-kf-feast.cloudfunctions.net/bq_telemetry_logger"
)
class Telemetry:
def __init__(self):
self._telemetry_enabled = False
self.check_env_and_configure()
def check_env_and_configure(self):
telemetry_enabled = (
os.getenv("FEAST_TELEMETRY", default="True") == "True"
) # written this way to turn the env var string into a boolean
# Check if it changed
if telemetry_enabled != self._telemetry_enabled:
self._telemetry_enabled = telemetry_enabled
if self._telemetry_enabled:
feast_home_dir = join(expanduser("~"), ".feast")
Path(feast_home_dir).mkdir(exist_ok=True)
telemetry_filepath = join(feast_home_dir, "telemetry")
self._is_test = os.getenv("FEAST_IS_TELEMETRY_TEST", "False") == "True"
self._telemetry_counter = {"get_online_features": 0}
if os.path.exists(telemetry_filepath):
with open(telemetry_filepath, "r") as f:
self._telemetry_id = f.read()
else:
self._telemetry_id = str(uuid.uuid4())
with open(telemetry_filepath, "w") as f:
f.write(self._telemetry_id)
print(
"Feast is an open source project that collects anonymized error reporting and usage statistics. To opt out or learn"
" more see https://docs.feast.dev/reference/telemetry"
)
@property
def telemetry_id(self):
if os.getenv("FEAST_FORCE_TELEMETRY_UUID"):
return os.getenv("FEAST_FORCE_TELEMETRY_UUID")
return self._telemetry_id
def log(self, function_name: str):
self.check_env_and_configure()
if self._telemetry_enabled and self.telemetry_id:
if function_name == "get_online_features":
if self._telemetry_counter["get_online_features"] % 10000 != 0:
self._telemetry_counter["get_online_features"] += 1
return
json = {
"function_name": function_name,
"telemetry_id": self.telemetry_id,
"timestamp": datetime.utcnow().isoformat(),
"version": get_version(),
"os": sys.platform,
"is_test": self._is_test,
}
try:
requests.post(TELEMETRY_ENDPOINT, json=json)
except Exception as e:
if self._is_test:
raise e
else:
pass
return
def log_exception(self, error_type: str, traceback: List[Tuple[str, int, str]]):
self.check_env_and_configure()
if self._telemetry_enabled and self.telemetry_id:
json = {
"error_type": error_type,
"traceback": traceback,
"telemetry_id": self.telemetry_id,
"version": get_version(),
"os": sys.platform,
"is_test": self._is_test,
}
try:
requests.post(TELEMETRY_ENDPOINT, json=json)
except Exception as e:
if self._is_test:
raise e
else:
pass
return
def log_exceptions(func):
@wraps(func)
def exception_logging_wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except Exception as e:
error_type = type(e).__name__
trace_to_log = []
tb = e.__traceback__
while tb is not None:
trace_to_log.append(
(
_trim_filename(tb.tb_frame.f_code.co_filename),
tb.tb_lineno,
tb.tb_frame.f_code.co_name,
)
)
tb = tb.tb_next
tele.log_exception(error_type, trace_to_log)
raise
return result
return exception_logging_wrapper
def log_exceptions_and_usage(func):
@wraps(func)
def exception_logging_wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
tele.log(func.__name__)
except Exception as e:
error_type = type(e).__name__
trace_to_log = []
tb = e.__traceback__
while tb is not None:
trace_to_log.append(
(
_trim_filename(tb.tb_frame.f_code.co_filename),
tb.tb_lineno,
tb.tb_frame.f_code.co_name,
)
)
tb = tb.tb_next
tele.log_exception(error_type, trace_to_log)
raise
return result
return exception_logging_wrapper
def _trim_filename(filename: str) -> str:
return filename.split("/")[-1]
# Single global telemetry object
tele = Telemetry()