55
66from splitio .api import APIException
77from splitio .api .client import HttpClientException
8- from splitio .api .commons import headers_from_metadata
9- from splitio .engine .impressions .impressions import ImpressionsMode
8+ from splitio .api .commons import headers_from_metadata , record_telemetry
9+ from splitio .util .time import get_current_epoch_time_ms
10+ from splitio .engine .impressions import ImpressionsMode
11+ from splitio .models .telemetry import HTTPExceptionsAndLatencies
1012
1113
1214_LOGGER = logging .getLogger (__name__ )
1517class ImpressionsAPI (object ): # pylint: disable=too-few-public-methods
1618 """Class that uses an httpClient to communicate with the impressions API."""
1719
18- def __init__ (self , client , apikey , sdk_metadata , mode = ImpressionsMode .OPTIMIZED ):
20+ def __init__ (self , client , apikey , sdk_metadata , telemetry_runtime_producer , mode = ImpressionsMode .OPTIMIZED ):
1921 """
2022 Class constructor.
2123
@@ -28,6 +30,7 @@ def __init__(self, client, apikey, sdk_metadata, mode=ImpressionsMode.OPTIMIZED)
2830 self ._apikey = apikey
2931 self ._metadata = headers_from_metadata (sdk_metadata )
3032 self ._metadata ['SplitSDKImpressionsMode' ] = mode .name
33+ self ._telemetry_runtime_producer = telemetry_runtime_producer
3134
3235 @staticmethod
3336 def _build_bulk (impressions ):
@@ -91,14 +94,16 @@ def flush_impressions(self, impressions):
9194 :type impressions: list
9295 """
9396 bulk = self ._build_bulk (impressions )
97+ start = get_current_epoch_time_ms ()
9498 try :
9599 response = self ._client .post (
96100 'events' ,
97101 '/testImpressions/bulk' ,
98102 self ._apikey ,
99103 body = bulk ,
100- extra_headers = self ._metadata
104+ extra_headers = self ._metadata ,
101105 )
106+ record_telemetry (response .status_code , get_current_epoch_time_ms () - start , HTTPExceptionsAndLatencies .IMPRESSION , self ._telemetry_runtime_producer )
102107 if not 200 <= response .status_code < 300 :
103108 raise APIException (response .body , response .status_code )
104109 except HttpClientException as exc :
@@ -116,14 +121,16 @@ def flush_counters(self, counters):
116121 :type impressions: list
117122 """
118123 bulk = self ._build_counters (counters )
124+ start = get_current_epoch_time_ms ()
119125 try :
120126 response = self ._client .post (
121127 'events' ,
122128 '/testImpressions/count' ,
123129 self ._apikey ,
124130 body = bulk ,
125- extra_headers = self ._metadata
131+ extra_headers = self ._metadata ,
126132 )
133+ record_telemetry (response .status_code , get_current_epoch_time_ms () - start , HTTPExceptionsAndLatencies .IMPRESSION_COUNT , self ._telemetry_runtime_producer )
127134 if not 200 <= response .status_code < 300 :
128135 raise APIException (response .body , response .status_code )
129136 except HttpClientException as exc :
0 commit comments