-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtelemetry.py
More file actions
1025 lines (890 loc) · 36 KB
/
telemetry.py
File metadata and controls
1025 lines (890 loc) · 36 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""SDK Telemetry helpers."""
from bisect import bisect_left
import threading
import os
from enum import Enum
from splitio.engine.impressions import ImpressionsMode
BUCKETS = (
1000, 1500, 2250, 3375, 5063,
7594, 11391, 17086, 25629, 38443,
57665, 86498, 129746, 194620, 291929,
437894, 656841, 985261, 1477892, 2216838,
3325257, 4987885, 7481828
)
MAX_LATENCY = 7481828
MAX_LATENCY_BUCKET_COUNT = 23
MAX_STREAMING_EVENTS = 20
MAX_TAGS = 10
class CounterConstants(Enum):
"""Impressions and events counters constants"""
IMPRESSIONS_QUEUED = 'impressionsQueued'
IMPRESSIONS_DEDUPED = 'impressionsDeduped'
IMPRESSIONS_DROPPED = 'impressionsDropped'
EVENTS_QUEUED = 'eventsQueued'
EVENTS_DROPPED = 'eventsDropped'
class _ConfigParams(Enum):
"""Config parameters constants"""
SPLITS_REFRESH_RATE = 'featuresRefreshRate'
SEGMENTS_REFRESH_RATE = 'segmentsRefreshRate'
IMPRESSIONS_REFRESH_RATE = 'impressionsRefreshRate'
EVENTS_REFRESH_RATE = 'eventsPushRate'
TELEMETRY_REFRESH_RATE = 'metricsRefreshRate'
OPERATION_MODE = 'operationMode'
STORAGE_TYPE = 'storageType'
STREAMING_ENABLED = 'streamingEnabled'
IMPRESSIONS_QUEUE_SIZE = 'impressionsQueueSize'
EVENTS_QUEUE_SIZE = 'eventsQueueSize'
IMPRESSIONS_MODE = 'impressionsMode'
IMPRESSIONS_LISTENER = 'impressionListener'
class _ExtraConfig(Enum):
"""Extra config constants"""
ACTIVE_FACTORY_COUNT = 'activeFactoryCount'
REDUNDANT_FACTORY_COUNT = 'redundantFactoryCount'
BLOCK_UNTIL_READY_TIMEOUT = 'blockUntilReadyTimeout'
NOT_READY = 'notReady'
TIME_UNTIL_READY = 'timeUntilReady'
REFRESH_RATE = 'refreshRate'
HTTP_PROXY = 'httpProxy'
HTTPS_PROXY_ENV = 'HTTPS_PROXY'
class _ApiURLs(Enum):
"""Api URL constants"""
SDK_URL = 'sdk_url'
EVENTS_URL = 'events_url'
AUTH_URL = 'auth_url'
STREAMING_URL = 'streaming_url'
TELEMETRY_URL = 'telemetry_url'
URL_OVERRIDE = 'urlOverride'
class HTTPExceptionsAndLatencies(Enum):
"""Sync exceptions and latencies constants"""
HTTP_ERRORS = 'httpErrors'
HTTP_LATENCIES = 'httpLatencies'
SPLIT = 'split'
SEGMENT = 'segment'
IMPRESSION = 'impression'
IMPRESSION_COUNT = 'impressionCount'
EVENT = 'event'
TELEMETRY = 'telemetry'
TOKEN = 'token'
class MethodExceptionsAndLatencies(Enum):
"""Method exceptions and latencies constants"""
METHOD_LATENCIES = 'methodLatencies'
METHOD_EXCEPTIONS = 'methodExceptions'
TREATMENT = 'treatment'
TREATMENTS = 'treatments'
TREATMENT_WITH_CONFIG = 'treatment_with_config'
TREATMENTS_WITH_CONFIG = 'treatments_with_config'
TREATMENTS_BY_FLAG_SET = 'treatments_by_flag_set'
TREATMENTS_BY_FLAG_SETS = 'treatments_by_flag_sets'
TREATMENTS_WITH_CONFIG_BY_FLAG_SET = 'treatments_with_config_by_flag_set'
TREATMENTS_WITH_CONFIG_BY_FLAG_SETS = 'treatments_with_config_by_flag_sets'
TRACK = 'track'
class _LastSynchronizationConstants(Enum):
"""Last sync constants"""
LAST_SYNCHRONIZATIONS = 'lastSynchronizations'
class SSEStreamingStatus(Enum):
"""SSE streaming status enums"""
ENABLED = 0
DISABLED = 1
PAUSED = 2
class SSEConnectionError(Enum):
"""SSE Connection Error enums"""
REQUESTED = 0
NON_REQUESTED = 1
class SSESyncMode(Enum):
"""SSE sync mode enums"""
STREAMING = 0
POLLING = 1
class _StreamingEventsConstant(Enum):
"""Storage types constant"""
STREAMING_EVENTS = 'streamingEvents'
class StreamingEventTypes(Enum):
"""Streaming event types constants"""
CONNECTION_ESTABLISHED = 0
OCCUPANCY_PRI = 10
OCCUPANCY_SEC = 20
STREAMING_STATUS = 30
SSE_CONNECTION_ERROR = 40
TOKEN_REFRESH = 50
ABLY_ERROR = 60
SYNC_MODE_UPDATE = 70
class StorageType(Enum):
"""Storage types constants"""
MEMORY = 'memory'
REDIS = 'redis'
PLUGGABLE = 'pluggable'
class OperationMode(Enum):
"""Storage modes constants"""
STANDALONE = 'standalone'
CONSUMER = 'consumer'
PARTIAL_CONSUMER = 'partial_consumer'
class UpdateFromSSE(Enum):
"""Update from sse constants"""
SPLIT_UPDATE = 'sp'
def get_latency_bucket_index(micros):
"""
Find the bucket index for a measured latency.
:param micros: Measured latency in microseconds
:type micros: int
:return: Bucket index for the given latency
:rtype: int
"""
if micros > MAX_LATENCY:
return len(BUCKETS) - 1
return bisect_left(BUCKETS, micros)
class MethodLatencies(object):
"""
Method Latency class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._treatment = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatment_with_config = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments_with_config = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments_by_flag_set = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments_by_flag_sets = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments_with_config_by_flag_set = [0] * MAX_LATENCY_BUCKET_COUNT
self._treatments_with_config_by_flag_sets = [0] * MAX_LATENCY_BUCKET_COUNT
self._track = [0] * MAX_LATENCY_BUCKET_COUNT
def add_latency(self, method, latency):
"""
Add Latency method
:param method: passed method name
:type method: str
:param latency: amount of latency in microseconds
:type latency: int
"""
latency_bucket = get_latency_bucket_index(latency)
with self._lock:
if method == MethodExceptionsAndLatencies.TREATMENT:
self._treatment[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS:
self._treatments[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG:
self._treatment_with_config[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG:
self._treatments_with_config[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET:
self._treatments_by_flag_set[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS:
self._treatments_by_flag_sets[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET:
self._treatments_with_config_by_flag_set[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS:
self._treatments_with_config_by_flag_sets[latency_bucket] += 1
elif method == MethodExceptionsAndLatencies.TRACK:
self._track[latency_bucket] += 1
else:
return
def pop_all(self):
"""
Pop all latencies
:return: Dictonary of latencies
:rtype: dict
"""
with self._lock:
latencies = {MethodExceptionsAndLatencies.METHOD_LATENCIES.value: {
MethodExceptionsAndLatencies.TREATMENT.value: self._treatment,
MethodExceptionsAndLatencies.TREATMENTS.value: self._treatments,
MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG.value: self._treatment_with_config,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG.value: self._treatments_with_config,
MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET.value: self._treatments_by_flag_set,
MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS.value: self._treatments_by_flag_sets,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET.value: self._treatments_with_config_by_flag_set,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS.value: self._treatments_with_config_by_flag_sets,
MethodExceptionsAndLatencies.TRACK.value: self._track
}
}
self._reset_all()
return latencies
class HTTPLatencies(object):
"""
HTTP Latency class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._split = [0] * MAX_LATENCY_BUCKET_COUNT
self._segment = [0] * MAX_LATENCY_BUCKET_COUNT
self._impression = [0] * MAX_LATENCY_BUCKET_COUNT
self._impression_count = [0] * MAX_LATENCY_BUCKET_COUNT
self._event = [0] * MAX_LATENCY_BUCKET_COUNT
self._telemetry = [0] * MAX_LATENCY_BUCKET_COUNT
self._token = [0] * MAX_LATENCY_BUCKET_COUNT
def add_latency(self, resource, latency):
"""
Add Latency method
:param resource: passed resource name
:type resource: str
:param latency: amount of latency in microseconds
:type latency: int
"""
latency_bucket = get_latency_bucket_index(latency)
with self._lock:
if resource == HTTPExceptionsAndLatencies.SPLIT:
self._split[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.SEGMENT:
self._segment[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.IMPRESSION:
self._impression[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.IMPRESSION_COUNT:
self._impression_count[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.EVENT:
self._event[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.TELEMETRY:
self._telemetry[latency_bucket] += 1
elif resource == HTTPExceptionsAndLatencies.TOKEN:
self._token[latency_bucket] += 1
else:
return
def pop_all(self):
"""
Pop all latencies
:return: Dictonary of latencies
:rtype: dict
"""
with self._lock:
latencies = {HTTPExceptionsAndLatencies.HTTP_LATENCIES.value: {HTTPExceptionsAndLatencies.SPLIT.value: self._split, HTTPExceptionsAndLatencies.SEGMENT.value: self._segment, HTTPExceptionsAndLatencies.IMPRESSION.value: self._impression,
HTTPExceptionsAndLatencies.IMPRESSION_COUNT.value: self._impression_count, HTTPExceptionsAndLatencies.EVENT.value: self._event,
HTTPExceptionsAndLatencies.TELEMETRY.value: self._telemetry, HTTPExceptionsAndLatencies.TOKEN.value: self._token}
}
self._reset_all()
return latencies
class MethodExceptions(object):
"""
Method exceptions class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._treatment = 0
self._treatments = 0
self._treatment_with_config = 0
self._treatments_with_config = 0
self._treatments_by_flag_set = 0
self._treatments_by_flag_sets = 0
self._treatments_with_config_by_flag_set = 0
self._treatments_with_config_by_flag_sets = 0
self._track = 0
def add_exception(self, method):
"""
Add exceptions method
:param method: passed method name
:type method: str
"""
with self._lock:
if method == MethodExceptionsAndLatencies.TREATMENT:
self._treatment += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS:
self._treatments += 1
elif method == MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG:
self._treatment_with_config += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG:
self._treatments_with_config += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET:
self._treatments_by_flag_set += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS:
self._treatments_by_flag_sets += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET:
self._treatments_with_config_by_flag_set += 1
elif method == MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS:
self._treatments_with_config_by_flag_sets += 1
elif method == MethodExceptionsAndLatencies.TRACK:
self._track += 1
else:
return
def pop_all(self):
"""
Pop all exceptions
:return: Dictonary of exceptions
:rtype: dict
"""
with self._lock:
exceptions = {MethodExceptionsAndLatencies.METHOD_EXCEPTIONS.value: {
MethodExceptionsAndLatencies.TREATMENT.value: self._treatment,
MethodExceptionsAndLatencies.TREATMENTS.value: self._treatments,
MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG.value: self._treatment_with_config,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG.value: self._treatments_with_config,
MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SET.value: self._treatments_by_flag_set,
MethodExceptionsAndLatencies.TREATMENTS_BY_FLAG_SETS.value: self._treatments_by_flag_sets,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SET.value: self._treatments_with_config_by_flag_set,
MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS.value: self._treatments_with_config_by_flag_sets,
MethodExceptionsAndLatencies.TRACK.value: self._track
}
}
self._reset_all()
return exceptions
class LastSynchronization(object):
"""
Last Synchronization info class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._split = 0
self._segment = 0
self._impression = 0
self._impression_count = 0
self._event = 0
self._telemetry = 0
self._token = 0
def add_latency(self, resource, sync_time):
"""
Add Latency method
:param resource: passed resource name
:type resource: str
:param sync_time: amount of last sync time
:type sync_time: int
"""
with self._lock:
if resource == HTTPExceptionsAndLatencies.SPLIT:
self._split = sync_time
elif resource == HTTPExceptionsAndLatencies.SEGMENT:
self._segment = sync_time
elif resource == HTTPExceptionsAndLatencies.IMPRESSION:
self._impression = sync_time
elif resource == HTTPExceptionsAndLatencies.IMPRESSION_COUNT:
self._impression_count = sync_time
elif resource == HTTPExceptionsAndLatencies.EVENT:
self._event = sync_time
elif resource == HTTPExceptionsAndLatencies.TELEMETRY:
self._telemetry = sync_time
elif resource == HTTPExceptionsAndLatencies.TOKEN:
self._token = sync_time
else:
return
def get_all(self):
"""
get all exceptions
:return: Dictonary of latencies
:rtype: dict
"""
with self._lock:
return {_LastSynchronizationConstants.LAST_SYNCHRONIZATIONS.value: {HTTPExceptionsAndLatencies.SPLIT.value: self._split, HTTPExceptionsAndLatencies.SEGMENT.value: self._segment, HTTPExceptionsAndLatencies.IMPRESSION.value: self._impression,
HTTPExceptionsAndLatencies.IMPRESSION_COUNT.value: self._impression_count, HTTPExceptionsAndLatencies.EVENT.value: self._event,
HTTPExceptionsAndLatencies.TELEMETRY.value: self._telemetry, HTTPExceptionsAndLatencies.TOKEN.value: self._token}
}
class HTTPErrors(object):
"""
Last Synchronization info class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._split = {}
self._segment = {}
self._impression = {}
self._impression_count = {}
self._event = {}
self._telemetry = {}
self._token = {}
def add_error(self, resource, status):
"""
Add Latency method
:param resource: passed resource name
:type resource: str
:param status: http error code
:type status: str
"""
status = str(status)
with self._lock:
if resource == HTTPExceptionsAndLatencies.SPLIT:
if status not in self._split:
self._split[status] = 0
self._split[status] += 1
elif resource == HTTPExceptionsAndLatencies.SEGMENT:
if status not in self._segment:
self._segment[status] = 0
self._segment[status] += 1
elif resource == HTTPExceptionsAndLatencies.IMPRESSION:
if status not in self._impression:
self._impression[status] = 0
self._impression[status] += 1
elif resource == HTTPExceptionsAndLatencies.IMPRESSION_COUNT:
if status not in self._impression_count:
self._impression_count[status] = 0
self._impression_count[status] += 1
elif resource == HTTPExceptionsAndLatencies.EVENT:
if status not in self._event:
self._event[status] = 0
self._event[status] += 1
elif resource == HTTPExceptionsAndLatencies.TELEMETRY:
if status not in self._telemetry:
self._telemetry[status] = 0
self._telemetry[status] += 1
elif resource == HTTPExceptionsAndLatencies.TOKEN:
if status not in self._token:
self._token[status] = 0
self._token[status] += 1
else:
return
def pop_all(self):
"""
Pop all errors
:return: Dictonary of exceptions
:rtype: dict
"""
with self._lock:
http_errors = {HTTPExceptionsAndLatencies.HTTP_ERRORS.value: {HTTPExceptionsAndLatencies.SPLIT.value: self._split, HTTPExceptionsAndLatencies.SEGMENT.value: self._segment, HTTPExceptionsAndLatencies.IMPRESSION.value: self._impression,
HTTPExceptionsAndLatencies.IMPRESSION_COUNT.value: self._impression_count, HTTPExceptionsAndLatencies.EVENT.value: self._event,
HTTPExceptionsAndLatencies.TELEMETRY.value: self._telemetry, HTTPExceptionsAndLatencies.TOKEN.value: self._token}
}
self._reset_all()
return http_errors
class TelemetryCounters(object):
"""
Method exceptions class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._impressions_queued = 0
self._impressions_deduped = 0
self._impressions_dropped = 0
self._events_queued = 0
self._events_dropped = 0
self._auth_rejections = 0
self._token_refreshes = 0
self._session_length = 0
self._update_from_sse = {}
def record_impressions_value(self, resource, value):
"""
Append to the resource value
:param resource: passed resource name
:type resource: str
:param value: value to be appended
:type value: int
"""
with self._lock:
if resource == CounterConstants.IMPRESSIONS_QUEUED:
self._impressions_queued += value
elif resource == CounterConstants.IMPRESSIONS_DEDUPED:
self._impressions_deduped += value
elif resource == CounterConstants.IMPRESSIONS_DROPPED:
self._impressions_dropped += value
else:
return
def record_events_value(self, resource, value):
"""
Append to the resource value
:param resource: passed resource name
:type resource: str
:param value: value to be appended
:type value: int
"""
with self._lock:
if resource == CounterConstants.EVENTS_QUEUED:
self._events_queued += value
elif resource == CounterConstants.EVENTS_DROPPED:
self._events_dropped += value
else:
return
def record_update_from_sse(self, event):
"""
Increment the update from sse resource by one.
"""
with self._lock:
if event.value not in self._update_from_sse:
self._update_from_sse[event.value] = 0
self._update_from_sse[event.value] += 1
def record_auth_rejections(self):
"""
Increment the auth rejection resource by one.
"""
with self._lock:
self._auth_rejections += 1
def record_token_refreshes(self):
"""
Increment the token refreshes resource by one.
"""
with self._lock:
self._token_refreshes += 1
def record_session_length(self, session):
"""
Set the session length value
:param session: value to be set
:type session: int
"""
with self._lock:
self._session_length = session
def get_counter_stats(self, resource):
"""
Get resource counter value
:param resource: passed resource name
:type resource: str
:return: resource value
:rtype: int
"""
with self._lock:
if resource == CounterConstants.IMPRESSIONS_QUEUED:
return self._impressions_queued
elif resource == CounterConstants.IMPRESSIONS_DEDUPED:
return self._impressions_deduped
elif resource == CounterConstants.IMPRESSIONS_DROPPED:
return self._impressions_dropped
elif resource == CounterConstants.EVENTS_QUEUED:
return self._events_queued
elif resource == CounterConstants.EVENTS_DROPPED:
return self._events_dropped
else:
return 0
def get_session_length(self):
"""
Get session length
:return: session length value
:rtype: int
"""
with self._lock:
return self._session_length
def pop_auth_rejections(self):
"""
Pop auth rejections
:return: auth rejections value
:rtype: int
"""
with self._lock:
auth_rejections = self._auth_rejections
self._auth_rejections = 0
return auth_rejections
def pop_token_refreshes(self):
"""
Pop token refreshes
:return: token refreshes value
:rtype: int
"""
with self._lock:
token_refreshes = self._token_refreshes
self._token_refreshes = 0
return token_refreshes
def pop_update_from_sse(self, event):
"""
Pop update from sse
:return: update from sse value
:rtype: int
"""
with self._lock:
if self._update_from_sse.get(event.value) is None:
return 0
update_from_sse = self._update_from_sse[event.value]
self._update_from_sse[event.value] = 0
return update_from_sse
class StreamingEvent(object):
"""
Streaming event class
"""
def __init__(self, streaming_event):
"""
Constructor
:param streaming_event: Streaming event tuple: ('type', 'data', 'time')
:type streaming_event: dict
"""
self._type = streaming_event[0].value
self._data = streaming_event[1]
self._time = streaming_event[2]
@property
def type(self):
"""
Get streaming event type
:return: streaming event type
:rtype: str
"""
return self._type
@property
def data(self):
"""
Get streaming event data
:return: streaming event data
:rtype: str
"""
return self._data
@property
def time(self):
"""
Get streaming event time
:return: streaming event time
:rtype: int
"""
return self._time
class StreamingEvents(object):
"""
Streaming events class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
with self._lock:
self._streaming_events = []
def record_streaming_event(self, streaming_event):
"""
Record new streaming event
:param streaming_event: Streaming event dict:
{'type': string, 'data': string, 'time': string}
:type streaming_event: dict
"""
if not StreamingEvent(streaming_event):
return
with self._lock:
if len(self._streaming_events) < MAX_STREAMING_EVENTS:
self._streaming_events.append(StreamingEvent(streaming_event))
def pop_streaming_events(self):
"""
Get and reset streaming events
:return: streaming events dict
:rtype: dict
"""
with self._lock:
streaming_events = self._streaming_events
self._streaming_events = []
return {_StreamingEventsConstant.STREAMING_EVENTS.value: [{'e': streaming_event.type, 'd': streaming_event.data,
't': streaming_event.time} for streaming_event in streaming_events]}
class TelemetryConfig(object):
"""
Telemetry init config class
"""
def __init__(self):
"""Constructor"""
self._lock = threading.RLock()
self._reset_all()
def _reset_all(self):
"""Reset variables"""
with self._lock:
self._block_until_ready_timeout = 0
self._not_ready = 0
self._time_until_ready = 0
self._operation_mode = None
self._storage_type = None
self._streaming_enabled = None
self._refresh_rate = {_ConfigParams.SPLITS_REFRESH_RATE.value: 0, _ConfigParams.SEGMENTS_REFRESH_RATE.value: 0,
_ConfigParams.IMPRESSIONS_REFRESH_RATE.value: 0, _ConfigParams.EVENTS_REFRESH_RATE.value: 0, _ConfigParams.TELEMETRY_REFRESH_RATE.value: 0}
self._url_override = {_ApiURLs.SDK_URL.value: False, _ApiURLs.EVENTS_URL.value: False, _ApiURLs.AUTH_URL.value: False,
_ApiURLs.STREAMING_URL.value: False, _ApiURLs.TELEMETRY_URL.value: False}
self._impressions_queue_size = 0
self._events_queue_size = 0
self._impressions_mode = None
self._impression_listener = False
self._http_proxy = None
self._active_factory_count = 0
self._redundant_factory_count = 0
self._flag_sets = 0
self._flag_sets_invalid = 0
def record_config(self, config, extra_config, total_flag_sets, invalid_flag_sets):
"""
Record configurations.
:param config: config dict: {
'operationMode': int, 'storageType': string, 'streamingEnabled': boolean,
'refreshRate' : {
'featuresRefreshRate': int,
'segmentsRefreshRate': int,
'impressionsRefreshRate': int,
'eventsPushRate': int,
'metricsRefreshRate': int
}
'urlOverride' : {
'sdk_url': boolean, 'events_url': boolean, 'auth_url': boolean,
'streaming_url': boolean, 'telemetry_url': boolean, }
},
'impressionsQueueSize': int, 'eventsQueueSize': int, 'impressionsMode': string,
'impressionsListener': boolean, 'activeFactoryCount': int, 'redundantFactoryCount': int
}
:type config: dict
"""
with self._lock:
self._operation_mode = self._get_operation_mode(config[_ConfigParams.OPERATION_MODE.value])
self._storage_type = self._get_storage_type(config[_ConfigParams.OPERATION_MODE.value], config[_ConfigParams.STORAGE_TYPE.value])
self._streaming_enabled = config[_ConfigParams.STREAMING_ENABLED.value]
self._refresh_rate = self._get_refresh_rates(config)
self._url_override = self._get_url_overrides(extra_config)
self._impressions_queue_size = config[_ConfigParams.IMPRESSIONS_QUEUE_SIZE.value]
self._events_queue_size = config[_ConfigParams.EVENTS_QUEUE_SIZE.value]
self._impressions_mode = self._get_impressions_mode(config[_ConfigParams.IMPRESSIONS_MODE.value])
self._impression_listener = True if config[_ConfigParams.IMPRESSIONS_LISTENER.value] is not None else False
self._http_proxy = self._check_if_proxy_detected()
self._flag_sets = total_flag_sets
self._flag_sets_invalid = invalid_flag_sets
def record_active_and_redundant_factories(self, active_factory_count, redundant_factory_count):
with self._lock:
self._active_factory_count = active_factory_count
self._redundant_factory_count = redundant_factory_count
def record_ready_time(self, ready_time):
"""
Record ready time.
:param ready_time: SDK ready time
:type ready_time: int
"""
with self._lock:
self._time_until_ready = ready_time
def record_bur_time_out(self):
"""
Record block until ready timeout count
"""
with self._lock:
self._block_until_ready_timeout += 1
def record_not_ready_usage(self):
"""
record non-ready usage count
"""
with self._lock:
self._not_ready += 1
def get_bur_time_outs(self):
"""
Get block until ready timeout.
:return: block until ready timeouts count
:rtype: int
"""
with self._lock:
return self._block_until_ready_timeout
def get_non_ready_usage(self):
"""
Get non-ready usage.
:return: non-ready usage count
:rtype: int
"""
with self._lock:
return self._not_ready
def get_stats(self):
"""
Get config stats.
:return: dict of all config stats.
:rtype: dict
"""
with self._lock:
return {
'bT': self._block_until_ready_timeout,
'nR': self._not_ready,
'tR': self._time_until_ready,
'oM': self._operation_mode,
'sT': self._storage_type,
'sE': self._streaming_enabled,
'rR': {'sp': self._refresh_rate[_ConfigParams.SPLITS_REFRESH_RATE.value],
'se': self._refresh_rate[_ConfigParams.SEGMENTS_REFRESH_RATE.value],
'im': self._refresh_rate[_ConfigParams.IMPRESSIONS_REFRESH_RATE.value],
'ev': self._refresh_rate[_ConfigParams.EVENTS_REFRESH_RATE.value],
'te': self._refresh_rate[_ConfigParams.TELEMETRY_REFRESH_RATE.value]},
'uO': {'s': self._url_override[_ApiURLs.SDK_URL.value],
'e': self._url_override[_ApiURLs.EVENTS_URL.value],
'a': self._url_override[_ApiURLs.AUTH_URL.value],
'st': self._url_override[_ApiURLs.STREAMING_URL.value],
't': self._url_override[_ApiURLs.TELEMETRY_URL.value]},
'iQ': self._impressions_queue_size,
'eQ': self._events_queue_size,
'iM': self._impressions_mode,
'iL': self._impression_listener,
'hp': self._http_proxy,
'aF': self._active_factory_count,
'rF': self._redundant_factory_count,
'fsT': self._flag_sets,
'fsI': self._flag_sets_invalid
}
def _get_operation_mode(self, op_mode):
"""
Get formatted operation mode
:param op_mode: config operation mode
:type config: str
:return: operation mode
:rtype: int
"""
with self._lock:
if op_mode == OperationMode.STANDALONE.value:
return 0
elif op_mode == OperationMode.CONSUMER.value:
return 1
else:
return 2
def _get_storage_type(self, op_mode, st_type):
"""
Get storage type from operation mode
:param op_mode: config operation mode
:type config: str
:return: storage type
:rtype: str
"""
with self._lock:
if op_mode == OperationMode.STANDALONE.value:
return StorageType.MEMORY.value
elif st_type == StorageType.REDIS.value:
return StorageType.REDIS.value
else:
return StorageType.PLUGGABLE.value
def _get_refresh_rates(self, config):
"""
Get refresh rates within config dict
:param config: config dict
:type config: dict
:return: refresh rates
:rtype: RefreshRates object
"""
with self._lock:
return {
_ConfigParams.SPLITS_REFRESH_RATE.value: config[_ConfigParams.SPLITS_REFRESH_RATE.value],
_ConfigParams.SEGMENTS_REFRESH_RATE.value: config[_ConfigParams.SEGMENTS_REFRESH_RATE.value],
_ConfigParams.IMPRESSIONS_REFRESH_RATE.value: config[_ConfigParams.IMPRESSIONS_REFRESH_RATE.value],
_ConfigParams.EVENTS_REFRESH_RATE.value: config[_ConfigParams.EVENTS_REFRESH_RATE.value],
_ConfigParams.TELEMETRY_REFRESH_RATE.value: config[_ConfigParams.TELEMETRY_REFRESH_RATE.value]
}
def _get_url_overrides(self, config):
"""
Get URL override within the config dict.
:param config: config dict
:type config: dict
:return: URL overrides dict
:rtype: URLOverrides object
"""
with self._lock:
return {
_ApiURLs.SDK_URL.value: True if _ApiURLs.SDK_URL.value in config else False,
_ApiURLs.EVENTS_URL.value: True if _ApiURLs.EVENTS_URL.value in config else False,
_ApiURLs.AUTH_URL.value: True if _ApiURLs.AUTH_URL.value in config else False,
_ApiURLs.STREAMING_URL.value: True if _ApiURLs.STREAMING_URL.value in config else False,
_ApiURLs.TELEMETRY_URL.value: True if _ApiURLs.TELEMETRY_URL.value in config else False
}
def _get_impressions_mode(self, imp_mode):
"""
Get impressions mode from operation mode
:param op_mode: config operation mode