1515import json
1616import logging
1717import threading
18+ import time
1819
1920import requests
2021from azure .core .exceptions import ClientAuthenticationError
@@ -54,7 +55,9 @@ def _transmit(self, envelopes):
5455 """
5556 if not envelopes :
5657 return 0
58+ exception = None
5759 try :
60+ start_time = time .time ()
5861 headers = {
5962 'Accept' : 'application/json' ,
6063 'Content-Type' : 'application/json; charset=utf-8' ,
@@ -67,6 +70,9 @@ def _transmit(self, envelopes):
6770 endpoint += '/v2.1/track'
6871 else :
6972 endpoint += '/v2/track'
73+ if self ._check_stats_collection ():
74+ with _requests_lock :
75+ _requests_map ['count' ] = _requests_map .get ('count' , 0 ) + 1 # noqa: E501
7076 response = requests .post (
7177 url = endpoint ,
7278 data = json .dumps (envelopes ),
@@ -77,23 +83,37 @@ def _transmit(self, envelopes):
7783 except requests .Timeout :
7884 logger .warning (
7985 'Request time out. Ingestion may be backed up. Retrying.' )
80- return self .options .minimum_retry_interval
86+ exception = self .options .minimum_retry_interval
8187 except requests .RequestException as ex :
8288 logger .warning (
8389 'Retrying due to transient client side error %s.' , ex )
90+ if self ._check_stats_collection ():
91+ with _requests_lock :
92+ _requests_map ['exception' ] = _requests_map .get ('exception' , 0 ) + 1 # noqa: E501
8493 # client side error (retryable)
85- return self .options .minimum_retry_interval
94+ exception = self .options .minimum_retry_interval
8695 except CredentialUnavailableError as ex :
8796 logger .warning ('Credential error. %s. Dropping telemetry.' , ex )
88- return - 1
97+ exception = - 1
8998 except ClientAuthenticationError as ex :
9099 logger .warning ('Authentication error %s' , ex )
91- return self .options .minimum_retry_interval
100+ exception = self .options .minimum_retry_interval
92101 except Exception as ex :
93102 logger .warning (
94103 'Error when sending request %s. Dropping telemetry.' , ex )
104+ if self ._check_stats_collection ():
105+ with _requests_lock :
106+ _requests_map ['exception' ] = _requests_map .get ('exception' , 0 ) + 1 # noqa: E501
95107 # Extraneous error (non-retryable)
96- return - 1
108+ exception = - 1
109+ finally :
110+ end_time = time .time ()
111+ if self ._check_stats_collection ():
112+ with _requests_lock :
113+ duration = _requests_map .get ('duration' , 0 )
114+ _requests_map ['duration' ] = duration + (end_time - start_time ) # noqa: E501
115+ if exception is not None :
116+ return exception
97117
98118 text = 'N/A'
99119 data = None
@@ -111,6 +131,10 @@ def _transmit(self, envelopes):
111131 with _requests_lock :
112132 _requests_map ['success' ] = _requests_map .get ('success' , 0 ) + 1 # noqa: E501
113133 return 0
134+ # Status code not 200 counts as failure
135+ if self ._check_stats_collection ():
136+ with _requests_lock :
137+ _requests_map ['failure' ] = _requests_map .get ('failure' , 0 ) + 1 # noqa: E501
114138 if response .status_code == 206 : # Partial Content
115139 if data :
116140 try :
@@ -138,6 +162,9 @@ def _transmit(self, envelopes):
138162 text ,
139163 ex ,
140164 )
165+ if self ._check_stats_collection ():
166+ with _requests_lock :
167+ _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
141168 return - response .status_code
142169 # cannot parse response body, fallback to retry
143170 if response .status_code in (
@@ -152,6 +179,11 @@ def _transmit(self, envelopes):
152179 text ,
153180 )
154181 # server side error (retryable)
182+ if self ._check_stats_collection ():
183+ with _requests_lock :
184+ _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
185+ if response .status_code == 429 :
186+ _requests_map ['throttle' ] = _requests_map .get ('throttle' , 0 ) + 1 # noqa: E501
155187 return self .options .minimum_retry_interval
156188 # Authentication error
157189 if response .status_code == 401 :
@@ -160,6 +192,9 @@ def _transmit(self, envelopes):
160192 response .status_code ,
161193 text ,
162194 )
195+ if self ._check_stats_collection ():
196+ with _requests_lock :
197+ _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
163198 return self .options .minimum_retry_interval
164199 # Forbidden error
165200 # Can occur when v2 endpoint is used while AI resource is configured
@@ -170,6 +205,9 @@ def _transmit(self, envelopes):
170205 response .status_code ,
171206 text ,
172207 )
208+ if self ._check_stats_collection ():
209+ with _requests_lock :
210+ _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
173211 return self .options .minimum_retry_interval
174212 logger .error (
175213 'Non-retryable server side error %s: %s.' ,
0 commit comments