55import abc
66
77from splitio .optional .loaders import aiohttp
8+ from splitio .util .time import get_current_epoch_time_ms
89
910SDK_URL = 'https://sdk.split.io/api'
1011EVENTS_URL = 'https://events.split.io/api'
@@ -73,6 +74,20 @@ def get(self, server, path, apikey):
7374 def post (self , server , path , apikey ):
7475 """http post request"""
7576
77+ def set_telemetry_data (self , metric_name , telemetry_runtime_producer ):
78+ """
79+ Set the data needed for telemetry call
80+
81+ :param metric_name: metric name for telemetry
82+ :type metric_name: str
83+
84+ :param telemetry_runtime_producer: telemetry recording instance
85+ :type telemetry_runtime_producer: splitio.engine.telemetry.TelemetryRuntimeProducer
86+ """
87+ self ._telemetry_runtime_producer = telemetry_runtime_producer
88+ self ._metric_name = metric_name
89+
90+
7691class HttpClient (HttpClientBase ):
7792 """HttpClient wrapper."""
7893
@@ -116,13 +131,15 @@ def get(self, server, path, sdk_key, query=None, extra_headers=None): # pylint:
116131 if extra_headers is not None :
117132 headers .update (extra_headers )
118133
134+ start = get_current_epoch_time_ms ()
119135 try :
120136 response = requests .get (
121137 _build_url (server , path , self ._urls ),
122138 params = query ,
123139 headers = headers ,
124140 timeout = self ._timeout
125141 )
142+ self ._record_telemetry (response .status_code , get_current_epoch_time_ms () - start )
126143 return HttpResponse (response .status_code , response .text , response .headers )
127144 except Exception as exc : # pylint: disable=broad-except
128145 raise HttpClientException ('requests library is throwing exceptions' ) from exc
@@ -152,6 +169,7 @@ def post(self, server, path, sdk_key, body, query=None, extra_headers=None): #
152169 if extra_headers is not None :
153170 headers .update (extra_headers )
154171
172+ start = get_current_epoch_time_ms ()
155173 try :
156174 response = requests .post (
157175 _build_url (server , path , self ._urls ),
@@ -160,10 +178,28 @@ def post(self, server, path, sdk_key, body, query=None, extra_headers=None): #
160178 headers = headers ,
161179 timeout = self ._timeout
162180 )
181+ self ._record_telemetry (response .status_code , get_current_epoch_time_ms () - start )
163182 return HttpResponse (response .status_code , response .text , response .headers )
164183 except Exception as exc : # pylint: disable=broad-except
165184 raise HttpClientException ('requests library is throwing exceptions' ) from exc
166185
186+ def _record_telemetry (self , status_code , elapsed ):
187+ """
188+ Record Telemetry info
189+
190+ :param status_code: http request status code
191+ :type status_code: int
192+
193+ :param elapsed: response time elapsed.
194+ :type status_code: int
195+ """
196+ self ._telemetry_runtime_producer .record_sync_latency (self ._metric_name , elapsed )
197+ if 200 <= status_code < 300 :
198+ self ._telemetry_runtime_producer .record_successful_sync (self ._metric_name , get_current_epoch_time_ms ())
199+ return
200+ self ._telemetry_runtime_producer .record_sync_error (self ._metric_name , status_code )
201+
202+
167203class HttpClientAsync (HttpClientBase ):
168204 """HttpClientAsync wrapper."""
169205
@@ -204,6 +240,7 @@ async def get(self, server, path, apikey, query=None, extra_headers=None): # py
204240 headers = _build_basic_headers (apikey )
205241 if extra_headers is not None :
206242 headers .update (extra_headers )
243+ start = get_current_epoch_time_ms ()
207244 try :
208245 async with self ._session .get (
209246 _build_url (server , path , self ._urls ),
@@ -212,6 +249,7 @@ async def get(self, server, path, apikey, query=None, extra_headers=None): # py
212249 timeout = self ._timeout
213250 ) as response :
214251 body = await response .text ()
252+ await self ._record_telemetry (response .status , get_current_epoch_time_ms () - start )
215253 return HttpResponse (response .status , body , response .headers )
216254 except aiohttp .ClientError as exc : # pylint: disable=broad-except
217255 raise HttpClientException ('aiohttp library is throwing exceptions' ) from exc
@@ -237,6 +275,7 @@ async def post(self, server, path, apikey, body, query=None, extra_headers=None)
237275 headers = _build_basic_headers (apikey )
238276 if extra_headers is not None :
239277 headers .update (extra_headers )
278+ start = get_current_epoch_time_ms ()
240279 try :
241280 async with self ._session .post (
242281 _build_url (server , path , self ._urls ),
@@ -246,6 +285,23 @@ async def post(self, server, path, apikey, body, query=None, extra_headers=None)
246285 timeout = self ._timeout
247286 ) as response :
248287 body = await response .text ()
288+ await self ._record_telemetry (response .status , get_current_epoch_time_ms () - start )
249289 return HttpResponse (response .status , body , response .headers )
250290 except aiohttp .ClientError as exc : # pylint: disable=broad-except
251- raise HttpClientException ('aiohttp library is throwing exceptions' ) from exc
291+ raise HttpClientException ('aiohttp library is throwing exceptions' ) from exc
292+
293+ async def _record_telemetry (self , status_code , elapsed ):
294+ """
295+ Record Telemetry info
296+
297+ :param status_code: http request status code
298+ :type status_code: int
299+
300+ :param elapsed: response time elapsed.
301+ :type status_code: int
302+ """
303+ await self ._telemetry_runtime_producer .record_sync_latency (self ._metric_name , elapsed )
304+ if 200 <= status_code < 300 :
305+ await self ._telemetry_runtime_producer .record_successful_sync (self ._metric_name , get_current_epoch_time_ms ())
306+ return
307+ await self ._telemetry_runtime_producer .record_sync_error (self ._metric_name , status_code )
0 commit comments