Repository navigation
Expand file tree
/
Copy pathtelemetry.py
More file actions
354 lines (283 loc) · 13.2 KB
/
Copy pathtelemetry.py
File metadata and controls
354 lines (283 loc) · 13.2 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""Telemetry engine classes."""
import json
import os
import logging
_LOGGER = logging.getLogger(__name__)
from splitio.storage.inmemmory import InMemoryTelemetryStorage
from splitio.models.telemetry import CounterConstants, UpdateFromSSE
class TelemetryStorageProducer(object):
"""Telemetry storage producer class."""
def __init__(self, telemetry_storage):
"""Initialize all producer classes."""
self._telemetry_init_producer = TelemetryInitProducer(telemetry_storage)
self._telemetry_evaluation_producer = TelemetryEvaluationProducer(telemetry_storage)
self._telemetry_runtime_producer = TelemetryRuntimeProducer(telemetry_storage)
def get_telemetry_init_producer(self):
"""get init producer instance."""
return self._telemetry_init_producer
def get_telemetry_evaluation_producer(self):
"""get evaluation producer instance."""
return self._telemetry_evaluation_producer
def get_telemetry_runtime_producer(self):
"""get runtime producer instance."""
return self._telemetry_runtime_producer
class TelemetryInitProducer(object):
"""Telemetry init producer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def record_config(self, config, extra_config, total_flag_sets=0, invalid_flag_sets=0):
"""Record configurations."""
self._telemetry_storage.record_config(config, extra_config, total_flag_sets, invalid_flag_sets)
current_app, app_worker_id = self._get_app_worker_id()
if current_app is not None:
self.add_config_tag("initilization:" + current_app)
self.add_config_tag("worker:#" + app_worker_id)
def record_ready_time(self, ready_time):
"""Record ready time."""
self._telemetry_storage.record_ready_time(ready_time)
def record_flag_sets(self, flag_sets):
"""Record flag sets."""
self._telemetry_storage.record_flag_sets(flag_sets)
def record_invalid_flag_sets(self, flag_sets):
"""Record invalid flag sets."""
self._telemetry_storage.record_invalid_flag_sets(flag_sets)
def record_bur_time_out(self):
"""Record block until ready timeout."""
self._telemetry_storage.record_bur_time_out()
def record_not_ready_usage(self):
"""record non-ready usage."""
self._telemetry_storage.record_not_ready_usage()
def record_active_and_redundant_factories(self, active_factory_count, redundant_factory_count):
self._telemetry_storage.record_active_and_redundant_factories(active_factory_count, redundant_factory_count)
def add_config_tag(self, tag):
"""Record tag string."""
self._telemetry_storage.add_config_tag(tag)
def _get_app_worker_id(self):
try:
import uwsgi
return "uwsgi", str(uwsgi.worker_id())
except ModuleNotFoundError:
_LOGGER.debug("NO uwsgi")
pass
if 'gunicorn' in os.environ.get("SERVER_SOFTWARE", ""):
return "gunicorn", str(os.getpid())
else:
return None, None
class TelemetryEvaluationProducer(object):
"""Telemetry evaluation producer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def record_latency(self, method, latency):
"""Record method latency time."""
self._telemetry_storage.record_latency(method, latency)
def record_exception(self, method):
"""Record method exception time."""
self._telemetry_storage.record_exception(method)
class TelemetryRuntimeProducer(object):
"""Telemetry runtime producer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def add_tag(self, tag):
"""Record tag string."""
self._telemetry_storage.add_tag(tag)
def record_impression_stats(self, data_type, count):
"""Record impressions stats."""
self._telemetry_storage.record_impression_stats(data_type, count)
def record_event_stats(self, data_type, count):
"""Record events stats."""
self._telemetry_storage.record_event_stats(data_type, count)
def record_successful_sync(self, resource, time):
"""Record successful sync."""
self._telemetry_storage.record_successful_sync(resource, time)
def record_sync_error(self, resource, status):
"""Record sync error."""
self._telemetry_storage.record_sync_error(resource, status)
def record_sync_latency(self, resource, latency):
"""Record latency time."""
self._telemetry_storage.record_sync_latency(resource, latency)
def record_auth_rejections(self):
"""Record auth rejection."""
self._telemetry_storage.record_auth_rejections()
def record_token_refreshes(self):
"""Record sse token refresh."""
self._telemetry_storage.record_token_refreshes()
def record_streaming_event(self, streaming_event):
"""Record incoming streaming event."""
self._telemetry_storage.record_streaming_event(streaming_event)
def record_session_length(self, session):
"""Record session length."""
self._telemetry_storage.record_session_length(session)
def record_update_from_sse(self, event):
"""Record update from sse."""
self._telemetry_storage.record_update_from_sse(event)
class TelemetryStorageConsumer(object):
"""Telemetry storage consumer class."""
def __init__(self, telemetry_storage):
"""Initialize all consumer classes."""
self._telemetry_init_consumer = TelemetryInitConsumer(telemetry_storage)
self._telemetry_evaluation_consumer = TelemetryEvaluationConsumer(telemetry_storage)
self._telemetry_runtime_consumer = TelemetryRuntimeConsumer(telemetry_storage)
def get_telemetry_init_consumer(self):
"""Get telemetry init instance"""
return self._telemetry_init_consumer
def get_telemetry_evaluation_consumer(self):
"""Get telemetry evaluation instance"""
return self._telemetry_evaluation_consumer
def get_telemetry_runtime_consumer(self):
"""Get telemetry runtime instance"""
return self._telemetry_runtime_consumer
class TelemetryInitConsumer(object):
"""Telemetry init consumer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def get_bur_time_outs(self):
"""Get block until ready timeout."""
return self._telemetry_storage.get_bur_time_outs()
def get_not_ready_usage(self):
"""Get none-ready usage."""
return self._telemetry_storage.get_not_ready_usage()
def get_config_stats(self):
"""Get config stats."""
config_stats = self._telemetry_storage.get_config_stats()
config_stats.update({'t': self.pop_config_tags()})
return config_stats
def get_config_stats_to_json(self):
"""Get config stats in json."""
return json.dumps(self._telemetry_storage.get_config_stats())
def pop_config_tags(self):
"""Get and reset tags."""
return self._telemetry_storage.pop_config_tags()
class TelemetryEvaluationConsumer(object):
"""Telemetry evaluation consumer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def pop_exceptions(self):
"""Get and reset method exceptions."""
return self._telemetry_storage.pop_exceptions()
def pop_latencies(self):
"""Get and reset eval latencies."""
return self._telemetry_storage.pop_latencies()
def pop_formatted_stats(self):
"""
Get formatted and reset stats.
:returns: formatted stats
:rtype: Dict
"""
exceptions = self.pop_exceptions()['methodExceptions']
latencies = self.pop_latencies()['methodLatencies']
return {
'mE': {
't': exceptions['treatment'],
'ts': exceptions['treatments'],
'tc': exceptions['treatment_with_config'],
'tcs': exceptions['treatments_with_config'],
'tf': exceptions['treatments_by_flag_set'],
'tfs': exceptions['treatments_by_flag_sets'],
'tcf': exceptions['treatments_with_config_by_flag_set'],
'tcfs': exceptions['treatments_with_config_by_flag_sets'],
'tr': exceptions['track']
},
'mL': {
't': latencies['treatment'],
'ts': latencies['treatments'],
'tc': latencies['treatment_with_config'],
'tcs': latencies['treatments_with_config'],
'tf': latencies['treatments_by_flag_set'],
'tfs': latencies['treatments_by_flag_sets'],
'tcf': latencies['treatments_with_config_by_flag_set'],
'tcfs': latencies['treatments_with_config_by_flag_sets'],
'tr': latencies['track']
},
}
class TelemetryRuntimeConsumer(object):
"""Telemetry runtime consumer class."""
def __init__(self, telemetry_storage):
"""Constructor."""
self._telemetry_storage = telemetry_storage
def get_impressions_stats(self, type):
"""Get impressions stats"""
return self._telemetry_storage.get_impressions_stats(type)
def get_events_stats(self, type):
"""Get events stats"""
return self._telemetry_storage.get_events_stats(type)
def get_last_synchronization(self):
"""Get last sync"""
return self._telemetry_storage.get_last_synchronization()['lastSynchronizations']
def pop_tags(self):
"""Get and reset tags."""
return self._telemetry_storage.pop_tags()
def pop_http_errors(self):
"""Get and reset http errors."""
return self._telemetry_storage.pop_http_errors()
def pop_http_latencies(self):
"""Get and reset http latencies."""
return self._telemetry_storage.pop_http_latencies()
def pop_auth_rejections(self):
"""Get and reset auth rejections."""
return self._telemetry_storage.pop_auth_rejections()
def pop_token_refreshes(self):
"""Get and reset token refreshes."""
return self._telemetry_storage.pop_token_refreshes()
def pop_streaming_events(self):
"""Get and reset streaming events."""
return self._telemetry_storage.pop_streaming_events()
def pop_update_from_sse(self, event):
"""Get and reset update from sse."""
return self._telemetry_storage.pop_update_from_sse(event)
def get_session_length(self):
"""Get session length"""
return self._telemetry_storage.get_session_length()
def pop_formatted_stats(self):
"""
Get formatted and reset stats.
:returns: formatted stats
:rtype: Dict
"""
last_synchronization = self.get_last_synchronization()
http_errors = self.pop_http_errors()['httpErrors']
http_latencies = self.pop_http_latencies()['httpLatencies']
return {
'iQ': self.get_impressions_stats(CounterConstants.IMPRESSIONS_QUEUED),
'iDe': self.get_impressions_stats(CounterConstants.IMPRESSIONS_DEDUPED),
'iDr': self.get_impressions_stats(CounterConstants.IMPRESSIONS_DROPPED),
'eQ': self.get_events_stats(CounterConstants.EVENTS_QUEUED),
'eD': self.get_events_stats(CounterConstants.EVENTS_DROPPED),
'ufs': {event.value: self.pop_update_from_sse(event) for event in UpdateFromSSE},
'lS': {'sp': last_synchronization['split'],
'se': last_synchronization['segment'],
'im': last_synchronization['impression'],
'ic': last_synchronization['impressionCount'],
'ev': last_synchronization['event'],
'te': last_synchronization['telemetry'],
'to': last_synchronization['token']
},
't': self.pop_tags(),
'hE': {'sp': http_errors['split'],
'se': http_errors['segment'],
'im': http_errors['impression'],
'ic': http_errors['impressionCount'],
'ev': http_errors['event'],
'te': http_errors['telemetry'],
'to': http_errors['token']
},
'hL': {'sp': http_latencies['split'],
'se': http_latencies['segment'],
'im': http_latencies['impression'],
'ic': http_latencies['impressionCount'],
'ev': http_latencies['event'],
'te': http_latencies['telemetry'],
'to': http_latencies['token']
},
'aR': self.pop_auth_rejections(),
'tR': self.pop_token_refreshes(),
'sE': [{'e': event['e'],
'd': event['d'],
't': event['t']
} for event in self.pop_streaming_events()['streamingEvents']],
'sL': self.get_session_length()
}